Не получается добавить токен в заголовок(react, axios)
Всем привет. Есть проект на реакт, в котором после авторизации хотелось бы добавить токен ко всем запросам. Но, что бы я не пробовал, оно не получается. Я попробовал все варианты, описанные в этом обсуждении: https://question-it.com/questions/336071/prisoedinit-zagolovok-avtorizatsii-dlja-vseh-zaprosov-axios . Я не знаю, какие еще могут быть варианты. *если это важно, то в качестве стейт менеджмента использую mobx.
Я создаю instance при помощи этой функции:
export const _createInstance = (url = '') => {
return axios.create({
baseURL: `http://localhost:5000/${url}`,
// headers: {
// 'Authorization': `Bearer ${token}`,
// },
});
}
Использую я ее вот в таких классах.
import {_createInstance} from '../index';
export class Themes {
_instance = _createInstance('themes');
async createTheme(title) {
const res = await this._instance.post('', {title});
return res.data;
}
async getAllThemes() {
const res = await this._instance.get();
return res.data;
}
}
Потом создаю их экземпляры и использую для запросов.
import axios from "axios";
import {Auth} from './endpoints/auth'
import {Tags} from "./endpoints/tags";
import {Users} from "./endpoints/users";
import {Comments} from "./endpoints/comments";
import {Roles} from "./endpoints/roles";
import {Themes} from "./endpoints/themes";
export const _createInstance = (url = '') => {
return axios.create({
baseURL: `http://localhost:5000/${url}`,
// headers: {
// 'Authorization': `Bearer ${token}`,
// },
});
}
export const auth = new Auth();
export const tags = new Tags();
export const users = new Users();
export const comments = new Comments();
export const roles = new Roles();
export const themes = new Themes();