HOw can I logout from google from another component using ng-angular-social-login

1.3k Views Asked by At

I am using angularx-social-login. I can sign in with Google in the loginComponent:

this.authService.signIn(GoogleLoginProvider.PROVIDER_ID).then(
  data => {
    this.socialUser = data;
  })

I have this structure:

app
 auth
  loginComponent
 shared
  menuComponent(here is the link logout)

When I try to logout from menuComponent appears an error: You are not logged In.

logout(): void {
      this.authSocialService.signOut()
        .then( res => console.log( res ) )
        .catch( err => console.log(err) )
}

My question is how can I logout from menuComponent of the session opened in the loginComponent?

Here is recreated the error in Stackblitz.

1

There are 1 best solutions below

6
Karimov On BEST ANSWER

To issues a logout it's simple just call the signOut() from the service in any of your components, but make sure to import SocialAuthService first. Here's an example.

import { SocialAuthService, GoogleLoginProvider, SocialUser } from 'angularx-social-login';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
 user: SocialUser;

     constructor(private authService: SocialAuthService) {}

     ngOnInit() {
       this.authService.authState.subscribe((user) => {
        this.user = user;
        console.log(user);
     });
   }

   signOut(): void {
    this.authService.signOut();
  }
    
    }