From 62234b3f9d39f4084a7337b73535b4788aefecdf Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Fri, 31 Jan 2020 11:24:45 +0100 Subject: [PATCH] Refactor authentication flow --- .../src/fm/components/app/app.component.html | 4 +- .../src/fm/services/auth-guard.service.ts | 127 +++++++++--------- .../src/fm/shared/app.config.factory.ts | 24 +--- 3 files changed, 73 insertions(+), 82 deletions(-) diff --git a/projects/common/src/fm/components/app/app.component.html b/projects/common/src/fm/components/app/app.component.html index 812b89a..a12ed78 100644 --- a/projects/common/src/fm/components/app/app.component.html +++ b/projects/common/src/fm/components/app/app.component.html @@ -1,7 +1,7 @@
diff --git a/projects/common/src/fm/services/auth-guard.service.ts b/projects/common/src/fm/services/auth-guard.service.ts index 3799812..a400d8e 100644 --- a/projects/common/src/fm/services/auth-guard.service.ts +++ b/projects/common/src/fm/services/auth-guard.service.ts @@ -1,60 +1,67 @@ -import { Injectable } from '@angular/core'; -import { - CanActivate, Router, CanLoad, Route, CanActivateChild , - ActivatedRouteSnapshot, - RouterStateSnapshot -} from '@angular/router'; - -import { Store } from '@ngrx/store'; -import { OAuthService } from 'angular-oauth2-oidc'; - - -import * as appCommonReducer from '../reducers/app-common.reducer' -import * as appCommonActions from '../actions/app-common.actions'; - - -@Injectable({ - providedIn: 'root', -}) -export class AuthGuard implements CanActivate, CanLoad, CanActivateChild { - - private loginDispatched = false; - private initialized = false; - constructor(private oauthService: OAuthService, private router: Router, private store: Store ) { } - - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { - let url: string = state.url; - - return this.checkLogin(url); - } - - canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { - let url: string = state.url; - - return this.checkLogin(url); - } - - canLoad(route: Route): boolean { - return this.checkLogin(route.path); - } - - checkLogin(url: string): boolean { - if (!this.oauthService.hasValidAccessToken()) { - if (!this.loginDispatched) { - this.oauthService.silentRefresh().then(info => { - this.router.navigateByUrl(url); - }).catch(error => { - this.loginDispatched = true; - this.store.dispatch(new appCommonActions.Login(url)); - }) - } - return false; - } else { - if (!this.initialized) { - this.initialized = true; - this.store.dispatch(new appCommonActions.InitUser()); - } - return true; - } - } -} +import { Injectable } from '@angular/core'; +import { + CanActivate, Router, CanLoad, Route, CanActivateChild , + ActivatedRouteSnapshot, + RouterStateSnapshot +} from '@angular/router'; + +import { Store } from '@ngrx/store'; +import { OAuthService } from 'angular-oauth2-oidc'; + + +import * as appCommonReducer from '../reducers/app-common.reducer' +import * as appCommonActions from '../actions/app-common.actions'; + + +@Injectable({ + providedIn: 'root', +}) +export class AuthGuard implements CanActivate, CanLoad, CanActivateChild { + + private loginDispatched = false; + private initialized = false; + constructor(private oauthService: OAuthService, private router: Router, private store: Store ) { } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { + let url: string = state.url; + + return this.checkLogin(url); + } + + canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { + let url: string = state.url; + + return this.checkLogin(url); + } + + canLoad(route: Route): boolean { + return this.checkLogin(route.path); + } + + checkLogin(url: string): boolean { + if (!this.oauthService.hasValidAccessToken()) { + debugger; + this.oauthService.responseType + if(this.oauthService.responseType == "code") + if(this.oauthService.getRefreshToken() != null ) { + this.oauthService.refreshToken().then(() => { + this.store.dispatch(new appCommonActions.InitUser()); + return true; + }).catch(() => { + this.oauthService.initCodeFlow(url); + return false; + } + ) + } else { + this.oauthService.initCodeFlow(url); + return false; + } + else + this.oauthService.initImplicitFlow(url); + return false; + } else { + this.store.dispatch(new appCommonActions.InitUser()); + return true; + } + } +} diff --git a/projects/common/src/fm/shared/app.config.factory.ts b/projects/common/src/fm/shared/app.config.factory.ts index 5bee96e..a824c10 100644 --- a/projects/common/src/fm/shared/app.config.factory.ts +++ b/projects/common/src/fm/shared/app.config.factory.ts @@ -30,32 +30,16 @@ export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthS }; oauthService.setupAutomaticSilentRefresh(); let location = injector.get(Location); - var urlPath =location.path(); + let router = injector.get(Router); + let urlPath = location.path(); oauthService.loadDiscoveryDocument().then(() => { oauthService.tryLogin({ onTokenReceived: (info) => { urlPath = info.state; } }).then(() => { - let router = injector.get(Router); - if (!oauthService.hasValidAccessToken()) { - if(appConfig.getConfig("grantType") == "code") - if(oauthService.getRefreshToken() != null ) { - oauthService.refreshToken().then(() => { - router.navigateByUrl(urlPath); - }).catch(() => { - oauthService.initCodeFlow(urlPath); - } - ) - } else { - oauthService.initCodeFlow(urlPath); - } - else - oauthService.initImplicitFlow(urlPath); - } else { - router.navigateByUrl(urlPath); - } - }); + router.navigateByUrl(urlPath); + }); }) }); }