Fix authguard
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good Details

feature/MinimizeSolution
Willem Dantuma 2020-05-11 20:39:00 +02:00
parent baf8767a91
commit b70ec77328
1 changed files with 22 additions and 20 deletions

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;
}
} else {
this.store.dispatch(new appCommonActions.InitUser());
return true;
}
this.store.dispatch(new appCommonActions.InitUser());
return(true);
}
});
}
}