I'm working on a project that uses cognito as an auth service to secure a serverless rest API made using nodeJS. I've successfully closed the API for non-authenticated clients. I now need to automatically inject a token in the header whenever i make a request from my Angular client. What i tried is implementing an HttpInterceptor like this :
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
setHeaders: {
'Content-Type' : 'application/json; charset=utf-8',
'Accept' : 'application/json',
'Authorization': `${this.authService.userToken}`,
//#endregion
},
})
return next.handle(req);
}
This always worked fine for me when using the standard angular HttpClient to make requests. But now as i'm using API from aws-amplify package to make request to my API.Gateway, these requests can't get intercepted like this. Here's how i make requests :
import { API } from 'aws-amplify';
.
.
.
return API.get('apiName', '/users',{})
And these are not using the Angular HttpClient.
EDIT : Also in app.module.ts :
providers : [{
provide : HTTP_INTERCEPTORS,
useClass: HttpRequestInterceptor, --> My interceptor class
multi : true,
}]
Anyone got an idea of how i can intercept these request to my API Gateway to inject the token?
Have a nice one !