2021-02-02 09:55:14 +00:00
|
|
|
import { makeAutoObservable } from "mobx";
|
2021-02-02 14:26:58 +00:00
|
|
|
import api from "../api";
|
|
|
|
import { setWithCredentialsStatus } from "../api/client";
|
2021-02-02 09:55:14 +00:00
|
|
|
import ModuleStore from "./ModuleStore";
|
|
|
|
import SettingsStore from "./SettingsStore";
|
|
|
|
import UserStore from "./UserStore";
|
|
|
|
|
|
|
|
class AuthStore {
|
|
|
|
userStore = null;
|
|
|
|
moduleStore = null;
|
|
|
|
settingsStore = null;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.userStore = new UserStore();
|
|
|
|
this.moduleStore = new ModuleStore();
|
|
|
|
this.settingsStore = new SettingsStore();
|
|
|
|
makeAutoObservable(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
init = async () => {
|
2021-02-02 14:26:58 +00:00
|
|
|
const isAuthenticated = await api.user.checkIsAuthenticated();
|
|
|
|
|
2021-02-02 09:55:14 +00:00
|
|
|
await this.settingsStore.getPortalSettings();
|
2021-02-02 14:26:58 +00:00
|
|
|
|
|
|
|
if (!isAuthenticated) return;
|
|
|
|
|
2021-02-02 09:55:14 +00:00
|
|
|
await this.userStore.getCurrentUser();
|
|
|
|
await this.moduleStore.getModules();
|
|
|
|
};
|
2021-02-02 14:26:58 +00:00
|
|
|
|
|
|
|
login = async (user, hash) => {
|
|
|
|
try {
|
|
|
|
const response = await api.user.login(user, hash);
|
|
|
|
|
|
|
|
console.log("Login response", response);
|
|
|
|
|
|
|
|
debugger;
|
|
|
|
|
|
|
|
setWithCredentialsStatus(true);
|
|
|
|
|
|
|
|
await this.init();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2021-02-02 09:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new AuthStore();
|