Skip to content

Commit

Permalink
auth: Deprecate getUserId in favor of getUserInfo
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
otaviojacobi committed Aug 10, 2023
1 parent 10d43bf commit c50ed7b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 41 deletions.
8 changes: 5 additions & 3 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ const sdk = fromSharedOptions();
* [.isLoggedIn()](#balena.auth.isLoggedIn) ⇒ <code>Promise</code>
* [.getToken()](#balena.auth.getToken) ⇒ <code>Promise</code>
* [.getUserInfo()](#balena.auth.getUserInfo) ⇒ <code>Promise</code>
* [.getUserId()](#balena.auth.getUserId) ⇒ <code>Promise</code>
* ~~[.getUserId()](#balena.auth.getUserId) ⇒ <code>Promise</code>~~
* [.getUserActorId()](#balena.auth.getUserActorId) ⇒ <code>Promise</code>
* ~~[.getEmail()](#balena.auth.getEmail) ⇒ <code>Promise</code>~~
* [.logout()](#balena.auth.logout) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -6485,7 +6485,7 @@ balena.models.billing.downloadInvoice(orgId, '0000').then(function(stream) {
* [.isLoggedIn()](#balena.auth.isLoggedIn) ⇒ <code>Promise</code>
* [.getToken()](#balena.auth.getToken) ⇒ <code>Promise</code>
* [.getUserInfo()](#balena.auth.getUserInfo) ⇒ <code>Promise</code>
* [.getUserId()](#balena.auth.getUserId) ⇒ <code>Promise</code>
* ~~[.getUserId()](#balena.auth.getUserId) ⇒ <code>Promise</code>~~
* [.getUserActorId()](#balena.auth.getUserActorId) ⇒ <code>Promise</code>
* ~~[.getEmail()](#balena.auth.getEmail) ⇒ <code>Promise</code>~~
* [.logout()](#balena.auth.logout) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -6759,7 +6759,9 @@ balena.auth.getUserInfo().then(function(userInfo) {
```
<a name="balena.auth.getUserId"></a>

#### auth.getUserId() ⇒ <code>Promise</code>
#### ~~auth.getUserId() ⇒ <code>Promise</code>~~
***Deprecated***

This will only work if you used [login](#balena.auth.login) to log in.

**Kind**: static method of [<code>auth</code>](#balena.auth)
Expand Down
6 changes: 4 additions & 2 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ const getAuth = function (
* @public
* @function
* @memberof balena.auth
* @deprecated use balena.auth.getUserInfo()
*
* @description This will only work if you used {@link balena.auth.login} to log in.
*
Expand Down Expand Up @@ -357,9 +358,10 @@ const getAuth = function (
* });
*/
async function getUserActorId(): Promise<number> {
const { id } = await getUserInfo();
const { actor } = (await pine.get({
resource: 'user',
id: await getUserId(),
id,
options: {
$select: 'actor',
},
Expand Down Expand Up @@ -513,7 +515,7 @@ const getAuth = function (
*
*/
async function requestVerificationEmail() {
const id = await getUserId();
const { id } = await getUserInfo();
await pine.patch({
resource: 'user',
id,
Expand Down
43 changes: 22 additions & 21 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1362,28 +1362,29 @@ const getDeviceModel = function (
$expand: { is_for__device_type: deviceTypeOptions },
} as const;

const [userId, apiKey, application, deviceType] = await Promise.all([
sdkInstance.auth.getUserId(),
sdkInstance.models.application.generateProvisioningKey(
applicationSlugOrUuidOrId,
),
sdkInstance.models.application.get(
applicationSlugOrUuidOrId,
applicationOptions,
) as Promise<PineTypedResult<Application, typeof applicationOptions>>,
typeof deviceTypeSlug === 'string'
? (sdkInstance.models.deviceType.get(deviceTypeSlug, {
$select: 'slug',
$expand: {
is_of__cpu_architecture: {
$select: 'slug',
const [{ id: userId }, apiKey, application, deviceType] =
await Promise.all([
sdkInstance.auth.getUserInfo(),
sdkInstance.models.application.generateProvisioningKey(
applicationSlugOrUuidOrId,
),
sdkInstance.models.application.get(
applicationSlugOrUuidOrId,
applicationOptions,
) as Promise<PineTypedResult<Application, typeof applicationOptions>>,
typeof deviceTypeSlug === 'string'
? (sdkInstance.models.deviceType.get(deviceTypeSlug, {
$select: 'slug',
$expand: {
is_of__cpu_architecture: {
$select: 'slug',
},
},
},
}) as Promise<
PineTypedResult<DeviceType, typeof deviceTypeOptions>
>)
: null,
]);
}) as Promise<
PineTypedResult<DeviceType, typeof deviceTypeOptions>
>)
: null,
]);
if (deviceType != null) {
const isCompatibleParameter =
sdkInstance.models.os.isArchitectureCompatibleWith(
Expand Down
2 changes: 1 addition & 1 deletion src/models/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const getKeyModel = function (deps: InjectedDependenciesParam) {
// Avoid ugly whitespaces
key = key.trim();

const userId = await sdkInstance.auth.getUserId();
const { id: userId } = await sdkInstance.auth.getUserInfo();
return await pine.post({
resource: 'user__has__public_key',
body: {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('SDK authentication', function () {
email: credentials.register.email,
password: credentials.register.password,
});
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();
await balena.request.send({
method: 'DELETE',
url: `/v2/user(${userId})`,
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/models/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ describe('Application Model', function () {

describe('given two releases', function () {
before(async function () {
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();
this.oldRelease = await balena.pine.post({
resource: 'release',
body: {
Expand Down Expand Up @@ -1350,11 +1350,12 @@ describe('Application Model', function () {
[
'draft',
async function () {
const { id: userId } = await balena.auth.getUserInfo();
this.testNonLatestRelease = await balena.pine.post({
resource: 'release',
body: {
belongs_to__application: this.application.id,
is_created_by__user: await balena.auth.getUserId(),
is_created_by__user: userId,
commit: 'draft-release-commit',
status: 'success',
source: 'cloud',
Expand All @@ -1368,11 +1369,12 @@ describe('Application Model', function () {
[
'invalidated',
async function () {
const { id } = await balena.auth.getUserInfo();
this.testNonLatestRelease = await balena.pine.post({
resource: 'release',
body: {
belongs_to__application: this.application.id,
is_created_by__user: await balena.auth.getUserId(),
is_created_by__user: id,
commit: 'invalidated-release-commit',
status: 'success',
source: 'cloud',
Expand Down
8 changes: 3 additions & 5 deletions tests/integration/models/organization-membership.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
itShouldGetAllTagsByResource,
} from './tags';
import type * as tagsHelper from './tags';
import { UserKeyWhoAmIResponse } from '../../../src';

const keyAlternatives = [
['id', (member: Pick<BalenaSdk.OrganizationMembership, 'id'>) => member.id],
Expand Down Expand Up @@ -44,11 +43,10 @@ describe('Organization Membership Model', function () {
let ctx: Mocha.Context;
before(async function () {
ctx = this;
const userInfoResult =
(await balena.auth.whoami()) as UserKeyWhoAmIResponse;
expect(userInfoResult?.actorType).to.be.eq('user');
const userInfoResult = await balena.auth.getUserInfo();
this.username = userInfoResult.username;
this.userId = await balena.auth.getUserId();
this.userId = userInfoResult.id;

const roles = await balena.pine.get({
resource: 'organization_membership_role',
options: { $select: ['id', 'name'] },
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/models/release.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('Release Model', function () {
const testReleaseByField: Dictionary<BalenaSdk.Release> = {};

before(async function () {
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();
await Promise.all(
uniquePropertyNames.map(async (field, i) => {
const fieldKey = getFieldLabel(field);
Expand Down Expand Up @@ -633,7 +633,7 @@ describe('Release Model', function () {
describe('balena.models.release.getLatestByApplication()', function () {
before(async function () {
ctx = this;
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();

for (const body of [
{
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('Release Model', function () {
describe('given two releases that share the same commit root', function () {
before(async function () {
const { application } = this;
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();
await balena.pine.post({
resource: 'release',
body: {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export function loginPaidUser() {
}

async function resetInitialOrganization() {
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();
const initialOrg = await getInitialOrganization();
await balena.pine.delete({
resource: 'organization_membership',
Expand Down Expand Up @@ -506,7 +506,7 @@ export function givenMulticontainerApplication(beforeFn: Mocha.HookFunction) {
givenAnApplication(beforeFn);

beforeFn(async function () {
const userId = await balena.auth.getUserId();
const { id: userId } = await balena.auth.getUserInfo();
const oldDate = new Date('2017-01-01').toISOString();
const now = new Date().toISOString();
const [webService, dbService, [oldRelease, newRelease]] = await Promise.all(
Expand Down

0 comments on commit c50ed7b

Please sign in to comment.