कोणीय 4 में अंतर्निहित प्रवाह कॉलबैक को संभालने का सबसे अच्छा तरीका क्या है? मैं चाहता हूं कि गार्ड तब तक प्रतीक्षा करे जब तक उपयोगकर्ता को टोकन के साथ रीडायरेक्ट नहीं किया जाता है और गार्ड को सही या गलत लौटने से पहले संग्रहीत किया जाता है, मुझे टोकन की जांच करने के लिए रीडायरेक्ट करने से पहले कुछ सेकंड के लिए एक्सेस अस्वीकृत मार्ग मिल रहा है। क्या मैं कर रहा हूं उससे ऑथगार्ड को संभालने का कोई बेहतर तरीका है इसलिए मुझे प्रमाणीकरण पूर्ण होने से पहले प्रवेश अस्वीकार नहीं किया गया है?कोणीय 4 लागू प्रवाह कॉलबैक और राउटर गार्ड
मैं राउटर गार्ड को रीडायरेक्ट के लिए कैसे इंतजार करूं?
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;
}
ऐसा लगता है कि यह अच्छा काम कर रहा है लेकिन मेरे पास एक और मुद्दा है जो मुझे लगता है कि शायद वे पर्यवेक्षकों का उपयोग करने से संबंधित हैं। जब मैं obserable के अंदर एक इंजेक्शन सेवा का उपयोग करने की कोशिश करता हूं जैसे is.sgerLoggedIn विधि के अंदर this.logger.log यह इस विधि पर observer.js में त्रुटि फेंकता है: Observable.prototype।_try सदस्यता लें = फ़ंक्शन (सिंक) { कोशिश करें { इसे वापस करें। सदस्यता छोड़ें (सिंक); } पकड़ो (गलती) { sink.syncErrorThrown = true; sink.syncErrorValue = err; सिंक। आतंक (गलती); } }; – Fab
यह कन्स्ट्रक्टर में पंजीकृत है और अवलोकन के बाहर काम करता है लेकिन – Fab
के अंदर नहीं, वास्तव में अगर मैं इंजेक्शन सेवा का उपयोग करने का प्रयास करता हूं तो मुझे वास्तव में केवल .reduce के अंदर त्रुटियां मिल रही हैं। – Fab