Skip to content
Permalink
Browse files

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
  • Loading branch information
Adam Fanello
Adam Fanello committed Dec 27, 2019
1 parent b39bbe1 commit 8de758e5dcc8c0f6ef7ce729aa872acf0d96abc2
Showing with 19 additions and 6 deletions.
  1. +19 −6 src/app/services/user.service.ts
@@ -1,15 +1,16 @@
import {SyncService} from "./sync.service";

const imageToURI = require('image-to-data-uri');
import {Injectable} from "@angular/core";
import {Injectable, NgZone} from '@angular/core';
import {UserSettings} from "../models/user-settings";
import {CollectionService} from "./collection.service";
import {BehaviorSubject} from "rxjs";
import {AuthUser} from "../models/auth-user";
import {PersistenceService, PersistenceException} from "./persistence.service";
import {ToastrService} from "ngx-toastr";
import {AmplifyService} from 'aws-amplify-angular';
import {AuthClass} from 'aws-amplify';
import {AuthClass, Hub} from 'aws-amplify';
import {Router} from '@angular/router';

const STORAGE_KEY = 'user';

@@ -41,15 +42,27 @@ export class UserService {
private readonly amplifySvc: AmplifyService,
private persistSvc: PersistenceService,
private syncSvc: SyncService,
private toastr: ToastrService
private toastr: ToastrService,
private readonly router: Router,
private ngZone: NgZone,
) {
// Listen for federated sign-in and redirect to account page to complete the process
Hub.listen('auth', ({ payload: { event, data } }) => {
if (event === 'signIn') {
this.ngZone.run(() =>
this.router.navigate(['/home/account'])
).then();
}
});

// Listen for any sign-in/sign-out
this.amplifySvc.authStateChange$
.subscribe(authState => {
if (authState.state === 'signedIn') {
this.onCognitoUserAuth(authState.user);
this.onCognitoUserAuth(authState.user).then();
}
else {
this.onCognitoUserAuth(undefined);
else if (authState.state === 'signedOut') {
this.onCognitoUserAuth(undefined).then();
}
});

0 comments on commit 8de758e

Please sign in to comment.
You can’t perform that action at this time.