Есть кастомный хук useAxios. Не имеет значения, что он делает. Как внутри этого хука получить доступ к userStore mobx?
Код userStore
import axios from "axios/index";
import { BASE_SERVER_API_URL } from "../config/constants";
import { redirect } from "react-router-dom";
import { makeAutoObservable } from "mobx";
class UserStore {
username = ""
image = ""
accessToken = ""
role = ""
email = ""
constructor() {
makeAutoObservable(this);
}
setUserInfo(user) {
this.username = user.username;
this.image = user.image;
this.email = user.email;
this.role = user.role;
}
setAccessToken(token) {
this.accessToken = token;
}
getDataFromStorage() {
let data = localStorage.getItem("user");
if (data !== null) {
let userInfo = JSON.parse(data);
this.username = userInfo.username;
this.image = userInfo.image;
this.email = userInfo.email;
this.role = userInfo.role;
this.accessToken = userInfo.accessToken;
}
}
saveUserToStorage() {
let saveData = {
username: this.username,
image: this.image,
email: this.email,
role: this.role,
accessToken: this.accessToken
}
localStorage.setItem("user", JSON.stringify(saveData));
}
async reAuthorize() {
try {
let res = await axios.post(`${BASE_SERVER_API_URL}/v1/auth/token-pair/`, null, {withCredentials: true});
this.accessToken = res.data.access;
this.saveUserToStorage();
} catch (e) {
try {
this.logout();
redirect("/identity/login");
} catch (e) {
}
}
}
logout() {
this.username = "";
this.image = "";
this.email = "";
this.role = "";
this.accessToken = "";
this.saveUserToStorage();
}
}
let userStore = new UserStore();
export default userStore;