|
1 | 1 | import {SyncService} from "./sync.service";
|
2 | 2 |
|
3 | 3 | const imageToURI = require('image-to-data-uri');
|
4 |
| -import {Injectable} from "@angular/core"; |
| 4 | +import {Injectable, NgZone} from '@angular/core'; |
5 | 5 | import {UserSettings} from "../models/user-settings";
|
6 | 6 | import {CollectionService} from "./collection.service";
|
7 | 7 | import {BehaviorSubject} from "rxjs";
|
8 | 8 | import {AuthUser} from "../models/auth-user";
|
9 | 9 | import {PersistenceService, PersistenceException} from "./persistence.service";
|
10 | 10 | import {ToastrService} from "ngx-toastr";
|
11 | 11 | 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'; |
13 | 14 |
|
14 | 15 | const STORAGE_KEY = 'user';
|
15 | 16 |
|
@@ -41,15 +42,27 @@ export class UserService {
|
41 | 42 | private readonly amplifySvc: AmplifyService,
|
42 | 43 | private persistSvc: PersistenceService,
|
43 | 44 | private syncSvc: SyncService,
|
44 |
| - private toastr: ToastrService |
| 45 | + private toastr: ToastrService, |
| 46 | + private readonly router: Router, |
| 47 | + private ngZone: NgZone, |
45 | 48 | ) {
|
| 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 |
46 | 59 | this.amplifySvc.authStateChange$
|
47 | 60 | .subscribe(authState => {
|
48 | 61 | if (authState.state === 'signedIn') {
|
49 |
| - this.onCognitoUserAuth(authState.user); |
| 62 | + this.onCognitoUserAuth(authState.user).then(); |
50 | 63 | }
|
51 |
| - else { |
52 |
| - this.onCognitoUserAuth(undefined); |
| 64 | + else if (authState.state === 'signedOut') { |
| 65 | + this.onCognitoUserAuth(undefined).then(); |
53 | 66 | }
|
54 | 67 | });
|
55 | 68 |
|
|
0 commit comments