* WIP onboarding v2 * Welcome screen for onboarding complete * fix home page and WIP create skeleton for llm preference search/options * render llms as options * add search functionality to llm preference & add survey step * fix openai settings undefined & create custom logo onboarding page * add user setup UI * add data handling & privacy onboarding screen * add create workspace onboarding screen * fix survey width in onboarding * create vector database connection onboarding page * add workspace image & all skeleton ui complete * fix navigation buttons and ui tweaks to fit on screen * WIP LLMPreference * LLM Preference screen fully functional * create components for vector db options and fix styling of azure options * remove unneeded comment * vector db connection onboarding screen complete * minor ui tweak to searchbar * user setup page fully working * create workspace onboarding page fully working * useNavigate for navigation between pages * mobile layout, cleanup old files, survey functionality implemented * fix default logo appearing when should be blank & password setup bug fix * Modify flow of onboarding todo: embedding set up * Add embedder setup screen & insert into flow * update embedding back button auto-dismiss toasts on each step * move page defs under imports fix bg color on mobile styling --------- Co-authored-by: shatfield4 <seanhatfield5@gmail.com>
142 lines
3.9 KiB
JavaScript
142 lines
3.9 KiB
JavaScript
import { useEffect, useState } from "react";
|
|
import { Navigate } from "react-router-dom";
|
|
import { FullScreenLoader } from "../Preloader";
|
|
import validateSessionTokenForUser from "@/utils/session";
|
|
import paths from "@/utils/paths";
|
|
import { AUTH_TIMESTAMP, AUTH_TOKEN, AUTH_USER } from "@/utils/constants";
|
|
import { userFromStorage } from "@/utils/request";
|
|
import System from "@/models/system";
|
|
import UserMenu from "../UserMenu";
|
|
|
|
// Used only for Multi-user mode only as we permission specific pages based on auth role.
|
|
// When in single user mode we just bypass any authchecks.
|
|
function useIsAuthenticated() {
|
|
const [isAuthd, setIsAuthed] = useState(null);
|
|
const [shouldRedirectToOnboarding, setShouldRedirectToOnboarding] =
|
|
useState(false);
|
|
const [multiUserMode, setMultiUserMode] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const validateSession = async () => {
|
|
const {
|
|
MultiUserMode,
|
|
RequiresAuth,
|
|
LLMProvider = null,
|
|
VectorDB = null,
|
|
} = await System.keys();
|
|
|
|
setMultiUserMode(MultiUserMode);
|
|
|
|
// Check for the onboarding redirect condition
|
|
if (
|
|
!MultiUserMode &&
|
|
!RequiresAuth && // Not in Multi-user AND no password set.
|
|
!LLMProvider &&
|
|
!VectorDB
|
|
) {
|
|
setShouldRedirectToOnboarding(true);
|
|
setIsAuthed(true);
|
|
return;
|
|
}
|
|
|
|
if (!MultiUserMode && !RequiresAuth) {
|
|
setIsAuthed(true);
|
|
return;
|
|
}
|
|
|
|
// Single User password mode check
|
|
if (!MultiUserMode && RequiresAuth) {
|
|
const localAuthToken = localStorage.getItem(AUTH_TOKEN);
|
|
if (!localAuthToken) {
|
|
setIsAuthed(false);
|
|
return;
|
|
}
|
|
|
|
const isValid = await validateSessionTokenForUser();
|
|
setIsAuthed(isValid);
|
|
return;
|
|
}
|
|
|
|
const localUser = localStorage.getItem(AUTH_USER);
|
|
const localAuthToken = localStorage.getItem(AUTH_TOKEN);
|
|
if (!localUser || !localAuthToken) {
|
|
setIsAuthed(false);
|
|
return;
|
|
}
|
|
|
|
const isValid = await validateSessionTokenForUser();
|
|
if (!isValid) {
|
|
localStorage.removeItem(AUTH_USER);
|
|
localStorage.removeItem(AUTH_TOKEN);
|
|
localStorage.removeItem(AUTH_TIMESTAMP);
|
|
setIsAuthed(false);
|
|
return;
|
|
}
|
|
|
|
setIsAuthed(true);
|
|
};
|
|
validateSession();
|
|
}, []);
|
|
|
|
return { isAuthd, shouldRedirectToOnboarding, multiUserMode };
|
|
}
|
|
|
|
// Allows only admin to access the route and if in single user mode,
|
|
// allows all users to access the route
|
|
export function AdminRoute({ Component }) {
|
|
const { isAuthd, shouldRedirectToOnboarding, multiUserMode } =
|
|
useIsAuthenticated();
|
|
if (isAuthd === null) return <FullScreenLoader />;
|
|
|
|
if (shouldRedirectToOnboarding) {
|
|
return <Navigate to={paths.onboarding.home()} />;
|
|
}
|
|
|
|
const user = userFromStorage();
|
|
return isAuthd && (user?.role === "admin" || !multiUserMode) ? (
|
|
<UserMenu>
|
|
<Component />
|
|
</UserMenu>
|
|
) : (
|
|
<Navigate to={paths.home()} />
|
|
);
|
|
}
|
|
|
|
// Allows manager and admin to access the route and if in single user mode,
|
|
// allows all users to access the route
|
|
export function ManagerRoute({ Component }) {
|
|
const { isAuthd, shouldRedirectToOnboarding, multiUserMode } =
|
|
useIsAuthenticated();
|
|
if (isAuthd === null) return <FullScreenLoader />;
|
|
|
|
if (shouldRedirectToOnboarding) {
|
|
return <Navigate to={paths.onboarding.home()} />;
|
|
}
|
|
|
|
const user = userFromStorage();
|
|
return isAuthd && (user?.role !== "default" || !multiUserMode) ? (
|
|
<UserMenu>
|
|
<Component />
|
|
</UserMenu>
|
|
) : (
|
|
<Navigate to={paths.home()} />
|
|
);
|
|
}
|
|
|
|
export default function PrivateRoute({ Component }) {
|
|
const { isAuthd, shouldRedirectToOnboarding } = useIsAuthenticated();
|
|
if (isAuthd === null) return <FullScreenLoader />;
|
|
|
|
if (shouldRedirectToOnboarding) {
|
|
return <Navigate to="/onboarding" />;
|
|
}
|
|
|
|
return isAuthd ? (
|
|
<UserMenu>
|
|
<Component />
|
|
</UserMenu>
|
|
) : (
|
|
<Navigate to={paths.login()} />
|
|
);
|
|
}
|