merlyn/frontend/src/components/Modals/Password/index.jsx
Timothy Carambat 91f5f94200
[FEATURE] Enable the ability to have multi user instances (#158)
* multi user wip

* WIP MUM features

* invitation mgmt

* suspend or unsuspend users

* workspace mangement

* manage chats

* manage chats

* add Support for admin system settings for users to delete workspaces and limit chats per user

* fix issue ith system var
update app to lazy load invite page

* cleanup and bug fixes

* wrong method

* update readme

* update readme

* update readme

* bump version to 0.1.0
2023-07-25 10:37:04 -07:00

98 lines
2.7 KiB
JavaScript

import React, { useState, useEffect } from "react";
import System from "../../../models/system";
import SingleUserAuth from "./SingleUserAuth";
import MultiUserAuth from "./MultiUserAuth";
import { AUTH_TOKEN, AUTH_USER } from "../../../utils/constants";
export default function PasswordModal({ mode = "single" }) {
return (
<div className="fixed top-0 left-0 right-0 z-50 w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] h-full bg-black bg-opacity-50 flex items-center justify-center">
<div className="flex fixed top-0 left-0 right-0 w-full h-full" />
<div className="relative w-full max-w-2xl max-h-full">
{mode === "single" ? <SingleUserAuth /> : <MultiUserAuth />}
</div>
</div>
);
}
export function usePasswordModal() {
const [auth, setAuth] = useState({
required: false,
mode: "single",
});
useEffect(() => {
async function checkAuthReq() {
if (!window) return;
const settings = await System.keys();
if (settings?.MultiUserMode) {
const currentToken = window.localStorage.getItem(AUTH_TOKEN);
if (!!currentToken) {
const valid = await System.checkAuth(currentToken);
if (!valid) {
setAuth({
requiresAuth: true,
mode: "multi",
});
window.localStorage.removeItem(AUTH_USER);
window.localStorage.removeItem(AUTH_TOKEN);
return;
} else {
setAuth({
requiresAuth: false,
mode: "multi",
});
return;
}
} else {
setAuth({
requiresAuth: true,
mode: "multi",
});
return;
}
} else {
// Running token check in single user Auth mode.
// If Single user Auth is disabled - skip check
const requiresAuth = settings?.RequiresAuth || false;
if (!requiresAuth) {
setAuth({
requiresAuth: false,
mode: "single",
});
return;
}
const currentToken = window.localStorage.getItem(AUTH_TOKEN);
if (!!currentToken) {
const valid = await System.checkAuth(currentToken);
if (!valid) {
setAuth({
requiresAuth: true,
mode: "single",
});
window.localStorage.removeItem(AUTH_TOKEN);
return;
} else {
setAuth({
requiresAuth: false,
mode: "single",
});
return;
}
} else {
setAuth({
requiresAuth: true,
mode: "single",
});
return;
}
}
}
checkAuthReq();
}, []);
return auth;
}