I want to create a login system. I have a Spring Boot Api with a JWT-Token for security written with this tutorial. On my Front-End I use React JS and Axios for requests.
This is my axios configuration:
import axios from 'axios';
const api = axios.create({
baseURL:'http://localhost:8080',
headers: {'Access-Control-Allow-Origin': '*'}
});
export default api;
And this is the login function:
loginClickHandler = () => {
const data = {
"username":this.state.username,
"password":this.state.password
};
api.post("/login",data)
.then(response => {
console.log(response);
if(response.data !== null) {
this.setState({
loggedIn:true,
userData:response.data
});
}else{
this.showMessage(2,response.data.error)
}
})
.catch(error => {
this.showMessage(2,error);
});
};
The login function itself and everything else works fine, but I do not get the Authorization Token displayed in the browser as a header. This is the console.log(response)
(only headers):
headers:
cache-control: "no-cache, no-store, max-age=0, must-revalidate"
content-length: "0"
expires: "0"
pragma: "no-cache"
However when I use Postman for the request, I get 14 headers and one of them is the Authorization header with the token in it as expected.
Now my question is, how I can get the header displayed in my browser so I can store it for later requests.
Thank you
EDIT:
I just found that the Authorization Header gets displayed in the Network tab of the browser but not in the response.
SOLUTION:
I had to add this line in my JWTAuthenticationFilter successfulAuthentication
method:
res.addHeader("Access-Control-Expose-Headers",HEADER_STRING);
as @Jack Chen and @Sarthak Aggarwal suggested