diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 6b8e25635b..a0f9d101ab 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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 }, ]; diff --git a/src/app/services/auth-guard.service.ts b/src/app/services/auth-guard.service.ts new file mode 100644 index 0000000000..35e884afa6 --- /dev/null +++ b/src/app/services/auth-guard.service.ts @@ -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; + } +} diff --git a/src/app/services/services.module.ts b/src/app/services/services.module.ts index 5a26b8a8ca..996113d043 100644 --- a/src/app/services/services.module.ts +++ b/src/app/services/services.module.ts @@ -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,