Use route guards from jslib to reduce duplication

This commit is contained in:
Thomas Rittson 2021-06-07 09:50:03 +10:00
parent 1e998094df
commit 1a143ef7c7
4 changed files with 51 additions and 25 deletions

View File

@ -7,9 +7,10 @@ import {
} from '@angular/router'; } from '@angular/router';
import { AuthGuardService } from 'jslib/angular/services/auth-guard.service'; import { AuthGuardService } from 'jslib/angular/services/auth-guard.service';
import { LockGuardService } from 'jslib/angular/services/lock-guard.service';
import { LaunchGuardService } from './services/launch-guard.service'; import { LockGuardService } from './services/lock-guard.service';
import { NotPrivateGuardService } from './services/not-private-guard.service';
import { UnauthGuardService } from './services/unauth-guard.service';
import { EnvironmentComponent } from './accounts/environment.component'; import { EnvironmentComponent } from './accounts/environment.component';
import { HintComponent } from './accounts/hint.component'; import { HintComponent } from './accounts/hint.component';
@ -65,13 +66,13 @@ const routes: Routes = [
{ {
path: 'home', path: 'home',
component: HomeComponent, component: HomeComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: 'home' }, data: { state: 'home' },
}, },
{ {
path: 'login', path: 'login',
component: LoginComponent, component: LoginComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: 'login' }, data: { state: 'login' },
}, },
{ {
@ -83,19 +84,19 @@ const routes: Routes = [
{ {
path: '2fa', path: '2fa',
component: TwoFactorComponent, component: TwoFactorComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: '2fa' }, data: { state: '2fa' },
}, },
{ {
path: '2fa-options', path: '2fa-options',
component: TwoFactorOptionsComponent, component: TwoFactorOptionsComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: '2fa-options' }, data: { state: '2fa-options' },
}, },
{ {
path: 'sso', path: 'sso',
component: SsoComponent, component: SsoComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: 'sso' }, data: { state: 'sso' },
}, },
{ {
@ -106,19 +107,19 @@ const routes: Routes = [
{ {
path: 'register', path: 'register',
component: RegisterComponent, component: RegisterComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: 'register' }, data: { state: 'register' },
}, },
{ {
path: 'hint', path: 'hint',
component: HintComponent, component: HintComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: 'hint' }, data: { state: 'hint' },
}, },
{ {
path: 'environment', path: 'environment',
component: EnvironmentComponent, component: EnvironmentComponent,
canActivate: [LaunchGuardService], canActivate: [NotPrivateGuardService, UnauthGuardService],
data: { state: 'environment' }, data: { state: 'environment' },
}, },
{ {

View File

@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
Router,
} from '@angular/router';
import { UserService } from 'jslib-common/abstractions/user.service';
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
@Injectable()
export class LockGuardService extends BaseLockGuardService {
constructor(private vaultTimeoutService: VaultTimeoutService, private userService: UserService,
private router: Router) {
super(vaultTimeoutService, userService, router);
}
protected landingPage = '/tabs/current';
}

View File

@ -10,7 +10,7 @@ import { UserService } from 'jslib/abstractions/user.service';
import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service'; import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service';
@Injectable() @Injectable()
export class LaunchGuardService implements CanActivate { export class NotPrivateGuardService implements CanActivate {
constructor(private vaultTimeoutService: VaultTimeoutService, private userService: UserService, constructor(private vaultTimeoutService: VaultTimeoutService, private userService: UserService,
private router: Router) { } private router: Router) { }
@ -19,19 +19,6 @@ export class LaunchGuardService implements CanActivate {
this.router.navigate(['private-mode']); this.router.navigate(['private-mode']);
return false; return false;
} }
return true;
const isAuthed = await this.userService.isAuthenticated();
if (!isAuthed) {
return true;
}
const locked = await this.vaultTimeoutService.isLocked();
if (locked) {
this.router.navigate(['lock']);
} else {
this.router.navigate(['tabs/current']);
}
return false;
} }
} }

View File

@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import {
Router,
} from '@angular/router';
import { UserService } from 'jslib-common/abstractions/user.service';
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
import { UnauthGuardService as BaseUnauthGuardService } from 'jslib/angular/services/unauth-guard.service';
@Injectable()
export class UnauthGuardService extends BaseUnauthGuardService {
constructor(private vaultTimeoutService: VaultTimeoutService, private userService: UserService,
private router: Router) {
super(vaultTimeoutService, userService, router);
}
protected landingPage: '/tabs/current';
}