Refactor authentication flow
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good

This commit is contained in:
Willem Dantuma 2020-01-31 11:24:45 +01:00
parent e53378a76b
commit 62234b3f9d
3 changed files with 73 additions and 82 deletions

View File

@ -1,7 +1,7 @@
<div class="app fullscreen" (click)="handleClick($event)" [ngClass]="{'fullscreen' :(fullScreen|async)}"> <div class="app fullscreen" (click)="handleClick($event)" [ngClass]="{'fullscreen' :(fullScreen|async)}">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary"> <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<button type="button" class="btn btn-light" (click)="handleToggleMenu($event)"><i class="fa fa-bars" aria-hidden="true"></i></button> <button type="button" class="btn btn-outline-light" (click)="handleToggleMenu($event)"><i class="fa fa-bars" aria-hidden="true"></i></button>
<router-outlet name="menu"></router-outlet> <router-outlet name="menu ml-4 float-right"></router-outlet>
</nav> </nav>
<div class="body"> <div class="body">
<router-outlet></router-outlet> <router-outlet></router-outlet>

View File

@ -1,60 +1,67 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { import {
CanActivate, Router, CanLoad, Route, CanActivateChild , CanActivate, Router, CanLoad, Route, CanActivateChild ,
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
RouterStateSnapshot RouterStateSnapshot
} from '@angular/router'; } from '@angular/router';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import * as appCommonReducer from '../reducers/app-common.reducer' import * as appCommonReducer from '../reducers/app-common.reducer'
import * as appCommonActions from '../actions/app-common.actions'; import * as appCommonActions from '../actions/app-common.actions';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AuthGuard implements CanActivate, CanLoad, CanActivateChild { export class AuthGuard implements CanActivate, CanLoad, CanActivateChild {
private loginDispatched = false; private loginDispatched = false;
private initialized = false; private initialized = false;
constructor(private oauthService: OAuthService, private router: Router, private store: Store<appCommonReducer.State> ) { } constructor(private oauthService: OAuthService, private router: Router, private store: Store<appCommonReducer.State> ) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url; let url: string = state.url;
return this.checkLogin(url); return this.checkLogin(url);
} }
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url; let url: string = state.url;
return this.checkLogin(url); return this.checkLogin(url);
} }
canLoad(route: Route): boolean { canLoad(route: Route): boolean {
return this.checkLogin(route.path); return this.checkLogin(route.path);
} }
checkLogin(url: string): boolean { checkLogin(url: string): boolean {
if (!this.oauthService.hasValidAccessToken()) { if (!this.oauthService.hasValidAccessToken()) {
if (!this.loginDispatched) { debugger;
this.oauthService.silentRefresh().then(info => { this.oauthService.responseType
this.router.navigateByUrl(url); if(this.oauthService.responseType == "code")
}).catch(error => { if(this.oauthService.getRefreshToken() != null ) {
this.loginDispatched = true; this.oauthService.refreshToken().then(() => {
this.store.dispatch(new appCommonActions.Login(url)); this.store.dispatch(new appCommonActions.InitUser());
}) return true;
} }).catch(() => {
return false; this.oauthService.initCodeFlow(url);
} else { return false;
if (!this.initialized) { }
this.initialized = true; )
this.store.dispatch(new appCommonActions.InitUser()); } else {
} this.oauthService.initCodeFlow(url);
return true; return false;
} }
} else
} this.oauthService.initImplicitFlow(url);
return false;
} else {
this.store.dispatch(new appCommonActions.InitUser());
return true;
}
}
}

View File

@ -30,32 +30,16 @@ export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthS
}; };
oauthService.setupAutomaticSilentRefresh(); oauthService.setupAutomaticSilentRefresh();
let location = injector.get(Location); let location = injector.get(Location);
var urlPath =location.path(); let router = injector.get(Router);
let urlPath = location.path();
oauthService.loadDiscoveryDocument().then(() => { oauthService.loadDiscoveryDocument().then(() => {
oauthService.tryLogin({ oauthService.tryLogin({
onTokenReceived: (info) => { onTokenReceived: (info) => {
urlPath = info.state; urlPath = info.state;
} }
}).then(() => { }).then(() => {
let router = injector.get(Router); router.navigateByUrl(urlPath);
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);
}
});
}) })
}); });
} }