diff --git a/src/app/accounts/login.component.ts b/src/app/accounts/login.component.ts index 2e239f92..7c572e57 100644 --- a/src/app/accounts/login.component.ts +++ b/src/app/accounts/login.component.ts @@ -29,6 +29,7 @@ export class LoginComponent extends BaseLoginComponent { analytics: Angulartics2, toasterService: ToasterService, i18nService: I18nService, private componentFactoryResolver: ComponentFactoryResolver) { super(authService, router, analytics, toasterService, i18nService); + super.successRoute = '/dashboard'; } settings() { diff --git a/src/app/accounts/two-factor.component.ts b/src/app/accounts/two-factor.component.ts index ba00aa90..c281175d 100644 --- a/src/app/accounts/two-factor.component.ts +++ b/src/app/accounts/two-factor.component.ts @@ -38,6 +38,7 @@ export class TwoFactorComponent extends BaseTwoFactorComponent { private componentFactoryResolver: ComponentFactoryResolver) { super(authService, router, analytics, toasterService, i18nService, apiService, platformUtilsService, window, environmentService); + super.successRoute = '/dashboard'; } anotherMethod() { diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index e996d879..cd597bdb 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -4,15 +4,20 @@ import { Routes, } from '@angular/router'; -import { AuthGuardService } from 'jslib/angular/services/auth-guard.service'; +import { AuthGuardService } from './services/auth-guard.service'; +import { LaunchGuardService } from './services/launch-guard.service'; import { LoginComponent } from './accounts/login.component'; import { TwoFactorComponent } from './accounts/two-factor.component'; import { DashboardComponent } from './dashboard/dashboard.component'; const routes: Routes = [ - { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, - { path: 'login', component: LoginComponent }, + { path: '', redirectTo: '/login', pathMatch: 'full' }, + { + path: 'login', + component: LoginComponent, + canActivate: [LaunchGuardService], + }, { path: '2fa', component: TwoFactorComponent }, { path: 'dashboard', diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8cfeec2b..064af722 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -6,7 +6,7 @@ import { Angulartics2Module } from 'angulartics2'; import { Angulartics2GoogleAnalytics } from 'angulartics2/ga'; import { AppRoutingModule } from './app-routing.module'; -import { ServicesModule } from './services.module'; +import { ServicesModule } from './services/services.module'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; diff --git a/src/app/services/auth-guard.service.ts b/src/app/services/auth-guard.service.ts new file mode 100644 index 00000000..f890b991 --- /dev/null +++ b/src/app/services/auth-guard.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; +import { + CanActivate, + Router, +} from '@angular/router'; + +import { MessagingService } from 'jslib/abstractions/messaging.service'; +import { UserService } from 'jslib/abstractions/user.service'; + +@Injectable() +export class AuthGuardService implements CanActivate { + constructor(private userService: UserService, private router: Router, + private messagingService: MessagingService) { } + + async canActivate() { + const isAuthed = await this.userService.isAuthenticated(); + if (!isAuthed) { + this.messagingService.send('logout'); + return false; + } + + return true; + } +} diff --git a/src/app/services/launch-guard.service.ts b/src/app/services/launch-guard.service.ts new file mode 100644 index 00000000..5dd9cb63 --- /dev/null +++ b/src/app/services/launch-guard.service.ts @@ -0,0 +1,22 @@ +import { Injectable } from '@angular/core'; +import { + CanActivate, + Router, +} from '@angular/router'; + +import { UserService } from 'jslib/abstractions/user.service'; + +@Injectable() +export class LaunchGuardService implements CanActivate { + constructor(private userService: UserService, private router: Router) { } + + async canActivate() { + const isAuthed = await this.userService.isAuthenticated(); + if (!isAuthed) { + return true; + } + + this.router.navigate(['/dashboard']); + return false; + } +} diff --git a/src/app/services.module.ts b/src/app/services/services.module.ts similarity index 94% rename from src/app/services.module.ts rename to src/app/services/services.module.ts index 42d17dcd..92194d63 100644 --- a/src/app/services.module.ts +++ b/src/app/services/services.module.ts @@ -12,9 +12,11 @@ import { ElectronRendererSecureStorageService } from 'jslib/electron/services/el import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service'; import { isDev } from 'jslib/electron/utils'; -import { I18nService } from '../services/i18n.service'; +import { AuthGuardService } from './auth-guard.service'; +import { LaunchGuardService } from './launch-guard.service'; + +import { I18nService } from '../../services/i18n.service'; -import { AuthGuardService } from 'jslib/angular/services/auth-guard.service'; import { BroadcasterService } from 'jslib/angular/services/broadcaster.service'; import { ValidationService } from 'jslib/angular/services/validation.service'; @@ -31,6 +33,7 @@ import { NodeCryptoFunctionService } from 'jslib/services/nodeCryptoFunction.ser import { StateService } from 'jslib/services/state.service'; import { TokenService } from 'jslib/services/token.service'; import { UserService } from 'jslib/services/user.service'; +import { WebCryptoFunctionService } from 'jslib/services/webCryptoFunction.service'; import { ApiService as ApiServiceAbstraction } from 'jslib/abstractions/api.service'; import { AppIdService as AppIdServiceAbstraction } from 'jslib/abstractions/appId.service'; @@ -55,7 +58,8 @@ const broadcasterService = new BroadcasterService(); const messagingService = new ElectronRendererMessagingService(broadcasterService); const storageService: StorageServiceAbstraction = new ElectronStorageService(); const secureStorageService: StorageServiceAbstraction = new ElectronRendererSecureStorageService(); -const cryptoFunctionService: CryptoFunctionServiceAbstraction = new NodeCryptoFunctionService(); +const cryptoFunctionService: CryptoFunctionServiceAbstraction = new WebCryptoFunctionService(window, + platformUtilsService); const cryptoService = new CryptoService(storageService, secureStorageService, cryptoFunctionService); const appIdService = new AppIdService(storageService); const tokenService = new TokenService(storageService); @@ -104,6 +108,7 @@ export function initFactory(): Function { providers: [ ValidationService, AuthGuardService, + LaunchGuardService, { provide: AuthServiceAbstraction, useValue: authService }, { provide: EnvironmentServiceAbstraction, useValue: environmentService }, { provide: TokenServiceAbstraction, useValue: tokenService },