mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-07 09:31:31 +01:00
auth guard service to routes
This commit is contained in:
parent
2030100631
commit
6452cfaf7f
@ -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 },
|
||||
];
|
||||
|
||||
|
27
src/app/services/auth-guard.service.ts
Normal file
27
src/app/services/auth-guard.service.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user