I have getNewToken
and hasTheUserTokenOrPermission
methods. The first one returns token and set the user data after that the second one checks after WE SET THAT user data - if he has permission to see something in the guards.
So i need to wait for the first result and after that to check if there is permission. I need to return either true
or false
from the canActivateGuard.
So this can be solved on this way with the promise pattern
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } from '@angular/router';
import { AuthStoreService } from '@core/services/auth-store.service';
import { Observable, of, Subject } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
import { LoginService } from 'src/api/controllers/Login';
import { OAuthService } from 'angular-oauth2-oidc';
/**
* HasPermissionGuard - Used for checking the user permission for the core routing
* in order to check if the user has permission to see the page
*/
@Injectable()
export class HasPermissionGuard implements CanActivate {
constructor(
private loginService: LoginService,
private oauthService: OAuthService,
public authStore: AuthStoreService,
private router: Router
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> | Observable<any> {
return this.getNewToken().toPromise().then(data => {
return this.hasTheUserTokenOrPermission(route);
}).then((shouldWeProvideAccess: boolean) => {
console.log('shouldWeProvideAccess', shouldWeProvideAccess);
if (shouldWeProvideAccess) {
return shouldWeProvideAccess;
} else {
this.router.navigate(['/403']);
return shouldWeProvideAccess;
}
})
}
getNewToken(): Observable<any> {
return this.loginService.token({ token: this.oauthService.getAccessToken() }).pipe(
tap(response => {
this.authStore.setData(response);
},
), catchError(err => {
this.router.navigate(['/405']);
return Observable.throw(err);
}));
}
hasTheUserTokenOrPermission(route): boolean {
if (!this.authStore.hasUserInfo()) {
return true;
}
if (this.authStore.hasPermission(route.data.permission)) {
return true;
}
return false;
}
}
but when i try to solve this on observable pattern
i have problems
return this.getNewToken().subscribe(data => {
let shouldWeProvideAccess = this.hasTheUserTokenOrPermission(route);
console.log('shouldWeProvideAccess', shouldWeProvideAccess);
if (shouldWeProvideAccess) {
return shouldWeProvideAccess;
} else {
this.router.navigate(['/403']);
return shouldWeProvideAccess;
}
})
i get
Type 'Subscription' is not assignable to type 'Promise<any> | Observable<any>'.
error.
How can i return result from here ?