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
} from '@angular/router';
import { Store } from '@ngrx/store';
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> ) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
let url: string = state.url;
return this.checkLogin(url);
}
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
let url: string = state.url;
return this.checkLogin(url);
}
canLoad(route: Route): boolean {
canLoad(route: Route): Promise<boolean> {
return this.checkLogin(route.path);
}
checkLogin(url: string): boolean {
if (!this.oauthService.hasValidAccessToken()) {
if(this.oauthService.getRefreshToken() != null ) {
this.oauthService.refreshToken().then(() => {
this.store.dispatch(new appCommonActions.InitUser());
return true;
}).catch(() => {
checkLogin(url: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
if (!this.oauthService.hasValidAccessToken()) {
if(this.oauthService.getRefreshToken() != null ) {
this.oauthService.refreshToken().then(() => {
this.store.dispatch(new appCommonActions.InitUser());
resolve(true);
}).catch(() => {
resolve(false);
this.oauthService.initCodeFlow(url);
})
} else {
resolve(false);
this.oauthService.initCodeFlow(url);
return false;
}
)
} else {
this.oauthService.initCodeFlow(url);
return false;
this.store.dispatch(new appCommonActions.InitUser());
return(true);
}
} else {
this.store.dispatch(new appCommonActions.InitUser());
return true;
}
});
}
}