Loading libs/authentication/components/src/lib/loading/loading.component.ts +7 −4 Original line number Diff line number Diff line Loading @@ -29,11 +29,14 @@ export class LoadingComponent implements OnInit, OnDestroy { ) {} public ngOnInit(): void { // TODO : remove the concept of isAuthenticated$ if (this.authentication.isAuthenticated$) { this.subscription = this.authentication.isAuthenticated$.pipe( filter(isAuthenticated => isAuthenticated !== null), tap(() => this.router.navigate([ '/', 'authentication', 'login' ])) ).subscribe(); } } public ngOnDestroy(): void { this.subscription?.unsubscribe(); Loading libs/authentication/src/lib/authentication.guard.ts +41 −47 Original line number Diff line number Diff line Loading @@ -38,7 +38,7 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild { public canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<UrlTree | boolean> | boolean { ): Promise<UrlTree | boolean> { console.debug('[RxapAuthenticationGuard] can activate', state.url); return this.checkAuthStatus(route, state); } Loading @@ -46,27 +46,22 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild { public canActivateChild( childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<UrlTree | boolean> | boolean { ): Promise<UrlTree | boolean> { console.debug('[RxapAuthenticationGuard] can activate child', state.url); return this.checkAuthStatus(childRoute, state); } public isAuthenticated(): Observable<boolean | null> { return this.authentication.isAuthenticated$; } private checkAuthStatus( private async checkAuthStatus( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ) { ): Promise<UrlTree | boolean> { if (this.deactivated) { return true; } return this.isAuthenticated().pipe( take(1), map(authenticated => { const authenticated = await this.authentication.isAuthenticated(); if (authenticated === null) { if (!this.lastUrl || (state.url !== '/' && !state.url.match(/authentication\/login/))) { this.lastUrl = state.url; Loading Loading @@ -104,8 +99,7 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild { return this.router.createUrlTree([ '/', 'authentication', 'login' ]); } } }) ); } private createUrlTreeToLastUrl(lastUrl: string): UrlTree { Loading libs/authentication/src/lib/authentication.service.ts +4 −8 Original line number Diff line number Diff line Loading @@ -22,7 +22,7 @@ export interface IAuthenticationService { signInWithEmailAndPassword(email: string, password: string, remember: boolean): Promise<boolean>; signOut(): Promise<boolean>; signOut(): Promise<void>; } Loading Loading @@ -87,15 +87,11 @@ export class RxapAuthenticationService implements IAuthenticationService { } public async signOut(): Promise<boolean> { public async signOut(): Promise<void> { console.warn('The default RxapAuthenticationService implementation should only be used in a development environment!'); const logout = true; this.isAuthenticated$.next(!logout); if (logout) { this.isAuthenticated$.next(false); localStorage.removeItem(RXAP_AUTHENTICATION_LOCAL_STORAGE_KEY); } return logout; } public signInWithProvider(provider: string): Promise<boolean> { console.warn('The default RxapAuthenticationService implementation should only be used in a development environment!'); Loading libs/authentication/src/lib/sign-out.directive.ts +7 −5 Original line number Diff line number Diff line Loading @@ -21,11 +21,13 @@ export class SignOutDirective { constructor(private readonly auth: RxapAuthenticationService) { } @HostListener('click') public onClick() { this.auth .signOut() .then(() => this.successful.emit()) .catch(error => this.failure.emit(error)); public async onClick() { try { await this.auth.signOut(); this.successful.emit(); } catch (e) { this.failure.emit(e); } } } Loading Loading
libs/authentication/components/src/lib/loading/loading.component.ts +7 −4 Original line number Diff line number Diff line Loading @@ -29,11 +29,14 @@ export class LoadingComponent implements OnInit, OnDestroy { ) {} public ngOnInit(): void { // TODO : remove the concept of isAuthenticated$ if (this.authentication.isAuthenticated$) { this.subscription = this.authentication.isAuthenticated$.pipe( filter(isAuthenticated => isAuthenticated !== null), tap(() => this.router.navigate([ '/', 'authentication', 'login' ])) ).subscribe(); } } public ngOnDestroy(): void { this.subscription?.unsubscribe(); Loading
libs/authentication/src/lib/authentication.guard.ts +41 −47 Original line number Diff line number Diff line Loading @@ -38,7 +38,7 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild { public canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<UrlTree | boolean> | boolean { ): Promise<UrlTree | boolean> { console.debug('[RxapAuthenticationGuard] can activate', state.url); return this.checkAuthStatus(route, state); } Loading @@ -46,27 +46,22 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild { public canActivateChild( childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<UrlTree | boolean> | boolean { ): Promise<UrlTree | boolean> { console.debug('[RxapAuthenticationGuard] can activate child', state.url); return this.checkAuthStatus(childRoute, state); } public isAuthenticated(): Observable<boolean | null> { return this.authentication.isAuthenticated$; } private checkAuthStatus( private async checkAuthStatus( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ) { ): Promise<UrlTree | boolean> { if (this.deactivated) { return true; } return this.isAuthenticated().pipe( take(1), map(authenticated => { const authenticated = await this.authentication.isAuthenticated(); if (authenticated === null) { if (!this.lastUrl || (state.url !== '/' && !state.url.match(/authentication\/login/))) { this.lastUrl = state.url; Loading Loading @@ -104,8 +99,7 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild { return this.router.createUrlTree([ '/', 'authentication', 'login' ]); } } }) ); } private createUrlTreeToLastUrl(lastUrl: string): UrlTree { Loading
libs/authentication/src/lib/authentication.service.ts +4 −8 Original line number Diff line number Diff line Loading @@ -22,7 +22,7 @@ export interface IAuthenticationService { signInWithEmailAndPassword(email: string, password: string, remember: boolean): Promise<boolean>; signOut(): Promise<boolean>; signOut(): Promise<void>; } Loading Loading @@ -87,15 +87,11 @@ export class RxapAuthenticationService implements IAuthenticationService { } public async signOut(): Promise<boolean> { public async signOut(): Promise<void> { console.warn('The default RxapAuthenticationService implementation should only be used in a development environment!'); const logout = true; this.isAuthenticated$.next(!logout); if (logout) { this.isAuthenticated$.next(false); localStorage.removeItem(RXAP_AUTHENTICATION_LOCAL_STORAGE_KEY); } return logout; } public signInWithProvider(provider: string): Promise<boolean> { console.warn('The default RxapAuthenticationService implementation should only be used in a development environment!'); Loading
libs/authentication/src/lib/sign-out.directive.ts +7 −5 Original line number Diff line number Diff line Loading @@ -21,11 +21,13 @@ export class SignOutDirective { constructor(private readonly auth: RxapAuthenticationService) { } @HostListener('click') public onClick() { this.auth .signOut() .then(() => this.successful.emit()) .catch(error => this.failure.emit(error)); public async onClick() { try { await this.auth.signOut(); this.successful.emit(); } catch (e) { this.failure.emit(e); } } } Loading