Skip to content

Commit c0de0dd

Browse files
author
Steve Hobbs
authored
Merge pull request #202 from kevinsschmidt/fix/auth-redirects
fixed auth redirects
2 parents 6ab4b71 + b7c3a1b commit c0de0dd

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

01-Login/src/app/auth/auth.guard.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@angular/router';
88
import { Observable } from 'rxjs';
99
import { AuthService } from './auth.service';
10-
import { tap } from 'rxjs/operators';
10+
import { skipWhile, switchMap, tap } from 'rxjs/operators';
1111

1212
@Injectable({
1313
providedIn: 'root'
@@ -20,13 +20,14 @@ export class AuthGuard implements CanActivate {
2020
next: ActivatedRouteSnapshot,
2121
state: RouterStateSnapshot
2222
): Observable<boolean> | Promise<boolean|UrlTree> | boolean {
23-
return this.auth.isAuthenticated$.pipe(
23+
return this.auth.isAuthInProgress$.pipe(
24+
skipWhile((inProgress: boolean) => inProgress),
25+
switchMap(inProgress => this.auth.isAuthenticated$),
2426
tap(loggedIn => {
2527
if (!loggedIn) {
2628
this.auth.login(state.url);
2729
}
28-
})
29-
);
30+
}));
3031
}
3132

3233
}

01-Login/src/app/auth/auth.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export class AuthService {
3535
// Create subject and public observable of user profile data
3636
private userProfileSubject$ = new BehaviorSubject<any>(null);
3737
userProfile$ = this.userProfileSubject$.asObservable();
38+
39+
isAuthInProgress$ = new BehaviorSubject<boolean>(false);
40+
3841
// Create a local property for login status
3942
loggedIn: boolean = null;
4043

@@ -89,6 +92,8 @@ export class AuthService {
8992
// Call when app reloads after user logs in with Auth0
9093
const params = window.location.search;
9194
if (params.includes('code=') && params.includes('state=')) {
95+
this.isAuthInProgress$.next(true);
96+
9297
let targetRoute: string; // Path to redirect to after login processsed
9398
const authComplete$ = this.handleRedirectCallback$.pipe(
9499
// Have client, now call method to handle auth callback redirect
@@ -107,6 +112,8 @@ export class AuthService {
107112
// Subscribe to authentication completion observable
108113
// Response will be an array of user and login status
109114
authComplete$.subscribe(([user, loggedIn]) => {
115+
this.isAuthInProgress$.next(false);
116+
110117
// Redirect to target route after callback processing
111118
this.router.navigateByUrl(targetRoute);
112119
});

02-Calling-an-API/src/app/auth/auth.guard.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@angular/router';
88
import { Observable } from 'rxjs';
99
import { AuthService } from './auth.service';
10-
import { tap } from 'rxjs/operators';
10+
import { skipWhile, switchMap, tap } from 'rxjs/operators';
1111

1212
@Injectable({
1313
providedIn: 'root'
@@ -20,13 +20,14 @@ export class AuthGuard implements CanActivate {
2020
next: ActivatedRouteSnapshot,
2121
state: RouterStateSnapshot
2222
): Observable<boolean> | Promise<boolean|UrlTree> | boolean {
23-
return this.auth.isAuthenticated$.pipe(
23+
return this.auth.isAuthInProgress$.pipe(
24+
skipWhile((inProgress: boolean) => inProgress),
25+
switchMap(inProgress => this.auth.isAuthenticated$),
2426
tap(loggedIn => {
2527
if (!loggedIn) {
2628
this.auth.login(state.url);
2729
}
28-
})
29-
);
30+
}));
3031
}
3132

3233
}

02-Calling-an-API/src/app/auth/auth.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class AuthService {
3636
// Create subject and public observable of user profile data
3737
private userProfileSubject$ = new BehaviorSubject<any>(null);
3838
userProfile$ = this.userProfileSubject$.asObservable();
39+
40+
isAuthInProgress$ = new BehaviorSubject<boolean>(false);
41+
3942
// Create a local property for login status
4043
loggedIn: boolean = null;
4144

@@ -98,6 +101,8 @@ export class AuthService {
98101
// Call when app reloads after user logs in with Auth0
99102
const params = window.location.search;
100103
if (params.includes('code=') && params.includes('state=')) {
104+
this.isAuthInProgress$.next(true);
105+
101106
let targetRoute: string; // Path to redirect to after login processsed
102107
const authComplete$ = this.handleRedirectCallback$.pipe(
103108
// Have client, now call method to handle auth callback redirect
@@ -116,6 +121,8 @@ export class AuthService {
116121
// Subscribe to authentication completion observable
117122
// Response will be an array of user and login status
118123
authComplete$.subscribe(([user, loggedIn]) => {
124+
this.isAuthInProgress$.next(false);
125+
119126
// Redirect to target route after callback processing
120127
this.router.navigate([targetRoute]);
121128
});

0 commit comments

Comments
 (0)