I'm currently making a express --> react api. I can't set header with jwt token, I think I've tried any method I could find. Could it be related to CORS settings?
Redux action
return (dispatch) => {
return instance.post("/login", userData).then((res) => {
const token = res.data.accessToken;
localStorage.setItem("token", token);
instance.interceptors.request.use(
(config) => {
const token = localStorage.getItem("token");
const auth = token ? `Bearer ${token}` : "";
config.headers.common["Authorization"] = auth;
return config;
},
(error) => Promise.reject(error)
);
// setAuthorizationToken(token);
});
};
};
Token correctly lands in localstorage.
Global axios settings
var instance = axios.create({
baseURL: "http://localhost:4000",
withCredentials: true,
});
Cors settings on the server side.
app.use(
cors({
credentials: true,
allowedHeaders: "Content-Type, Authorization",
exposedHeaders: "Authorization",
origin:
process.env.NODE_ENV === "development"
? "http://localhost:3000"
: (process.env.FRONTEND_HOST as string),
})
);
Any ideas?