auth guard service to routes

This commit is contained in:
Kyle Spearrin 2018-02-08 11:12:25 -05:00
parent 2030100631
commit 6452cfaf7f
3 changed files with 38 additions and 2 deletions

View File

@ -4,6 +4,8 @@ import {
Routes,
} from '@angular/router';
import { AuthGuardService } from './services/auth-guard.service';
import { HintComponent } from './accounts/hint.component';
import { LoginComponent } from './accounts/login.component';
import { RegisterComponent } from './accounts/register.component';
@ -11,11 +13,15 @@ import { TwoFactorComponent } from './accounts/two-factor.component';
import { VaultComponent } from './vault/vault.component';
const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '', redirectTo: '/vault', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: '2fa', component: TwoFactorComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'vault', component: VaultComponent },
{
path: 'vault',
component: VaultComponent,
canActivate: [AuthGuardService]
},
{ path: 'hint', component: HintComponent },
];

View File

@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
Router,
} from '@angular/router';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { UserService } from 'jslib/abstractions/user.service';
@Injectable()
export class AuthGuardService implements CanActivate {
constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router) { }
canActivate(): boolean {
if (!this.userService.isAuthenticated()) {
this.router.navigate(['vault']);
return false;
}
if (this.cryptoService.getKey() == null) {
this.router.navigate(['lock']);
return false;
}
return true;
}
}

View File

@ -13,6 +13,7 @@ import { DesktopSecureStorageService } from '../../services/desktopSecureStorage
import { DesktopStorageService } from '../../services/desktopStorage.service';
import { I18nService } from '../../services/i18n.service';
import { AuthGuardService } from './auth-guard.service';
import { ValidationService } from './validation.service';
import { Analytics } from 'jslib/misc/analytics';
@ -119,6 +120,7 @@ function initFactory(i18n: I18nService, platformUtils: DesktopPlatformUtilsServi
declarations: [],
providers: [
ValidationService,
AuthGuardService,
{ provide: AuthServiceAbstraction, useValue: authService },
{ provide: CipherServiceAbstraction, useValue: cipherService },
{ provide: FolderServiceAbstraction, useValue: folderService },
@ -133,6 +135,7 @@ function initFactory(i18n: I18nService, platformUtils: DesktopPlatformUtilsServi
{ provide: PasswordGenerationServiceAbstraction, useValue: passwordGenerationService },
{ provide: ApiServiceAbstraction, useValue: apiService },
{ provide: SyncServiceAbstraction, useValue: syncService },
{ provide: UserServiceAbstraction, useValue: userService },
{
provide: APP_INITIALIZER,
useFactory: initFactory,