In my angular application I am trying to send a jwt token as a header in order to authorize my app for requests. But I am getting an error 500 because I am sending the jwt token in a wrong format, this is how I am sending right now:
Bearer {"id_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTY1MDMxMTg0MH0.zWxESmFkM_nE8LEqIfFwSb-nEG593qaYnS1IFjd9qdYbOZJmMSXirfW3S68lQ0PBJcNop-OGtB6JJjtNJprDIQ"}
What I want is the following:
Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTY1MDMxMTg0MH0.zWxESmFkM_nE8LEqIfFwSb-nEG593qaYnS1IFjd9qdYbOZJmMSXirfW3S68lQ0PBJcNop-OGtB6JJjtNJprDIQ
I tried the following but I am getting an undefined can not read property of trim error:
token.split(" ")[1].trim()
could someone help me out and tell me what I am doing wrong? This is my interceptor where I am trying to send the header:
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor() {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!request.url || (request.url.startsWith('http') && !(environment.rootUrl && request.url.startsWith(environment.rootUrl)))) {
return next.handle(request);
}
const token =
localStorage.getItem('token') ||
sessionStorage.getItem('token');
if (token) {
console.log(token)
request = request.clone({
setHeaders: {
Authorization: 'Bearer ' + token.split(" ")[1].trim()
},
});
}
return next.handle(request);
}
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/Xk3CzAp
Comments
Post a Comment