diff --git a/src/angular/services/lock-guard.service.ts b/src/angular/services/lock-guard.service.ts new file mode 100644 index 0000000000..9e318ab86e --- /dev/null +++ b/src/angular/services/lock-guard.service.ts @@ -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; + } +} diff --git a/src/angular/services/unauth-guard.service.ts b/src/angular/services/unauth-guard.service.ts new file mode 100644 index 0000000000..236837e350 --- /dev/null +++ b/src/angular/services/unauth-guard.service.ts @@ -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; + } +}