Cách tốt nhất để xử lý dòng chảy ẩn Gọi lại trong Angular 4 là gì? Tôi muốn Guard đợi cho đến khi người dùng được chuyển hướng trở lại với token và nó được lưu trước khi Guard trả về true hoặc false, tôi sẽ nhận được Route Denied trong vài giây trước khi tôi được chuyển hướng trở lại để kiểm tra token. Có cách nào tốt hơn để xử lý các AuthGuard hơn những gì tôi đang làm vì vậy tôi không nhận được truy cập bị từ chối trước khi xác thực hoàn thành?Angular 4 Implicit Flow Callback và Router Guard
Làm cách nào để bảo vệ bộ định tuyến đợi chuyển hướng?
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;
}
Điều đó dường như làm việc tốt nhưng tôi có một vấn đề khác tôi đoán có thể liên quan đến việc sử dụng Observables. Khi tôi cố gắng sử dụng dịch vụ được tiêm bên trong Obserable như this.logger.log bên trong phương thức isUserLoggedIn, nó ném lỗi trong observer.js vào phương thức này: Observable.prototype._trySubscribe = function (sink) { thử { trả lại this._subscribe (sink); } bắt (err) { sink.syncErrorThrown = true; sink.syncErrorValue = err; sink.error (err); } }; – Fab
Nó được đăng ký trong hàm khởi tạo và hoạt động bên ngoài quan sát nhưng không phải bên trong – Fab
Tôi thực sự chỉ gặp lỗi bên trong .reduce nếu tôi cố gắng sử dụng dịch vụ được tiêm. – Fab