Reputation: 2663
I am using oidc-client in my angular project. I have written the below code in app.component.ts to redirect user to home page if user object is not null. But the user object is always null even after successful login. I am using microsoft azure to authenticate.
app.component.ts
async ngOnInit() {
await this.authService.getUser().then(user => {
console.log(user) // always returns null even after login
if (user) {
this.router.navigate(['/home']);
}
});
}
login() {
this.authService.signinRedirect();
}
AuthenticationService.ts
export class AuthenticationService {
private userManager: UserManager;
constructor(settings: Settings) {
this.userManager = new UserManager(settings);
}
public signinRedirect() {
this.userManager.signinRedirect();
}
public getUser() {
return this.userManager.getUser();
}
}
I dont see any key:val pair in local/session storage named oidc.user.xxxxxxx after successful login.
Also, I crosschecked with some sample in-house applications. After a successful login, oidc api is triggering a call to token endpoint. In my case this end point is not being called.
What could be missing? What am I doing wrong?
Upvotes: 0
Views: 1821
Reputation: 2663
Registering a signin redirect callback in app.component.ts solved this issue.
async ngOnInit() {
if (window.location.href.includes('code')) { // without this if condition I am getting an error saying no state in response. not sure if there is a better way.
this.authService.signinRedirectCallback();
}
await this.authService.getUser().then(user => {
console.log(user) // always returns null even after login
if (user) {
this.router.navigate(['/home']);
}
});
}
Upvotes: 2