1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-10 21:33:20 +00:00

routing with guards

This commit is contained in:
Kyle Spearrin
2018-04-25 12:30:27 -04:00
parent cafb7da93e
commit 48bde677bc
7 changed files with 65 additions and 7 deletions

View File

@@ -29,6 +29,7 @@ export class LoginComponent extends BaseLoginComponent {
analytics: Angulartics2, toasterService: ToasterService, analytics: Angulartics2, toasterService: ToasterService,
i18nService: I18nService, private componentFactoryResolver: ComponentFactoryResolver) { i18nService: I18nService, private componentFactoryResolver: ComponentFactoryResolver) {
super(authService, router, analytics, toasterService, i18nService); super(authService, router, analytics, toasterService, i18nService);
super.successRoute = '/dashboard';
} }
settings() { settings() {

View File

@@ -38,6 +38,7 @@ export class TwoFactorComponent extends BaseTwoFactorComponent {
private componentFactoryResolver: ComponentFactoryResolver) { private componentFactoryResolver: ComponentFactoryResolver) {
super(authService, router, analytics, toasterService, i18nService, apiService, super(authService, router, analytics, toasterService, i18nService, apiService,
platformUtilsService, window, environmentService); platformUtilsService, window, environmentService);
super.successRoute = '/dashboard';
} }
anotherMethod() { anotherMethod() {

View File

@@ -4,15 +4,20 @@ import {
Routes, Routes,
} from '@angular/router'; } 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 { LoginComponent } from './accounts/login.component';
import { TwoFactorComponent } from './accounts/two-factor.component'; import { TwoFactorComponent } from './accounts/two-factor.component';
import { DashboardComponent } from './dashboard/dashboard.component'; import { DashboardComponent } from './dashboard/dashboard.component';
const routes: Routes = [ const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' }, { path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent }, {
path: 'login',
component: LoginComponent,
canActivate: [LaunchGuardService],
},
{ path: '2fa', component: TwoFactorComponent }, { path: '2fa', component: TwoFactorComponent },
{ {
path: 'dashboard', path: 'dashboard',

View File

@@ -6,7 +6,7 @@ import { Angulartics2Module } from 'angulartics2';
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga'; import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import { ServicesModule } from './services.module'; import { ServicesModule } from './services/services.module';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -12,9 +12,11 @@ import { ElectronRendererSecureStorageService } from 'jslib/electron/services/el
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service'; import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
import { isDev } from 'jslib/electron/utils'; 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 { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { ValidationService } from 'jslib/angular/services/validation.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 { StateService } from 'jslib/services/state.service';
import { TokenService } from 'jslib/services/token.service'; import { TokenService } from 'jslib/services/token.service';
import { UserService } from 'jslib/services/user.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 { ApiService as ApiServiceAbstraction } from 'jslib/abstractions/api.service';
import { AppIdService as AppIdServiceAbstraction } from 'jslib/abstractions/appId.service'; import { AppIdService as AppIdServiceAbstraction } from 'jslib/abstractions/appId.service';
@@ -55,7 +58,8 @@ const broadcasterService = new BroadcasterService();
const messagingService = new ElectronRendererMessagingService(broadcasterService); const messagingService = new ElectronRendererMessagingService(broadcasterService);
const storageService: StorageServiceAbstraction = new ElectronStorageService(); const storageService: StorageServiceAbstraction = new ElectronStorageService();
const secureStorageService: StorageServiceAbstraction = new ElectronRendererSecureStorageService(); 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 cryptoService = new CryptoService(storageService, secureStorageService, cryptoFunctionService);
const appIdService = new AppIdService(storageService); const appIdService = new AppIdService(storageService);
const tokenService = new TokenService(storageService); const tokenService = new TokenService(storageService);
@@ -104,6 +108,7 @@ export function initFactory(): Function {
providers: [ providers: [
ValidationService, ValidationService,
AuthGuardService, AuthGuardService,
LaunchGuardService,
{ provide: AuthServiceAbstraction, useValue: authService }, { provide: AuthServiceAbstraction, useValue: authService },
{ provide: EnvironmentServiceAbstraction, useValue: environmentService }, { provide: EnvironmentServiceAbstraction, useValue: environmentService },
{ provide: TokenServiceAbstraction, useValue: tokenService }, { provide: TokenServiceAbstraction, useValue: tokenService },