Skip to content

Commit

Permalink
auth: Add getUserInfo
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
otaviojacobi committed Aug 10, 2023
1 parent a3dabec commit 7b92c31
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
17 changes: 17 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ const sdk = fromSharedOptions();
* [.loginWithToken(authToken)](#balena.auth.loginWithToken) ⇒ <code>Promise</code>
* [.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>
* [.getUserActorId()](#balena.auth.getUserActorId) ⇒ <code>Promise</code>
* [.getEmail()](#balena.auth.getEmail) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -6483,6 +6484,7 @@ balena.models.billing.downloadInvoice(orgId, '0000').then(function(stream) {
* [.loginWithToken(authToken)](#balena.auth.loginWithToken) ⇒ <code>Promise</code>
* [.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>
* [.getUserActorId()](#balena.auth.getUserActorId) ⇒ <code>Promise</code>
* [.getEmail()](#balena.auth.getEmail) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -6740,6 +6742,21 @@ balena.auth.getToken().then(function(token) {
console.log(token);
});
```
<a name="balena.auth.getUserInfo"></a>

#### auth.getUserInfo() ⇒ <code>Promise</code>
This will only work if you used [login](#balena.auth.login) to log in.

**Kind**: static method of [<code>auth</code>](#balena.auth)
**Summary**: Get current logged in user's info
**Access**: public
**Fulfil**: <code>Object</code> - user info
**Example**
```js
balena.auth.getUserInfo().then(function(userInfo) {
console.log(userInfo);
});
```
<a name="balena.auth.getUserId"></a>

#### auth.getUserId() ⇒ <code>Promise</code>
Expand Down
35 changes: 34 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import * as errors from 'balena-errors';
import memoizee from 'memoizee';
import type { InjectedDependenciesParam, InjectedOptionsParam } from '.';
import { WhoamiResult } from './types/auth';
import { UserInfo, WhoamiResult } from './types/auth';

const getAuth = function (
deps: InjectedDependenciesParam,
Expand Down Expand Up @@ -279,6 +279,38 @@ const getAuth = function (
});
}

/**
* @summary Get current logged in user's info
* @name getUserInfo
* @public
* @function
* @memberof balena.auth
*
* @description This will only work if you used {@link balena.auth.login} to log in.
*
* @fulfil {Object} - user info
* @returns {Promise}
*
* @example
* balena.auth.getUserInfo().then(function(userInfo) {
* console.log(userInfo);
* });
*/
async function getUserInfo(): Promise<UserInfo> {
const actor = await getActorDetails();

if (actor.actorType !== 'user') {
throw new Error(
'The authentication credentials in use are not of a user',
);
}
return {
id: actor.actorTypeId,
email: actor.email,
username: actor.username,
};
}

/**
* @summary Get current logged in user's id
* @name getUserId
Expand Down Expand Up @@ -501,6 +533,7 @@ const getAuth = function (
getUserId,
getUserActorId,
getEmail,
getUserInfo,
logout,
register,
verifyEmail,
Expand Down
6 changes: 6 additions & 0 deletions src/types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ export type WhoamiResult =
| UserKeyWhoAmIResponse
| ApplicationKeyWhoAmIResponse
| DeviceKeyWhoAmIResponse;

export interface UserInfo {
id: number;
username: string;
email: string | null;
}
60 changes: 60 additions & 0 deletions tests/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaNotLoggedIn',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -215,6 +225,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaNotLoggedIn',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -265,6 +285,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const userInfo = await balena.auth.getUserInfo();
expect(userInfo.email).to.equal(credentials.email);
expect(userInfo.username).to.equal(credentials.username);
expect(userInfo.id).to.be.a('number');
expect(userInfo.id).to.be.greaterThan(0);
});
});

describe('balena.auth.getUserId()', () => {
it('should eventually be a user id', async () => {
const userId = await balena.auth.getUserId();
Expand Down Expand Up @@ -321,6 +351,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'message',
'The authentication credentials in use are not of a user',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async () => {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -389,6 +429,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const promise = balena.auth.getUserInfo();
await expect(promise).to.be.rejected.and.eventually.have.property(
'message',
'The authentication credentials in use are not of a user',
);
});
});

describe('balena.auth.getUserId()', () => {
it('should be rejected with an error', async () => {
const promise = balena.auth.getUserId();
Expand Down Expand Up @@ -453,6 +503,16 @@ describe('SDK authentication', function () {
});
});

describe('balena.auth.getUserInfo()', () => {
it('should be rejected with an error', async function () {
const userInfo = await balena.auth.getUserInfo();
expect(userInfo.email).to.equal(credentials.email);
expect(userInfo.username).to.equal(credentials.username);
expect(userInfo.id).to.be.a('number');
expect(userInfo.id).to.be.greaterThan(0);
});
});

describe('balena.auth.getUserId()', () => {
it('should eventually be a user id', async () => {
const userId = await balena.auth.getUserId();
Expand Down

0 comments on commit 7b92c31

Please sign in to comment.