Fix authguard
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
This commit is contained in:
parent
baf8767a91
commit
b70ec77328
@ -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 {
|
||||
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());
|
||||
return true;
|
||||
resolve(true);
|
||||
}).catch(() => {
|
||||
resolve(false);
|
||||
this.oauthService.initCodeFlow(url);
|
||||
return false;
|
||||
}
|
||||
)
|
||||
})
|
||||
} else {
|
||||
resolve(false);
|
||||
this.oauthService.initCodeFlow(url);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
this.store.dispatch(new appCommonActions.InitUser());
|
||||
return true;
|
||||
return(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user