import { BaseLoginProvider } from '../classes/base-login-provider.class' ; import { SocialUser } from '../classes/social-user.class' ; import { LoginOptions } from '../interfaces/login-options.interface' ; declare const gapi ; export class GoogleLoginProvider extends BaseLoginProvider { public static readonly PROVIDER_ID : string = 'google' ; protected auth2 : any ; constructor ( private clientId , private opt = { scope : 'email' } ) { super ( ) ; } initialize ( ) { return new Promise ( ( resolve , reject ) => { this . loadScript ( GoogleLoginProvider . PROVIDER_ID , '//apis.google.com/js/platform.js' , ( ) => { gapi . load ( 'auth2' , ( ) => { this . auth2 = gapi . auth2 . init ( { ... this . opt , client_id : this . clientId , } ) ; this . auth2 . then ( ( ) => { if ( this . auth2 . isSignedIn . get ( ) ) { const user = new SocialUser ( ) ; const profile = this . auth2 . currentUser . get ( ) . getBasicProfile ( ) ; const token = this . auth2 . currentUser . get ( ) . getAuthResponse ( true ) . access_token ; const backendToken = this . auth2 . currentUser . get ( ) . getAuthResponse ( true ) . id_token ; user . socialUid = profile . getId ( ) ; user . name = profile . getName ( ) ; user . email = profile . getEmail ( ) ; user . photoUrl = profile . getImageUrl ( ) ; user . firstName = profile . getGivenName ( ) ; user . lastName = profile . getFamilyName ( ) ; user . accessToken = token ; user . idToken = backendToken ; resolve ( user ) ; } } ) ; } ) ; } ) ; } ) ; } signIn ( ) { return new Promise ( ( resolve , reject ) => { const promise = this . auth2 . signIn ( ) ; promise . then ( ( ) => { const user = new SocialUser ( ) ; const profile = this . auth2 . currentUser . get ( ) . getBasicProfile ( ) ; const token = this . auth2 . currentUser . get ( ) . getAuthResponse ( true ) . access_token ; const backendToken = this . auth2 . currentUser . get ( ) . getAuthResponse ( true ) . id_token ; user . socialUid = profile . getId ( ) ; user . name = profile . getName ( ) ; user . email = profile . getEmail ( ) ; user . photoUrl = profile . getImageUrl ( ) ; user . accessToken = token ; user . idToken = backendToken ; resolve ( user ) ; } ) ; } ) ; } signOut ( ) { return new Promise ( ( resolve , reject ) => { this . auth2 . signOut ( ) . then ( ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } }