Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 How to redirect page after logout?

Tags:

angular

I know this has been answered previously but I'm not satisfied with the answers given.

My problem is simple, the user want to logout.

If it's on a page that required to be login (set in auth.guard.ts with auth.service.ts) and the user logout I want him to be redirected to home.

However if he is on a safe page I don't want to redirected him.

I could use the AuthGuard but I don't want to reload the page, so no:

location.reload()

What's my option ?

like image 477
Frennetix Avatar asked Jul 27 '17 16:07

Frennetix


People also ask

How do I redirect after logout?

To redirect the user after they log out from a specific application, you must add the URL used in the returnTo parameter of the redirect URL to the Allowed Logout URLs list in the Settings tab of your Auth0 application that is associated with the CLIENT_ID parameter.


1 Answers

import { Router } from '@angular/router';
.....

constructor(private router:Router, authService: AuthService){}
  //toastMessagesService: ToastMessagesService){} if you have one
....

onLogOut(){
  authService.logOut(); 

  if (this.router.url==="/admin"){
        //this.toastMessagesService.show("Logged out"); // display a toast style message if you have one :)
        this.router.navigate(["/home"]); //for the case 'the user logout I want him to be redirected to home.'
  }
  else{
   // Stay here or do something
   // for the case 'However if he is on a safe page I don't want to redirected him.'
   }
 }
like image 77
Vega Avatar answered Nov 03 '22 00:11

Vega