From 90fe1c662a0cbe18a3c24c01711806bc6eeef2b6 Mon Sep 17 00:00:00 2001 From: Mordechai Senderowitz Date: Wed, 11 Dec 2024 15:50:24 -0500 Subject: [PATCH] feat(auth-js, ngx-auth): make public get/remove/store user This enables the use of biometric login --- projects/auth-js/oidc/index.ts | 2 +- projects/auth-js/oidc/oidc-auth-manager.ts | 33 ++++++++++++++-------- projects/ngx-auth/core/auth.service.ts | 23 ++++++++++++++- projects/ngx-auth/core/index.ts | 2 +- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/projects/auth-js/oidc/index.ts b/projects/auth-js/oidc/index.ts index 215b29e..f5daba2 100644 --- a/projects/auth-js/oidc/index.ts +++ b/projects/auth-js/oidc/index.ts @@ -6,7 +6,7 @@ * Copyright (C) 2018 Badisi */ -export { Log } from 'oidc-client-ts'; +export { Log, User } from 'oidc-client-ts'; export type { UserProfile } from 'oidc-client-ts'; export type { Optional, AuthSubscriber, AuthSubscription } from '../core'; diff --git a/projects/auth-js/oidc/oidc-auth-manager.ts b/projects/auth-js/oidc/oidc-auth-manager.ts index ef57fe8..8a9e037 100644 --- a/projects/auth-js/oidc/oidc-auth-manager.ts +++ b/projects/auth-js/oidc/oidc-auth-manager.ts @@ -121,7 +121,7 @@ export class OIDCAuthManager extends AuthManager { } }), this.#userManager.events.addSilentRenewError(async () => { - await this.#removeUser(); + await this.removeUser(); }) ); @@ -219,6 +219,18 @@ export class OIDCAuthManager extends AuthManager { return this.#signinSilent(args).catch(error => console.error(error)); } + public async storeUser(user: User): Promise { + await this.#userManager?.storeUser(user); + } + + public async removeUser(): Promise { + this.user = null; + await Promise.all([ + this.#userManager?.clearStaleState(), + this.#userManager?.removeUser() + ]); + } + public getSettings(): OIDCAuthSettings { return this.#settings; } @@ -262,6 +274,11 @@ export class OIDCAuthManager extends AuthManager { return AuthUtils.decodeJwt(this.#accessToken); } + public async getUser(): Promise { + await this.#waitForRenew('getUser()'); + return this.#user; + } + // --- DESTROY --- public destroy(): void { @@ -388,7 +405,7 @@ export class OIDCAuthManager extends AuthManager { async #redirect(url: string | null, error?: unknown): Promise { if (error) { console.error(error); - await this.#removeUser(); + await this.removeUser(); } const redirectUrl = AuthUtils.stringToURL(url ?? '/'); @@ -401,21 +418,13 @@ export class OIDCAuthManager extends AuthManager { } } - async #removeUser(): Promise { - this.user = null; - await Promise.all([ - this.#userManager?.clearStaleState(), - this.#userManager?.removeUser() - ]); - } - async #signinSilent(args?: SigninSilentArgs): Promise { this.#notifyRenew(true); try { await this.#userManager?.signinSilent(args); } catch (error) { - await this.#removeUser(); + await this.removeUser(); throw error; } finally { this.#notifyRenew(false); @@ -453,7 +462,7 @@ export class OIDCAuthManager extends AuthManager { throw error; }); await this.#redirect(redirectUrl); - await this.#removeUser(); + await this.removeUser(); } catch (error) { redirectUrl = '/'; await this.#redirect(redirectUrl, error); diff --git a/projects/ngx-auth/core/auth.service.ts b/projects/ngx-auth/core/auth.service.ts index e3fd287..ae69d94 100644 --- a/projects/ngx-auth/core/auth.service.ts +++ b/projects/ngx-auth/core/auth.service.ts @@ -2,7 +2,7 @@ import { inject, Injectable, NgZone, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import { AccessToken, AuthSubscription, AuthUtils, IdToken, LoginArgs, LogoutArgs, OIDCAuthManager, - RenewArgs, UserProfile, UserSession + RenewArgs, User, UserProfile, UserSession } from '@badisi/auth-js/oidc'; import { Observable, ReplaySubject } from 'rxjs'; import { distinctUntilChanged, map } from 'rxjs/operators'; @@ -102,6 +102,20 @@ export class AuthService implements OnDestroy { return this.#manager.renew(args); } + /** + * @see {@link OIDCAuthManager.storeUser} + */ + public async storeUser(user: User): Promise { + return this.#manager.storeUser(user); + } + + /** + * @see {@link OIDCAuthManager.removeUser} + */ + public async removeUser(): Promise { + return this.#manager.removeUser(); + } + /** * @see {@link OIDCAuthManager.getSettings} */ @@ -165,6 +179,13 @@ export class AuthService implements OnDestroy { return this.#manager.getAccessTokenDecoded(); } + /** + * @see {@link OIDCAuthManager.getUser} + */ + public async getUser(): Promise { + return this.#manager.getUser(); + } + // --- HELPER(s) ---- #listenForManagerChanges(): void { diff --git a/projects/ngx-auth/core/index.ts b/projects/ngx-auth/core/index.ts index f82118f..866e348 100644 --- a/projects/ngx-auth/core/index.ts +++ b/projects/ngx-auth/core/index.ts @@ -6,7 +6,7 @@ * Copyright (C) 2018 Badisi */ -export { Log, AuthUtils, UserSession, DesktopNavigation } from '@badisi/auth-js/oidc'; +export { Log, AuthUtils, User, UserSession, DesktopNavigation } from '@badisi/auth-js/oidc'; export type { UserProfile, AccessToken, IdToken, MobileWindowParams, LoginArgs, LogoutArgs, RenewArgs, SigninMobileArgs, SignoutMobileArgs } from '@badisi/auth-js/oidc';