Saroj Kumar Sahoo
Saroj Kumar Sahoo

Reputation: 83

how to disable the back button after logout in angular 7

I have one login page as part of my application . I want to disable my back button after logout successfully so that the user can not go back.

Upvotes: 6

Views: 22129

Answers (4)

Md Faraz
Md Faraz

Reputation: 317

You can try below, It worked for me.

https://www.npmjs.com/package/angular-disable-browser-back-button

import { NgModule } from '@angular/core';
import { BackButtonDisableModule } from 'angular-disable-browser-back-button';
 
@NgModule({
  ...
  imports: [
    ...
    BackButtonDisableModule.forRoot()
  ],
  ...
})
export class AppModule {}

Upvotes: 0

Saroj Kumar Sahoo
Saroj Kumar Sahoo

Reputation: 83

yes Auth gurad is been useful here .

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth-service.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(private authService: AuthService) {

  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this.authService.getisLoggedIn();
  }
}

Add the auth guard in app.module.ts

{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] }
{ path: 'logout', component: LogoutComponent , canActivate:[AuthGuard]}

So that after logout we can disable the back button

Add the auth guard as part of app.module.ts providers: [AuthGuard]

Upvotes: 0

Sadid Khan
Sadid Khan

Reputation: 1985

You can add a guard to watch and decide if user can access the page or not rather than disabling the browser's events. CanActivate is the saviour

CanActivate (Interface)

Interface that a class can implement to be a guard deciding if a route can be activated. If all guards return true, navigation will continue. If any guard returns false, navigation will be cancelled. From official documentation of Angular

Here I am adding some code that I am currently using. Hope it helps to understand how to implement one.

import { CanActivate, ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';

import { IdentityService } from './identity.service';

@Injectable()
export class LoginGuard implements CanActivate {

    constructor(private identityService: IdentityService, private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        if (this.identityService.isLoggedIn()) { // determine if the uder is logged in from this method.
            return true;
        }
        this.router.navigate(['/login']);
        return false;
    }
}

add this LoginGuard class into provider in you app.module.ts

providers: [{ provide: LoginGuard, useClass: LoginGuard }]

then add canActive in the route to guard it.

{
    path: 'dashboard',
    component: DashboadComponent,
    canActivate: [LoginGuard]
}

Upvotes: 8

Nguyen Phong Thien
Nguyen Phong Thien

Reputation: 3387

You cannot disable it, you can only prevent it. Just check the login status and redirect it to a specific page if they're redirected to a page that needed to be logged in

Upvotes: 1

Related Questions