You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 29, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Jul 29, 2026. It is now read-only.
Logged in user will not be detected if refreshing in AuthGuarded route #2774
isLoggedIn(callback?) {
let is = !!this.currentUser._id;
//at this time is continues to be undefined
safeCb(callback)(is);
return Promise.resolve(is);
}
AuthGuard Cancels Navigation
This part of the constructor finishes running and get the user too late.
Implemented the following solution, will submit a PR if in agreement with strategy. @Awk34
auth.service.ts:
constructor(private http: HttpClient, private userService: UserService) {
this.http = http;
this.UserService = userService;
//Not calling this as it will get done the first time isLoggedIn() is called
//TODO: Delete this comment block. Currently commented for clarity.
/*
if(localStorage.getItem('id_token')) {
this.UserService.get().toPromise()
.then((user: User) => {
this.currentUser = user;
})
.catch(err => {
console.log(err);
localStorage.removeItem('id_token');
});
}
*/
}
isLoggedIn(callback?) {
let is = !!this.currentUser._id;
if(is || !localStorage.getItem('id_token')){
//Got the user locally, or there is no id_token stored
safeCb(callback)(is);
return Promise.resolve(is);
}else{
//User not yet loaded locally, but id_token is present
return this.UserService.get().toPromise()
.then((user: User) => {
this.currentUser = user;
return !!this.currentUser._id;
})
.catch(err => {
console.log(err);
localStorage.removeItem('id_token');
return false;
});
}
}
auth-guard.service.ts:
//Change can activate to return a Promise
canActivate(): Promise<boolean> {
return this.authService.isLoggedIn();
}
I had solved this in my implementation by editing canActivate. Either route seems to do well, I preferred to keep them separate, but am open to either.
canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
// this will be passed from the route config
// on the data property
const expectedRole = route.data.expectedRole;
let promise = new Promise<boolean>((resolve,reject) => {
this.authService.isLoggedIn().then(is => {
resolve(this.authService.hasRole(this.authService.currentUser.role,expectedRole));
});
});
return promise;
}
Activity
blindstuff commentedon Nov 12, 2018
Implemented the following solution, will submit a PR if in agreement with strategy. @Awk34
auth.service.ts:
auth-guard.service.ts:
koraysels commentedon Nov 14, 2018
Definitely an improvement, this works pretty good for me. Thanks!
albert-92 commentedon Dec 10, 2018
Cool solution @blindstuff (#2774 (comment))
How about taking it even a little bit further like:
auth.service.ts:
auth-guard.service.ts:
and for example in the admin module
admin.module.ts:
blindstuff commentedon Dec 19, 2018
Good idea @albert-92
I had solved this in my implementation by editing canActivate. Either route seems to do well, I preferred to keep them separate, but am open to either.