mirror of
https://github.com/bitwarden/jslib
synced 2025-12-21 10:43:23 +00:00
[EndUserVaultRefresh] Add base routing guard (#732)
* Add a base class for Angular routing guards * Update Guard naming convention
This commit is contained in:
47
angular/src/guards/auth.guard.ts
Normal file
47
angular/src/guards/auth.guard.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from "@angular/router";
|
||||
|
||||
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
|
||||
import { MessagingService } from "jslib-common/abstractions/messaging.service";
|
||||
import { StateService } from "jslib-common/abstractions/state.service";
|
||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||
|
||||
import { BaseGuard } from "./base.guard";
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard extends BaseGuard implements CanActivate {
|
||||
constructor(
|
||||
router: Router,
|
||||
private vaultTimeoutService: VaultTimeoutService,
|
||||
private messagingService: MessagingService,
|
||||
private keyConnectorService: KeyConnectorService,
|
||||
private stateService: StateService
|
||||
) {
|
||||
super(router);
|
||||
}
|
||||
|
||||
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
||||
const isAuthed = await this.stateService.getIsAuthenticated();
|
||||
if (!isAuthed) {
|
||||
this.messagingService.send("authBlocked");
|
||||
return false;
|
||||
}
|
||||
|
||||
const locked = await this.vaultTimeoutService.isLocked();
|
||||
if (locked) {
|
||||
if (routerState != null) {
|
||||
this.messagingService.send("lockedUrl", { url: routerState.url });
|
||||
}
|
||||
return this.redirect("lock", { queryParams: { promptBiometric: true } });
|
||||
}
|
||||
|
||||
if (
|
||||
!routerState.url.includes("remove-password") &&
|
||||
(await this.keyConnectorService.getConvertAccountRequired())
|
||||
) {
|
||||
return this.redirect("/remove-password");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user