import { Injectable } from '@angular/core' ; import Auth , { CognitoHostedUIIdentityProvider } from '@aws-amplify/auth' ; import { Hub , ICredentials } from '@aws-amplify/core' ; import { Subject , Observable } from 'rxjs' ; import { CognitoUser } from 'amazon-cognito-identity-js' ; export interface NewUser { email : string ; phone : string ; password : string ; firstName : string ; lastName : string ; } ; @ Injectable ( { providedIn : 'root' } ) export class AuthService { public loggedIn : boolean ; private _authState : Subject < CognitoUser | any > = new Subject < CognitoUser | any > ( ) ; authState : Observable < CognitoUser | any > = this . _authState . asObservable ( ) ; public static SIGN_IN = 'signIn' ; public static SIGN_OUT = 'signOut' ; public static FACEBOOK = CognitoHostedUIIdentityProvider . Facebook ; public static GOOGLE = CognitoHostedUIIdentityProvider . Google ; constructor ( ) { Hub . listen ( 'auth' , ( data ) => { const { channel , payload } = data ; if ( channel === 'auth' ) { this . _authState . next ( payload . event ) ; } } ) ; } signUp ( user ) { return Auth . signUp ( { "username" : user . email , "password" : user . password , "attributes" : { "email" : user . email , "given_name" : user . firstName , "family_name" : user . lastName , "phone_number" : user . phone } } ) ; } signIn ( username , password ) { return new Promise ( ( resolve , reject ) => { Auth . signIn ( username , password ) . then ( ( user ) => { this . loggedIn = true ; resolve ( user ) ; } ) . catch ( ( error ) => reject ( error ) ) ; } ) ; } signOut ( ) { return Auth . signOut ( ) . then ( ( ) => this . loggedIn = false ) ; } socialSignIn ( provider ) { return Auth . federatedSignIn ( { 'provider' : provider } ) ; } }