Skip to content

Commit

Permalink
feat(auth-js, ngx-auth): make public get/remove/store user
Browse files Browse the repository at this point in the history
This enables the use of biometric login
  • Loading branch information
ms-emp committed Dec 11, 2024
1 parent bd5ef76 commit 90fe1c6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion projects/auth-js/oidc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
33 changes: 21 additions & 12 deletions projects/auth-js/oidc/oidc-auth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
}
}),
this.#userManager.events.addSilentRenewError(async () => {
await this.#removeUser();
await this.removeUser();
})
);

Expand Down Expand Up @@ -219,6 +219,18 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
return this.#signinSilent(args).catch(error => console.error(error));
}

public async storeUser(user: User): Promise<void> {
await this.#userManager?.storeUser(user);
}

public async removeUser(): Promise<void> {
this.user = null;
await Promise.all([
this.#userManager?.clearStaleState(),
this.#userManager?.removeUser()
]);
}

public getSettings(): OIDCAuthSettings {
return this.#settings;
}
Expand Down Expand Up @@ -262,6 +274,11 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
return AuthUtils.decodeJwt<AccessToken>(this.#accessToken);
}

public async getUser(): Promise<User | null | undefined> {
await this.#waitForRenew('getUser()');
return this.#user;
}

// --- DESTROY ---

public destroy(): void {
Expand Down Expand Up @@ -388,7 +405,7 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
async #redirect(url: string | null, error?: unknown): Promise<void> {
if (error) {
console.error(error);
await this.#removeUser();
await this.removeUser();
}

const redirectUrl = AuthUtils.stringToURL(url ?? '/');
Expand All @@ -401,21 +418,13 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
}
}

async #removeUser(): Promise<void> {
this.user = null;
await Promise.all([
this.#userManager?.clearStaleState(),
this.#userManager?.removeUser()
]);
}

async #signinSilent(args?: SigninSilentArgs): Promise<void> {
this.#notifyRenew(true);

try {
await this.#userManager?.signinSilent(args);
} catch (error) {
await this.#removeUser();
await this.removeUser();
throw error;
} finally {
this.#notifyRenew(false);
Expand Down Expand Up @@ -453,7 +462,7 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
throw error;
});
await this.#redirect(redirectUrl);
await this.#removeUser();
await this.removeUser();
} catch (error) {
redirectUrl = '/';
await this.#redirect(redirectUrl, error);
Expand Down
23 changes: 22 additions & 1 deletion projects/ngx-auth/core/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -102,6 +102,20 @@ export class AuthService implements OnDestroy {
return this.#manager.renew(args);
}

/**
* @see {@link OIDCAuthManager.storeUser}
*/
public async storeUser(user: User): Promise<void> {
return this.#manager.storeUser(user);
}

/**
* @see {@link OIDCAuthManager.removeUser}
*/
public async removeUser(): Promise<void> {
return this.#manager.removeUser();
}

/**
* @see {@link OIDCAuthManager.getSettings}
*/
Expand Down Expand Up @@ -165,6 +179,13 @@ export class AuthService implements OnDestroy {
return this.#manager.getAccessTokenDecoded();
}

/**
* @see {@link OIDCAuthManager.getUser}
*/
public async getUser(): Promise<User | null | undefined> {
return this.#manager.getUser();
}

// --- HELPER(s) ----

#listenForManagerChanges(): void {
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-auth/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 90fe1c6

Please sign in to comment.