I used HttpInterceptor to set the "bearer token" heading to log in Angular, when I log in the server returns a token and I save it in a Response variable but I need a Request variable that is why I use the Intercept.
export class TokenInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) { }
intercept(req, next) {
const tokenizeReq = req.clone({
setHeaders: {
authorization: `Bearer ${this.authService.getToken()}`
}
});
return next.handle(tokenizeReq);
}
}
Image where token is Null in navigator i use Google Chrome
well when i print variables i found problem i set token when token doesn't exist,
image when print token in intercept
When i use setTimeout print token correct but i dont know how use in method intercept i have tried different ways to use this method but when I try to add it I don't know how to use the setTimeout in method intercept()
code when i use setTimeout for print token
export class TokenInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) { }
intercept(req, next) {
const tokenizeReq = req.clone({
setHeaders: {
authorization: `Bearer ${this.authService.getToken()}`
}
});
setTimeout(() => {
const token = this.authService.getToken();
console.log(token);
}, 100);
return next.handle(tokenizeReq);
}
}
how to use setTimeout in method intercept() i have tried save in variables but set numbers ( 6, 5 or 9 ) i dont know what to do.