mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-05 23:53:21 +00:00
routing with guards
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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';
|
||||
|
||||
24
src/app/services/auth-guard.service.ts
Normal file
24
src/app/services/auth-guard.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
22
src/app/services/launch-guard.service.ts
Normal file
22
src/app/services/launch-guard.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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 },
|
||||
Reference in New Issue
Block a user