Skip to content

Commit 8de758e

Browse files
author
Adam Fanello
committed
Part 7 - Add Google Auth Provider
- Detect federated sign-in and redirect user to account page. - This fixes bug of sign-in not being detected - landing on the account page triggers Auth.authStateChange$ - See aws-amplify/amplify-js#4621
1 parent b39bbe1 commit 8de758e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

Diff for: src/app/services/user.service.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import {SyncService} from "./sync.service";
22

33
const imageToURI = require('image-to-data-uri');
4-
import {Injectable} from "@angular/core";
4+
import {Injectable, NgZone} from '@angular/core';
55
import {UserSettings} from "../models/user-settings";
66
import {CollectionService} from "./collection.service";
77
import {BehaviorSubject} from "rxjs";
88
import {AuthUser} from "../models/auth-user";
99
import {PersistenceService, PersistenceException} from "./persistence.service";
1010
import {ToastrService} from "ngx-toastr";
1111
import {AmplifyService} from 'aws-amplify-angular';
12-
import {AuthClass} from 'aws-amplify';
12+
import {AuthClass, Hub} from 'aws-amplify';
13+
import {Router} from '@angular/router';
1314

1415
const STORAGE_KEY = 'user';
1516

@@ -41,15 +42,27 @@ export class UserService {
4142
private readonly amplifySvc: AmplifyService,
4243
private persistSvc: PersistenceService,
4344
private syncSvc: SyncService,
44-
private toastr: ToastrService
45+
private toastr: ToastrService,
46+
private readonly router: Router,
47+
private ngZone: NgZone,
4548
) {
49+
// Listen for federated sign-in and redirect to account page to complete the process
50+
Hub.listen('auth', ({ payload: { event, data } }) => {
51+
if (event === 'signIn') {
52+
this.ngZone.run(() =>
53+
this.router.navigate(['/home/account'])
54+
).then();
55+
}
56+
});
57+
58+
// Listen for any sign-in/sign-out
4659
this.amplifySvc.authStateChange$
4760
.subscribe(authState => {
4861
if (authState.state === 'signedIn') {
49-
this.onCognitoUserAuth(authState.user);
62+
this.onCognitoUserAuth(authState.user).then();
5063
}
51-
else {
52-
this.onCognitoUserAuth(undefined);
64+
else if (authState.state === 'signedOut') {
65+
this.onCognitoUserAuth(undefined).then();
5366
}
5467
});
5568

0 commit comments

Comments
 (0)