Skip to content

Commit

Permalink
Merge branch 'master' into migrate-tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Hobbs authored Sep 16, 2020
2 parents 6c746dc + c0de0dd commit b3f425a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
9 changes: 5 additions & 4 deletions 01-Login/src/app/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
import { tap } from 'rxjs/operators';
import { skipWhile, switchMap, tap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
Expand All @@ -20,13 +20,14 @@ export class AuthGuard implements CanActivate {
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean|UrlTree> | boolean {
return this.auth.isAuthenticated$.pipe(
return this.auth.isAuthInProgress$.pipe(
skipWhile((inProgress: boolean) => inProgress),
switchMap(inProgress => this.auth.isAuthenticated$),
tap(loggedIn => {
if (!loggedIn) {
this.auth.login(state.url);
}
})
);
}));
}

}
7 changes: 7 additions & 0 deletions 01-Login/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class AuthService {
// Create subject and public observable of user profile data
private userProfileSubject$ = new BehaviorSubject<any>(null);
userProfile$ = this.userProfileSubject$.asObservable();

isAuthInProgress$ = new BehaviorSubject<boolean>(false);

// Create a local property for login status
loggedIn: boolean = null;

Expand Down Expand Up @@ -89,6 +92,8 @@ export class AuthService {
// Call when app reloads after user logs in with Auth0
const params = window.location.search;
if (params.includes('code=') && params.includes('state=')) {
this.isAuthInProgress$.next(true);

let targetRoute: string; // Path to redirect to after login processsed
const authComplete$ = this.handleRedirectCallback$.pipe(
// Have client, now call method to handle auth callback redirect
Expand All @@ -107,6 +112,8 @@ export class AuthService {
// Subscribe to authentication completion observable
// Response will be an array of user and login status
authComplete$.subscribe(([user, loggedIn]) => {
this.isAuthInProgress$.next(false);

// Redirect to target route after callback processing
this.router.navigateByUrl(targetRoute);
});
Expand Down
9 changes: 5 additions & 4 deletions 02-Calling-an-API/src/app/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
import { tap } from 'rxjs/operators';
import { skipWhile, switchMap, tap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
Expand All @@ -20,13 +20,14 @@ export class AuthGuard implements CanActivate {
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean|UrlTree> | boolean {
return this.auth.isAuthenticated$.pipe(
return this.auth.isAuthInProgress$.pipe(
skipWhile((inProgress: boolean) => inProgress),
switchMap(inProgress => this.auth.isAuthenticated$),
tap(loggedIn => {
if (!loggedIn) {
this.auth.login(state.url);
}
})
);
}));
}

}
7 changes: 7 additions & 0 deletions 02-Calling-an-API/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class AuthService {
// Create subject and public observable of user profile data
private userProfileSubject$ = new BehaviorSubject<any>(null);
userProfile$ = this.userProfileSubject$.asObservable();

isAuthInProgress$ = new BehaviorSubject<boolean>(false);

// Create a local property for login status
loggedIn: boolean = null;

Expand Down Expand Up @@ -98,6 +101,8 @@ export class AuthService {
// Call when app reloads after user logs in with Auth0
const params = window.location.search;
if (params.includes('code=') && params.includes('state=')) {
this.isAuthInProgress$.next(true);

let targetRoute: string; // Path to redirect to after login processsed
const authComplete$ = this.handleRedirectCallback$.pipe(
// Have client, now call method to handle auth callback redirect
Expand All @@ -116,6 +121,8 @@ export class AuthService {
// Subscribe to authentication completion observable
// Response will be an array of user and login status
authComplete$.subscribe(([user, loggedIn]) => {
this.isAuthInProgress$.next(false);

// Redirect to target route after callback processing
this.router.navigate([targetRoute]);
});
Expand Down

0 comments on commit b3f425a

Please sign in to comment.