简体   繁体   中英

How to get logged in user data as a object from "angularx-social-login"?

I tried to sign in function with google, facebook authentication using angularx-social-login package, it's also works and display the user data like name,profile picture. I want to get that user data as object to store globally.

here's my function

signInWithGoogle(): void {
try{
  this.authService.signIn(GoogleLoginProvider.PROVIDER_ID);
  localStorage.setItem('userObject', JSON.stringify(this.user));
  console.log(this.user)
  //this.router.navigateByUrl('/profile');
}
catch(err){
   console.log(err)
}}

Here's ngOnInit

ngOnInit(): void {
this.authService.authState.subscribe((user) => {
  this.user = user;
  this.loggedIn = (user != null);
}); }

You need to store the user information in ngOnInit function after subscribe. Try with below code,

ngOnInit(): void {
    this.authService.authState.subscribe((user) => {
        localStorage.setItem('userObject', JSON.stringify(user));
        this.user = user;
        this.loggedIn = (user != null);
    });
}

signInWithGoogle(): void {
    try{
        this.authService.signIn(GoogleLoginProvider.PROVIDER_ID);
    }catch(err) {
        console.log(err)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM