| @@ -31,12 +31,7 @@ export class FacebookLoginComponent implements OnInit { | |||||
| } | } | ||||
| ngAfterViewInit() { | ngAfterViewInit() { | ||||
| if (localStorage.getItem('FBUser') && localStorage.getItem('FBToken')) { | |||||
| this.user = JSON.parse(localStorage.getItem('FBUser')); | |||||
| this.token = localStorage.getItem('FBToken'); | |||||
| } else { | |||||
| this.setupFbLogin(); | |||||
| } | |||||
| this.setupFbLogin(); | |||||
| } | } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| @@ -48,9 +43,15 @@ export class FacebookLoginComponent implements OnInit { | |||||
| // Use the native implementation inside a real app! | // Use the native implementation inside a real app! | ||||
| const { FacebookLogin } = Plugins; | const { FacebookLogin } = Plugins; | ||||
| this.fbLogin = FacebookLogin; | this.fbLogin = FacebookLogin; | ||||
| if (localStorage.getItem('FBUser') && localStorage.getItem('FBToken')) { | |||||
| this.user = JSON.parse(localStorage.getItem('FBUser')); | |||||
| this.getCurrentToken(); | |||||
| } | |||||
| } else { | } else { | ||||
| this.hideAll = true; | this.hideAll = true; | ||||
| } | |||||
| } | |||||
| } | } | ||||
| async login() { | async login() { | ||||
| @@ -73,7 +74,7 @@ export class FacebookLoginComponent implements OnInit { | |||||
| } | } | ||||
| } | } | ||||
| async getCurrentToken() { | |||||
| async getCurrentToken() { | |||||
| const result = await this.fbLogin.getCurrentAccessToken(); | const result = await this.fbLogin.getCurrentAccessToken(); | ||||
| if (result.accessToken) { | if (result.accessToken) { | ||||
| @@ -91,12 +92,16 @@ export class FacebookLoginComponent implements OnInit { | |||||
| this.http.get(url).subscribe(res => { | this.http.get(url).subscribe(res => { | ||||
| this.user = res; | this.user = res; | ||||
| localStorage.setItem('FBUser', JSON.stringify(res)); | localStorage.setItem('FBUser', JSON.stringify(res)); | ||||
| }, err => { | |||||
| }, (err) => { | |||||
| console.log(JSON.stringify(err)); | |||||
| alert("Falied to fetch user data"); | alert("Falied to fetch user data"); | ||||
| }); | }); | ||||
| } | } | ||||
| async logout() { | async logout() { | ||||
| await this.fbLogin.logout().then(() => { | |||||
| console.log("Logged Out"); | |||||
| }, (err) => alert(JSON.stringify(err))); | |||||
| this.user = null; | this.user = null; | ||||
| this.token = null; | this.token = null; | ||||
| localStorage.removeItem('FBUser'); | localStorage.removeItem('FBUser'); | ||||