Skip to content

Commit

Permalink
Incorporate observable of loginWithRedirect in observable returned by…
Browse files Browse the repository at this point in the history
… AuthGuard (#659)
  • Loading branch information
Mobe91 committed Nov 19, 2024
1 parent 81dd79c commit 4553e0c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions projects/auth0-angular/src/lib/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -40,12 +40,15 @@ export class AuthGuard implements CanActivate, CanLoad, CanActivateChild {
state: RouterStateSnapshot
): Observable<boolean> {
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);
})
);
}
Expand Down

0 comments on commit 4553e0c

Please sign in to comment.