Next.js Discord

Discord Forum

How to add authorization token to axios headers?

Unanswered
Michalx0x posted this in #help-forum
Open in Discord
I created a middleware, which protect routes (image) and now I want to set jwt token, which is in cookies to axios headers, but I don't know how.

I tried like this:
axios.defaults.headers.common['Authorization'] = token ? Bearer ${token.value} : null;
and when I log axios headers in middleware the token is there, but It doesn't work and I get 401 code.

Anyone have an idea how to solve this? Maybe you need to set cookie headers elsewhere in the application?

Github project link: https://github.com/Skolaczk/SocialHub/tree/SH-26-protect-routes

1 Reply

When I set token to axios headers in home page everything works, but i don't know if this is a good solution