I have an interceptor that adds the access token to every request. The only problem is, the only way I can get this access token is by using a function that returns a Promise:
async getToken(): Promise<string> {
I HAVE to use this function to get the tokens. There is no other way. I have tried making the authInterceptor function async to no avail.
So how do I use this in a function based interceptor like so?
import { Inject, inject } from '@angular/core';
import { AuthService } from './auth.service';
import { HttpInterceptorFn } from '@angular/common/http';
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const authService = Inject(AuthService)
const authToken = await authService.getToken(); // how to do this??
console.log(authToken);
// Clone the request and add the authorization header
const authReq = req.clone({
setHeaders: {
Authorization: `Bearer ${authToken}`
}
});
// Pass the cloned request with the updated header to the next handler
return next(authReq);
};
Please help.
const a = from(await authService.getToken());a.subscribe(res => authToken = res)