I've been attempting to set up OAuth2 in my Angular project for user login. I followed the documentation, but whenever I try to log in, it keeps showing an Unauthorized error and I'm not sure how to resolve it. Here are my configurations:
auth config
export const authConfig: AuthConfig = {
// Identity Provider URL
issuer: 'my api base URL',
// Redirect URL after login
redirectUri: window.location.origin + '/worklists',
clientId: 'oAuth2-login',
responseType: 'code',
scope: 'open worklists genstate',
};
app component
private configure() {
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin();
}
app module
OAuthModule.forRoot({
resourceServer: {
allowedUrls: ['my api base url'],
sendAccessToken: true
}
})
jwt interceptor
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.idToken = this.authService.getAccessToken() || '';
const authReq = req.clone({
headers: this.idToken
? req.headers
.set('Authorization', `Bearer ${this.idToken}`)
.set('Access-Control-Allow-Origin', '*')
: req.headers,
params: req.params
});
console.log(authReq, 'auth req');
return next.handle(authReq);
}
this is my login service
public login(payload: LoginPayload, rememberMe: boolean = false) {
return this.httpService.post('/oauth/authorize', payload).pipe(
first(),
map((resp: LoginResponse) => {
console.log(resp, 'response')
return this.sessionService.storeLoginResponse(resp, rememberMe);
})
);
}
It seems like there might be an issue with the access token or my configuration. Any help would be greatly appreciated. Thank you.