From b2cc654deb3cb3e84c3ccb7374ab59a6e789b35e Mon Sep 17 00:00:00 2001 From: Moritz Becker Date: Tue, 19 Nov 2024 12:22:16 +0100 Subject: [PATCH] Incorporate observable of loginWithRedirect in observable returned by AuthGuard (#659) --- projects/auth0-angular/src/lib/auth.guard.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/projects/auth0-angular/src/lib/auth.guard.ts b/projects/auth0-angular/src/lib/auth.guard.ts index 1573074b..c734e2a2 100644 --- a/projects/auth0-angular/src/lib/auth.guard.ts +++ b/projects/auth0-angular/src/lib/auth.guard.ts @@ -8,8 +8,8 @@ import { UrlSegment, CanActivateChild, } from '@angular/router'; -import { Observable } from 'rxjs'; -import { tap, take } from 'rxjs/operators'; +import { Observable, of } from 'rxjs'; +import { take, switchMap, map } from 'rxjs/operators'; import { AuthService } from './auth.service'; @Injectable({ @@ -40,12 +40,15 @@ export class AuthGuard implements CanActivate, CanLoad, CanActivateChild { state: RouterStateSnapshot ): Observable { return this.auth.isAuthenticated$.pipe( - tap((loggedIn) => { + switchMap((loggedIn) => { if (!loggedIn) { - this.auth.loginWithRedirect({ - appState: { target: state.url }, - }); + return this.auth + .loginWithRedirect({ + appState: { target: state.url }, + }) + .pipe(map(() => false)); } + return of(true); }) ); }