Fix authguard
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Willem Dantuma 2020-05-11 20:39:00 +02:00
parent baf8767a91
commit b70ec77328

View File

@ -5,6 +5,7 @@ import {
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';
@ -20,40 +21,41 @@ export class AuthGuard implements CanActivate, CanLoad, CanActivateChild {
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): Promise<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): Promise<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): Promise<boolean> {
return this.checkLogin(route.path); return this.checkLogin(route.path);
} }
checkLogin(url: string): boolean { checkLogin(url: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
if (!this.oauthService.hasValidAccessToken()) { if (!this.oauthService.hasValidAccessToken()) {
if(this.oauthService.getRefreshToken() != null ) { if(this.oauthService.getRefreshToken() != null ) {
this.oauthService.refreshToken().then(() => { this.oauthService.refreshToken().then(() => {
this.store.dispatch(new appCommonActions.InitUser()); this.store.dispatch(new appCommonActions.InitUser());
return true; resolve(true);
}).catch(() => { }).catch(() => {
resolve(false);
this.oauthService.initCodeFlow(url); this.oauthService.initCodeFlow(url);
return false; })
}
)
} else { } else {
resolve(false);
this.oauthService.initCodeFlow(url); this.oauthService.initCodeFlow(url);
return false;
} }
} else { } else {
this.store.dispatch(new appCommonActions.InitUser()); this.store.dispatch(new appCommonActions.InitUser());
return true; return(true);
} }
});
} }
} }