mirror of
https://github.com/bitwarden/browser
synced 2025-12-13 23:03:32 +00:00
Add unauthGuard and lockGuard to prevent unintended navigation (#351)
* Lift web repo unauthGuardService up to jslib * Add lockGuard (requires vault to be locked) * Fix linting
This commit is contained in:
29
src/angular/services/lock-guard.service.ts
Normal file
29
src/angular/services/lock-guard.service.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
Router,
|
||||||
|
} from '@angular/router';
|
||||||
|
|
||||||
|
import { UserService } from '../../abstractions/user.service';
|
||||||
|
import { VaultTimeoutService } from '../../abstractions/vaultTimeout.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class LockGuardService implements CanActivate {
|
||||||
|
constructor(private vaultTimeoutService: VaultTimeoutService, private userService: UserService,
|
||||||
|
private router: Router) { }
|
||||||
|
|
||||||
|
async canActivate() {
|
||||||
|
const locked = await this.vaultTimeoutService.isLocked();
|
||||||
|
if (!locked) {
|
||||||
|
const isAuthed = await this.userService.isAuthenticated();
|
||||||
|
if (!isAuthed) {
|
||||||
|
this.router.navigate(['login']);
|
||||||
|
} else {
|
||||||
|
this.router.navigate(['vault']);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/angular/services/unauth-guard.service.ts
Normal file
29
src/angular/services/unauth-guard.service.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
Router,
|
||||||
|
} from '@angular/router';
|
||||||
|
|
||||||
|
import { UserService } from '../../abstractions/user.service';
|
||||||
|
import { VaultTimeoutService } from '../../abstractions/vaultTimeout.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UnauthGuardService implements CanActivate {
|
||||||
|
constructor(private vaultTimeoutService: VaultTimeoutService, private userService: UserService,
|
||||||
|
private router: Router) { }
|
||||||
|
|
||||||
|
async canActivate() {
|
||||||
|
const isAuthed = await this.userService.isAuthenticated();
|
||||||
|
if (isAuthed) {
|
||||||
|
const locked = await this.vaultTimeoutService.isLocked();
|
||||||
|
if (locked) {
|
||||||
|
this.router.navigate(['lock']);
|
||||||
|
} else {
|
||||||
|
this.router.navigate(['vault']);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user