import { BaseLoginProvider } from '../classes/base-login-provider.class' ; import { SocialUser } from '../classes/social-user.class' ; import { LoginOptions } from '../interfaces/login-options.interface' ; declare const FB ; export class FacebookLoginProvider extends BaseLoginProvider { public static readonly PROVIDER_ID = 'facebook' ; constructor ( private clientId , private opt = { scope : 'email,public_profile' } , private locale = 'en_US' ) { super ( ) ; } initialize ( ) { return new Promise ( ( resolve , reject ) => { this . loadScript ( FacebookLoginProvider . PROVIDER_ID , `//connect.facebook.net/${ this . locale }/sdk.js` , ( ) => { FB . init ( { appId : this . clientId , autoLogAppEvents : true , cookie : true , xfbml : true , version : 'v2.9' , } ) ; FB . getLoginStatus ( function ( response ) { if ( response . status === 'connected' ) { const authResponse = response . authResponse ; FB . api ( '/me?fields=name,email,picture,first_name,last_name' , ( fbUser ) => { const user = new SocialUser ( ) ; user . socialUid = fbUser . id ; user . name = fbUser . name ; user . email = fbUser . email ; user . photoUrl = 'https://graph.facebook.com/' + fbUser . id + '/picture?type=normal' ; user . firstName = fbUser . first_name ; user . lastName = fbUser . last_name ; user . accessToken = authResponse . accessToken ; resolve ( user ) ; } ) ; } } ) ; } ) ; } ) ; } signIn ( ) { return new Promise ( ( resolve , reject ) => { FB . login ( ( response ) => { if ( response . authResponse ) { const authResponse = response . authResponse ; FB . api ( '/me?fields=name,email,picture,first_name,last_name' , ( fbUser ) => { const user = new SocialUser ( ) ; user . socialUid = fbUser . id ; user . name = fbUser . name ; user . email = fbUser . email ; user . photoUrl = 'https://graph.facebook.com/' + fbUser . id + '/picture?type=normal' ; user . firstName = fbUser . first_name ; user . lastName = fbUser . last_name ; user . accessToken = authResponse . accessToken ; resolve ( user ) ; } ) ; } } , this . opt ) ; } ) ; } signOut ( ) { return new Promise ( ( resolve , reject ) => { FB . logout ( ( response ) => { resolve ( ) ; } ) ; } ) ; } }