diff --git a/packages/core-api/src/apis/definitions/IdentityApi.ts b/packages/core-api/src/apis/definitions/IdentityApi.ts index 4c9ccfdbf6..5da9ed4814 100644 --- a/packages/core-api/src/apis/definitions/IdentityApi.ts +++ b/packages/core-api/src/apis/definitions/IdentityApi.ts @@ -38,6 +38,11 @@ export type IdentityApi = { getIdToken(): string | undefined; // TODO: getProfile(): Promise - We want this to be async when added, but needs more work. + + /** + * Log out the current user + */ + logout(): Promise; }; export const identityApiRef = createApiRef({ diff --git a/packages/core-api/src/app/AppIdentity.ts b/packages/core-api/src/app/AppIdentity.ts index a0f933710b..77de445a20 100644 --- a/packages/core-api/src/app/AppIdentity.ts +++ b/packages/core-api/src/app/AppIdentity.ts @@ -22,21 +22,22 @@ import { SignInResult } from './types'; * and sign-in page. */ export class AppIdentity implements IdentityApi { + private hasIdentity = false; private userId?: string; private idToken?: string; private logoutFunc?: () => Promise; getUserId(): string { - if (!this.userId) { + if (!this.hasIdentity) { throw new Error( 'Tried to access IdentityApi userId before app was loaded', ); } - return this.userId; + return this.userId!; } - getIdToken(): string { - if (!this.idToken) { + getIdToken(): string | undefined { + if (!this.hasIdentity) { throw new Error( 'Tried to access IdentityApi idToken before app was loaded', ); @@ -45,15 +46,23 @@ export class AppIdentity implements IdentityApi { } async logout(): Promise { - if (!this.logoutFunc) { + if (!this.hasIdentity) { throw new Error( 'Tried to access IdentityApi logoutFunc before app was loaded', ); } - await this.logoutFunc; + await this.logoutFunc?.(); + location.reload(); } setSignInResult(result: SignInResult) { + if (this.hasIdentity) { + return; + } + if (!result.userId) { + throw new Error('Invalid sign-in result, userId not set'); + } + this.hasIdentity = true; this.userId = result.userId; this.idToken = result.idToken; this.logoutFunc = result.logout;