My Angular HTTP interceptor is trying to add Authorization headers to a request, but when the code runs, the resulting request does not behave as expected. The method changes from POST to OPTIONS and an error message is generated:
Access to XMLHttpRequest at 'http://localhost/authorization/oauth/token' from origin 'http://localhost:4200' has been blocked by CORS policy. The response to the preflight request fails access control checks and does not return an HTTP ok status.
@Injectable()
export class DlkmInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.url === authConfig.tokenEndpoint && request.method === 'POST') {
let authData = window.btoa(authConfig.clientId + ':' + authConfig.dummyClientSecret);
request = request.clone({
setHeaders: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + authData
}
});
return next.handle(request);
} else {
request = request.clone({});
return next.handle(request)
}
}
}