mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 07:43:35 +00:00
Move to libs
This commit is contained in:
42
libs/angular/src/guards/auth.guard.ts
Normal file
42
libs/angular/src/guards/auth.guard.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from "@angular/router";
|
||||
|
||||
import { AuthService } from "jslib-common/abstractions/auth.service";
|
||||
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
|
||||
import { MessagingService } from "jslib-common/abstractions/messaging.service";
|
||||
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private authService: AuthService,
|
||||
private router: Router,
|
||||
private messagingService: MessagingService,
|
||||
private keyConnectorService: KeyConnectorService
|
||||
) {}
|
||||
|
||||
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
||||
const authStatus = await this.authService.getAuthStatus();
|
||||
|
||||
if (authStatus === AuthenticationStatus.LoggedOut) {
|
||||
this.messagingService.send("authBlocked", { url: routerState.url });
|
||||
return false;
|
||||
}
|
||||
|
||||
if (authStatus === AuthenticationStatus.Locked) {
|
||||
if (routerState != null) {
|
||||
this.messagingService.send("lockedUrl", { url: routerState.url });
|
||||
}
|
||||
return this.router.createUrlTree(["lock"], { queryParams: { promptBiometric: true } });
|
||||
}
|
||||
|
||||
if (
|
||||
!routerState.url.includes("remove-password") &&
|
||||
(await this.keyConnectorService.getConvertAccountRequired())
|
||||
) {
|
||||
return this.router.createUrlTree(["/remove-password"]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
25
libs/angular/src/guards/lock.guard.ts
Normal file
25
libs/angular/src/guards/lock.guard.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { CanActivate, Router } from "@angular/router";
|
||||
|
||||
import { AuthService } from "jslib-common/abstractions/auth.service";
|
||||
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
||||
|
||||
@Injectable()
|
||||
export class LockGuard implements CanActivate {
|
||||
protected homepage = "vault";
|
||||
protected loginpage = "login";
|
||||
constructor(private authService: AuthService, private router: Router) {}
|
||||
|
||||
async canActivate() {
|
||||
const authStatus = await this.authService.getAuthStatus();
|
||||
|
||||
if (authStatus === AuthenticationStatus.Locked) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const redirectUrl =
|
||||
authStatus === AuthenticationStatus.LoggedOut ? this.loginpage : this.homepage;
|
||||
|
||||
return this.router.createUrlTree([redirectUrl]);
|
||||
}
|
||||
}
|
||||
25
libs/angular/src/guards/unauth.guard.ts
Normal file
25
libs/angular/src/guards/unauth.guard.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { CanActivate, Router } from "@angular/router";
|
||||
|
||||
import { AuthService } from "jslib-common/abstractions/auth.service";
|
||||
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
||||
|
||||
@Injectable()
|
||||
export class UnauthGuard implements CanActivate {
|
||||
protected homepage = "vault";
|
||||
constructor(private authService: AuthService, private router: Router) {}
|
||||
|
||||
async canActivate() {
|
||||
const authStatus = await this.authService.getAuthStatus();
|
||||
|
||||
if (authStatus === AuthenticationStatus.LoggedOut) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (authStatus === AuthenticationStatus.Locked) {
|
||||
return this.router.createUrlTree(["lock"]);
|
||||
}
|
||||
|
||||
return this.router.createUrlTree([this.homepage]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user