I'm building an app with login.
The user can login and a token is returned to the front end.
The problem is occurring when, I'm sending the token back to the server to be verified.
I'm using a HttpInterceptor
to send the Authorization header to the server.
When I log in the console:
console.log(cloned.headers.get("Authorization"))
The header is displayed fine, however, when logged in the express middleware
console.log(req.headers.authorization);
I get undefined
Also below are the request headers from the network tab, as you can see, no Authorization
Interceptor:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor() { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const id_token = localStorage.getItem("id_token");
if (id_token) {
const cloned = req.clone({
headers: req.headers.set("Authorization", "Bearer " + id_token)
});
// console.log(cloned)
console.log(cloned.headers.get("Authorization"))
return next.handle(cloned);
}
else {
return next.handle(req);
}
}
}
express:
router.use('/edit', function(req, res, next) {
console.log(req.headers.authorization);
const checkIfAuthenticated = expressJwt({
secret: RSA_PUBLIC_KEY
});
if (checkIfAuthenticated) {
return res.status(401).json({
title: 'Not Authorised',
error: 'You are not authorised'
});
}
})