import React, { useState, useEffect, useRef } from "react"; import System from "../../models/system"; export default function PasswordModal() { const [loading, setLoading] = useState(false); const formEl = useRef(null); const [error, setError] = useState(null); const handleLogin = async (e) => { setError(null); setLoading(true); e.preventDefault(); const data = {}; const form = new FormData(formEl.current); for (var [key, value] of form.entries()) data[key] = value; const { valid, token, message } = await System.requestToken(data); if (valid && !!token) { window.localStorage.setItem("anythingllm_authtoken", token); window.location.reload(); } else { setError(message); setLoading(false); } setLoading(false); }; return (

This workspace is password protected.

{error && (

Error: {error}

)}

You will only have to enter this password once. After successful login it will be stored in your browser.

); } export function usePasswordModal() { const [requiresAuth, setRequiresAuth] = useState(null); useEffect(() => { async function checkAuthReq() { if (!window) return; if (import.meta.env.DEV) { setRequiresAuth(false); } else { const currentToken = window.localStorage.getItem( "anythingllm_authtoken" ); const settings = await System.keys(); const requiresAuth = settings?.RequiresAuth || false; // If Auth is disabled - skip check if (!requiresAuth) { setRequiresAuth(requiresAuth); return; } if (!!currentToken) { const valid = await System.checkAuth(currentToken); if (!valid) { setRequiresAuth(true); window.localStorage.removeItem("anythingllm_authtoken"); return; } else { setRequiresAuth(false); return; } } setRequiresAuth(true); } } checkAuthReq(); }, []); return { requiresAuth }; }