简体   繁体   中英

How to disable users from accessing previous page by browser back button after logout in angular2?

I am trying not to allow users to go to previous page using browser back button after logout. I wish to show users a messge like "Please login to continue".in Angular 2

create a new file called authorization.guard.ts and add this

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

import {AppContextService} from './context';

@Injectable()
export class AuthorizationGuard implements CanActivate {
 constructor(
private appContextService:AppContextService
 ){}
  canActivate(
   next: ActivatedRouteSnapshot,
   state: RouterStateSnapshot): boolean {
  return this.appContextService.getAuthAdminLoggednIn();
 }
}

later in your main module import {AuthorizationGuard}

add this in your each router path

    {
    path: 'dashboard',
    canActivate:[AuthorizationGuard]
     },

Refer this files for complete authorization Refer this

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