DocSpace-client/packages/client/src/store/AuthStore.js

288 lines
6.8 KiB
JavaScript
Raw Normal View History

2021-03-01 15:15:11 +00:00
import { makeAutoObservable } from "mobx";
2022-08-04 14:34:53 +00:00
import api from "@docspace/common/api";
import { setWithCredentialsStatus } from "@docspace/common/api/client";
//import history from "../history";
2022-08-04 14:34:53 +00:00
import SettingsStore from "./PortalSettingsStore";
2021-02-02 09:55:14 +00:00
import UserStore from "./UserStore";
2021-05-05 11:59:08 +00:00
import TfaStore from "./TfaStore";
import InfoPanelStore from "./InfoPanelStore";
2022-08-04 14:34:53 +00:00
import {
logout as logoutDesktop,
desktopConstants,
} from "@docspace/common/desktop";
import {
/*combineUrl,*/ isAdmin,
setCookie,
getCookie,
} from "@docspace/common/utils";
2022-08-04 14:34:53 +00:00
import {
/*AppServerConfig,*/ LANGUAGE,
COOKIE_EXPIRATION_YEAR,
2022-08-04 14:34:53 +00:00
TenantStatus,
} from "@docspace/common/constants";
//const { proxyURL } = AppServerConfig;
2021-02-02 09:55:14 +00:00
class AuthStore {
userStore = null;
2021-02-02 09:55:14 +00:00
settingsStore = null;
2021-05-05 11:59:08 +00:00
tfaStore = null;
infoPanelStore = null;
2021-02-02 09:55:14 +00:00
2021-02-03 12:42:47 +00:00
isLoading = false;
Merge branch 'develop' into feature/mobx # Conflicts: # products/ASC.Files/Client/src/App.js # products/ASC.Files/Client/src/components/Article/Body/ThirdPartyList.js # products/ASC.Files/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.Files/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.Files/Client/src/components/pages/Home/index.js # products/ASC.Files/Client/src/store/files/selectors.js # products/ASC.People/Client/src/App.js # products/ASC.People/Client/src/components/pages/GroupAction/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.People/Client/src/components/pages/Home/index.js # products/ASC.People/Client/src/components/pages/Profile/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Profile/index.js # products/ASC.People/Client/src/components/pages/ProfileAction/index.js # products/ASC.People/Client/src/store/group/actions.js # products/ASC.People/Client/src/store/people/actions.js # products/ASC.People/Client/src/store/people/selectors.js # products/ASC.People/Client/yarn.lock # web/ASC.Web.Client/src/App.js # web/ASC.Web.Client/src/components/pages/About/index.js # web/ASC.Web.Client/src/components/pages/Home/index.js # web/ASC.Web.Client/src/store/settings/actions.js # web/ASC.Web.Common/src/components/Layout/index.js # web/ASC.Web.Common/src/components/NavMenu/index.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-nav.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-unauth.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/nav-item.js # web/ASC.Web.Common/src/components/PageLayout/index.js # web/ASC.Web.Common/src/store/auth/actions.js # web/ASC.Web.Common/src/store/auth/reducer.js # web/ASC.Web.Common/src/store/auth/selectors.js # web/ASC.Web.Components/src/components/loader/types/rombs.js
2021-02-20 14:31:58 +00:00
version = null;
providers = [];
isInit = false;
2021-02-02 09:55:14 +00:00
constructor() {
this.userStore = new UserStore();
this.settingsStore = new SettingsStore();
2021-05-05 11:59:08 +00:00
this.tfaStore = new TfaStore();
this.infoPanelStore = new InfoPanelStore();
2021-02-03 12:42:47 +00:00
2021-03-01 15:15:11 +00:00
makeAutoObservable(this);
2021-02-02 09:55:14 +00:00
}
init = async () => {
if (this.isInit) return;
this.isInit = true;
2022-03-24 17:56:51 +00:00
try {
await this.userStore.init();
} catch (e) {
console.error(e);
}
2021-02-03 12:42:47 +00:00
const requests = [];
requests.push(this.settingsStore.init());
if (this.isAuthenticated) {
2022-07-13 18:00:42 +00:00
!this.settingsStore.passwordSettings &&
requests.push(this.settingsStore.getPortalPasswordSettings());
2021-02-03 12:42:47 +00:00
}
return Promise.all(requests);
};
setLanguage() {
2022-03-24 17:56:51 +00:00
if (this.userStore.user?.cultureName) {
getCookie(LANGUAGE) !== this.userStore.user.cultureName &&
setCookie(LANGUAGE, this.userStore.user.cultureName, {
"max-age": COOKIE_EXPIRATION_YEAR,
});
} else {
setCookie(LANGUAGE, this.settingsStore.culture || "en-US", {
"max-age": COOKIE_EXPIRATION_YEAR,
});
}
}
2021-02-03 12:42:47 +00:00
get isLoaded() {
let success = false;
if (this.isAuthenticated) {
2022-03-24 17:56:51 +00:00
success =
(this.userStore.isLoaded && this.settingsStore.isLoaded) ||
this.settingsStore.tenantStatus === TenantStatus.PortalRestore;
success && this.setLanguage();
2021-02-03 12:42:47 +00:00
} else {
success = this.settingsStore.isLoaded;
}
return success;
}
get language() {
return (
(this.userStore.user && this.userStore.user.cultureName) ||
this.settingsStore.culture ||
"en"
2021-02-03 12:42:47 +00:00
);
}
get isAdmin() {
const { user } = this.userStore;
2021-02-03 12:42:47 +00:00
const { currentProductId } = this.settingsStore;
if (!user || !user.id) return false;
2021-02-03 12:42:47 +00:00
return isAdmin(user, currentProductId);
2021-02-03 12:42:47 +00:00
}
login = async (user, hash, session = true) => {
2021-02-02 14:26:58 +00:00
try {
const response = await api.user.login(user, hash, session);
2021-02-02 14:26:58 +00:00
2021-05-14 12:59:15 +00:00
if (!response || (!response.token && !response.tfa))
2021-05-30 19:48:04 +00:00
throw response.error.message;
2021-05-14 12:59:15 +00:00
if (response.tfa && response.confirmUrl) {
const url = response.confirmUrl.replace(window.location.origin, "");
2022-08-04 15:23:19 +00:00
return url;
2021-05-14 12:59:15 +00:00
}
2021-02-02 14:26:58 +00:00
setWithCredentialsStatus(true);
this.reset();
this.init();
2021-02-02 14:26:58 +00:00
2022-08-04 15:23:19 +00:00
return this.settingsStore.defaultPage;
2021-02-02 14:26:58 +00:00
} catch (e) {
2021-03-03 11:09:00 +00:00
return Promise.reject(e);
2021-02-02 14:26:58 +00:00
}
};
2021-02-03 12:42:47 +00:00
loginWithCode = async (userName, passwordHash, code) => {
await this.tfaStore.loginWithCode(userName, passwordHash, code);
setWithCredentialsStatus(true);
this.reset();
this.init();
return Promise.resolve(this.settingsStore.defaultPage);
};
2021-03-24 13:49:42 +00:00
thirdPartyLogin = async (SerializedProfile) => {
try {
const response = await api.user.thirdPartyLogin(SerializedProfile);
if (!response || !response.token) throw new Error("Empty API response");
2021-03-24 13:49:42 +00:00
setWithCredentialsStatus(true);
this.reset();
this.init();
2021-03-24 13:49:42 +00:00
return Promise.resolve(this.settingsStore.defaultPage);
2021-03-24 13:49:42 +00:00
} catch (e) {
return Promise.reject(e);
}
};
reset = (skipUser = false) => {
this.isInit = false;
this.skipModules = false;
if (!skipUser) {
this.userStore = new UserStore();
}
2021-02-25 08:35:31 +00:00
this.settingsStore = new SettingsStore();
};
2022-08-04 15:23:19 +00:00
logout = async () => {
await api.user.logout();
2021-02-03 12:42:47 +00:00
//console.log("Logout response ", response);
2021-02-03 12:42:47 +00:00
setWithCredentialsStatus(false);
2022-08-04 15:23:19 +00:00
const { isDesktopClient: isDesktop } = this.settingsStore;
2021-02-03 12:42:47 +00:00
isDesktop && logoutDesktop();
};
get isAuthenticated() {
2022-03-24 17:56:51 +00:00
return (
this.userStore.isAuthenticated ||
this.settingsStore.tenantStatus === TenantStatus.PortalRestore
);
}
2021-02-03 12:42:47 +00:00
getEncryptionAccess = (fileId) => {
return api.files
.getEncryptionAccess(fileId)
.then((keys) => {
return Promise.resolve(keys);
})
.catch((err) => console.error(err));
};
replaceFileStream = (fileId, file, encrypted, forcesave) => {
return api.files.updateFileStream(file, fileId, encrypted, forcesave);
};
setEncryptionAccess = (file) => {
return this.getEncryptionAccess(file.id).then((keys) => {
return new Promise((resolve, reject) => {
try {
window.AscDesktopEditor.cloudCryptoCommand(
"share",
{
cryptoEngineId: desktopConstants.cryptoEngineId,
file: [file.viewUrl],
keys: keys,
},
(obj) => {
let resFile = null;
if (obj.isCrypto) {
let bytes = obj.bytes;
let filename = "temp_name";
resFile = new File([bytes], filename);
}
resolve(resFile);
}
);
} catch (e) {
reject(e);
}
});
});
};
setDocumentTitle = (subTitle = null) => {
let title;
const currentModule = this.settingsStore.product;
const organizationName = this.settingsStore.organizationName;
if (subTitle) {
if (this.isAuthenticated && currentModule) {
title = subTitle + " - " + currentModule.title;
} else {
title = subTitle + " - " + organizationName;
}
} else if (currentModule && organizationName) {
title = currentModule.title + " - " + organizationName;
} else {
title = organizationName;
}
document.title = title;
};
Merge branch 'develop' into feature/mobx # Conflicts: # products/ASC.Files/Client/src/App.js # products/ASC.Files/Client/src/components/Article/Body/ThirdPartyList.js # products/ASC.Files/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.Files/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.Files/Client/src/components/pages/Home/index.js # products/ASC.Files/Client/src/store/files/selectors.js # products/ASC.People/Client/src/App.js # products/ASC.People/Client/src/components/pages/GroupAction/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.People/Client/src/components/pages/Home/index.js # products/ASC.People/Client/src/components/pages/Profile/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Profile/index.js # products/ASC.People/Client/src/components/pages/ProfileAction/index.js # products/ASC.People/Client/src/store/group/actions.js # products/ASC.People/Client/src/store/people/actions.js # products/ASC.People/Client/src/store/people/selectors.js # products/ASC.People/Client/yarn.lock # web/ASC.Web.Client/src/App.js # web/ASC.Web.Client/src/components/pages/About/index.js # web/ASC.Web.Client/src/components/pages/Home/index.js # web/ASC.Web.Client/src/store/settings/actions.js # web/ASC.Web.Common/src/components/Layout/index.js # web/ASC.Web.Common/src/components/NavMenu/index.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-nav.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-unauth.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/nav-item.js # web/ASC.Web.Common/src/components/PageLayout/index.js # web/ASC.Web.Common/src/store/auth/actions.js # web/ASC.Web.Common/src/store/auth/reducer.js # web/ASC.Web.Common/src/store/auth/selectors.js # web/ASC.Web.Components/src/components/loader/types/rombs.js
2021-02-20 14:31:58 +00:00
setProductVersion = (version) => {
this.version = version;
};
setProviders = (providers) => {
this.providers = providers;
};
2022-04-21 12:33:48 +00:00
getOforms = () => {
const culture =
this.userStore.user.cultureName || this.settingsStore.culture;
const promise = new Promise(async (resolve, reject) => {
let oforms = await api.settings.getOforms(
`${this.settingsStore.urlOforms}${culture}`
);
2022-06-16 14:03:42 +00:00
if (!oforms?.data?.data.length) {
oforms = await api.settings.getOforms(
`${this.settingsStore.urlOforms}en`
);
}
resolve(oforms);
});
return promise;
2022-04-21 12:33:48 +00:00
};
2021-02-02 09:55:14 +00:00
}
export default new AuthStore();