I'm trying to integrate with an identity provider (Keycloak in my case) in my Angular project. I'm using "angular-oauth2-oidc" library for that purpose.
So, I'm able to redirect a user from my home page to the login page of IDP on a button click, and it redirects me back to my original page after a successful login. So far so good, but my problem is that after the login process, session information including token is not set to my browser's session storage. If I repeat the process (calling the login function again), it then sets them correctly.
Here are the codes that I've worked on so far;
auth.service.ts
constructor(private oauthService: OAuthService) {}
authConfig: AuthConfig = {
issuer: environment.keycloak.issuerAddress,
redirectUri: window.location.origin + '/home',
clientId: environment.keycloak.clientId,
scope: environment.keycloak.scope,
responseType: environment.keycloak.responseType,
disableAtHashCheck: environment.keycloak.disableAtHashCheck,
showDebugInformation: environment.keycloak.showDebugInformation,
}
login(): Promise<any> {
return new Promise<void>((resolveFn, rejectFn) => {
this.initLogin().then(() => {
resolveFn();
}).catch(function(e){
rejectFn(e);
});
});
}
private initLogin(): Promise<any> {
return new Promise<void>((resolveFn, rejectFn) => {
this.oauthService.configure(this.authConfig);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
if (this.oauthService.hasValidAccessToken()) {
this.oauthService.setupAutomaticSilentRefresh();
resolveFn();
}else {
this.oauthService.initCodeFlow();
resolveFn();
}
}).catch(function(e){
rejectFn("Identity Provider is not reachable!");
});
});
}
home.component.ts
login(): void {
this.authService.login().then(() => {
//
}).catch((e) =>{
//
});
}
In summary, what I'm trying to do is that;
- When the user clicks on login button, configure the oauthService and try to login.
- If there's already a valid access token, then just setup a silent refresh and return.
- If there's no valid access token, then start the code flow and redirect to IDP's login page.
- If login try is failed with an exception, tell the user that IDP is not available.
Note: If I instead do the oauthService configuration in the constructor, and only call the oauthService.initCodeFlow() method when the user wants to login, then it works fine. The reason I'm not configuring it in constructor is that I want to be able to tell the user that IDP is not available when the user clicks on login button.