Angular 4 How to redirect a page after logging out?

I know that an answer was given earlier, but I am not satisfied with the answers.

My problem is simple, the user wants to log out.

If it is on the page that is required for login (installed in auth.guard.ts with auth.service.ts) and user logout, I want it to be redirected to the main one.

However , if it is on a secure page, I do not want to redirect it.

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

location.reload() 

What is my option?

+5
source share
3 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.' } } 
+6
source

you can redirect the user when he leaves home like this.

 import { Router } from '@angular/router'; @Injectable() export class AuthService { constructor( private _router: Router){} authSignOut(){ localStorage.removeItem('user'); this._router.navigate(['home']); } } 

About another, I'm a little confused ... could you share your code?

+1
source

Please find the following redirection code hoping it helps

  import {Component} from '@angular/core'; import {Router} from '@angular/router'; export class LogoutComponant { title = 'Logout'; constructor( private router: Router, ) {} onLogout() { this.router.navigate(['/']) } } 
+1
source

Source: https://habr.com/ru/post/1270281/


All Articles