Created
May 28, 2018 09:10
-
-
Save moderateepheezy/653b60f6243d84626b53ea96d94b7769 to your computer and use it in GitHub Desktop.
Login with facebook Angular 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@angular/core"; | |
import { Component } from "@angular/core"; | |
import { Http } from "@angular/http"; | |
import { facebookVariables } from "../../getFBSDK"; | |
import { Router } from "@angular/router"; | |
import { LocalStorage, JSONSchema } from "@ngx-pwa/local-storage"; | |
import { User } from "../models/User"; | |
import {GoogleAuthService} from "ng-gapi/lib/GoogleAuthService"; | |
import GoogleUser = gapi.auth2.GoogleUser; | |
import GoogleAuth = gapi.auth2.GoogleAuth; | |
declare var window: any; | |
declare var FB: any; | |
@Injectable() | |
export class UserService { | |
public static SESSION_STORAGE_KEY: string = 'accessToken'; | |
private userData: User = { | |
name: "", | |
token: "" | |
}; | |
constructor(private router: Router, protected localStorage: LocalStorage, private googleAuth: GoogleAuthService) { | |
(function(d, s, id) { | |
var js, | |
fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) { | |
return; | |
} | |
js = d.createElement(s); | |
js.id = id; | |
js.src = "//connect.facebook.net/en_US/sdk.js"; | |
fjs.parentNode.insertBefore(js, fjs); | |
})(document, "script", "facebook-jssdk"); | |
window.fbAsyncInit = () => { | |
FB.init(facebookVariables); | |
FB.AppEvents.logPageView(); | |
FB.Event.subscribe("auth.statusChange", response => { | |
if (response.status === "connected") { | |
// use the response variable to get any information about the user and to see the tokens about the users session | |
} | |
}); | |
}; | |
} | |
public getToken(): string { | |
let token: string = sessionStorage.getItem(UserService.SESSION_STORAGE_KEY); | |
if (!token) { | |
throw new Error("no token set , authentication required"); | |
} | |
return sessionStorage.getItem(UserService.SESSION_STORAGE_KEY); | |
} | |
public signIn(): void { | |
this.googleAuth.getAuth() | |
.subscribe((auth) => { | |
auth.signIn().then(res => this.signInSuccessHandler(res)); | |
}); | |
} | |
private signInSuccessHandler(res: User) { | |
this.userData = res; | |
// sessionStorage.setItem( | |
// UserService.SESSION_STORAGE_KEY, res.getAuthResponse().access_token | |
// ); | |
} | |
private getUserData(): Promise<any> { | |
console.log("Welcome! Fetching your information.... "); | |
return new Promise(resolve => { | |
FB.api("/me", (response) => { | |
this.userData.name = response.name; | |
this.localStorage.setItem('user', this.userData).subscribe(() => {}); | |
resolve(this.userData); | |
}); | |
}); | |
} | |
public loginWithFacebook() { | |
return new Promise(resolve => { | |
FB.getLoginStatus(async response => { | |
if (response.status == "connected") { | |
this.userData.token = response.authResponse.accessToken; | |
const data = await this.getUserData(); | |
resolve(this.userData); | |
// this.getUserData().then(data => resolve(this.facebookData)); | |
} else { | |
FB.login( | |
async loginResponse => { | |
this.userData.token = loginResponse.authResponse.accessToken; | |
const data = await this.getUserData(); | |
resolve(this.userData); | |
// this.getUserData().then(data => resolve(this.facebookData)); | |
}, | |
{ scope: "public_profile,email" } | |
); | |
} | |
}); | |
}); | |
} | |
public getFacebookData() { | |
const schema: JSONSchema = { | |
properties: { | |
name: { type: 'string' }, | |
token: { type: 'string' } | |
}, | |
required: ['name', 'token'] | |
}; | |
return new Promise(resolve => { | |
this.localStorage.getItem<User>('user', { schema }).subscribe((user) => { | |
// Called if data is valid or null | |
console.log(user); | |
resolve(user); | |
}, (error) => { | |
console.log(error); | |
resolve(error); | |
// Called if data is invalid | |
}); | |
}); | |
} | |
public logoutFromFacebook() { | |
FB.logout((response) => { | |
console.log('Logged Out'); | |
console.log(response); | |
this.router.navigate(['']) | |
}); | |
} | |
public parseFBToSDK() { | |
if (window.FB) { | |
window.FB.XFBML.parse(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment