Jaki jest najlepszy sposób obsługi niejawnego przepływu oddzwaniania w Angular 4? Chcę, żeby Strażnik czekał, aż użytkownik zostanie przekierowany z powrotem z tokenem i jest przechowywany, zanim Strażnik zwróci wartość true lub false. Odbieram dostęp Odmowa dostępu na kilka sekund, zanim nastąpi przekierowanie z powrotem, aby sprawdzić token. Czy istnieje lepszy sposób na obsłużenie AuthGuarda niż to, co robię, więc nie otrzymuję odmowy dostępu przed zakończeniem uwierzytelniania?Ograniczenie wywołania i ostrzeżenia o routerach Angular 4 Implicit Flow
Jak sprawić, aby router czekał na przekierowanie?
AppComponent
ngOnInit() {
//if there is a hash then the user is being redirected from the AuthServer with url params
if (window.location.hash && !this.authService.isUserLoggedIn()) {
//check the url hash
this.authService.authorizeCallback();
}
else if (!this.authService.isUserLoggedIn()) {
//try to authorize user if they aren't login
this.authService.tryAuthorize();
}
}
AuthSerivce
tryAuthorize() {
//redirect to open id connect /authorize endpoint
window.location.href = this.authConfigService.getSignInEndpoint();
}
authorizeCallback() {
let hash = window.location.hash.substr(1);
let result: any = hash.split('&').reduce(function (result: any, item: string) {
let parts = item.split('=');
result[parts[0]] = parts[1];
return result;
}, {});
if (result.error && result.error == 'access_denied') {
this.navigationService.AccessDenied();
}
else {
this.validateToken(result);
}
}
isUserLoggedIn(): boolean {
let token = this.getAccessToken();
//check if there is a token
if(token === undefined || token === null || token.trim() === '')
{
//no token or token is expired;
return false;
}
return true;
}
getAccessToken(): string {
let token = <string>this.storageService.get(this.accessTokenKey);
if(token === undefined || token === null || token.trim() === '')
{
return '';
}
return token;
}
resetAuthToken() {
this.storageService.store(this.accessTokenKey, '');
}
validateToken(tokenResults: any) {
//TODO: add other validations
//reset the token
this.resetAuthToken();
if (tokenResults && tokenResults.access_token) {
//store the token
this.storageService.store(this.accessTokenKey, tokenResults.access_token);
//navigate to clear the query string parameters
this.navigationService.Home();
}
else {
//navigate to Access Denied
this.navigationService.AccessDenied();
}
}
}
AuthGuard
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot){
var hasAccess = this.authService.isUserLoggedIn();
if(!hasAccess)
{
this.naviationService.AccessDenied();
return false;
}
return true;
}
Wydaje się, że działa dobrze, ale mam inny problem, który prawdopodobnie może być związany z używaniem urządzeń Observables. Gdy próbuję użyć usługi iniekcyjnej wewnątrz Obserable takiej jak ta.logger.log w metodzie isUserLoggedIn, generuje on błąd w pliku observer.js na tej metodzie: Observable.prototype._trySubscribe = funkcja (sink) { spróbuj { return this._subscribe (sink); } catch (err) { sink.syncErrorThrown = true; sink.syncErrorValue = err; sink.error (err); } }; – Fab
Jest zarejestrowany w konstruktorze i działa poza obserwowalnym, ale nie wewnątrz – Fab
Naprawdę tylko dostaję błędy wewnątrz .reduce, jeśli próbuję użyć wstrzykniętej usługi. – Fab