Alex Pomazan
Alex Pomazan

Reputation: 21

How could I pass token from cookies to each request using mobx?

I'm writing a web application with Next.js and mobx. How can I pass token (from cookies) for each request to API? Sometimes I should take it from the browser and sometimes from server side (context object). Notice, that I make requests in mobx actions (@action). Thanks in advance!

Upvotes: 0

Views: 364

Answers (1)

Nir Weber
Nir Weber

Reputation: 181

It depends on the way you are calling the API. You can achieve that with Axios for example:

const token = localStorage.get('access_token');
const { data } = awit axios('/api/users', { headers: { Authorization: `bearer ${token}` } });

Upvotes: 0

Related Questions