I'm using npm package oidc-client
in an Angular 10 app with Identity Server 4.
What I'm finding is that when I reload a web page in the browser it always redirects to the home page and doens't remember the current angular route.
I've found that this is because when the page reloads the UserManager
tries to refresh the user object, but this is done asynchronously and therefore the AuthGuard
sees null
for the user
because it hasn't loaded yet.
Can this be fixed. How?
@Injectable({
providedIn: 'root'
})
export class AuthService {
private manager = new UserManager(getClientSettings(this.appSettingsService));
private user: User | null;
constructor(private appSettingsService: AppSettingsService) {
// This should wait until the user has acually loaded. Doing async just makes everything break.
this.manager.getUser().then(user => {
this.user = user;
});
}
login() {
return this.manager.signinRedirect();
}
async completeAuthentication() {
this.user = await this.manager.signinRedirectCallback();
}
isAuthenticated(): boolean {
// This needs to be made to wait until the user has loaded.
return this.user != null && !this.user.expired;
}
It seems to be impossible to change isAuthenticated(): boolean
to isAuthenticated(): Observable<boolean>
because it is needed in AuthGuard
where canActivate
must return boolean
and not Observable<boolean>
and so it's necessary to get rid of the Observable
.