From d039d823af05f97d4bd3e3734a871631400da6e1 Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Thu, 8 Feb 2024 16:21:57 -0800 Subject: [PATCH 1/2] remove all traces of root email account and automatically redirect user to onboarding if first time setup --- backend/endpoints/auth.js | 23 ++++------------- backend/endpoints/system.js | 11 +++++++++ backend/utils/boot/index.js | 26 -------------------- frontend/src/models/system.ts | 26 ++++++++++++++++++++ frontend/src/pages/Authentication/SignIn.tsx | 14 ++++++++--- frontend/src/pages/DocumentView/index.tsx | 2 +- 6 files changed, 54 insertions(+), 48 deletions(-) diff --git a/backend/endpoints/auth.js b/backend/endpoints/auth.js index 4f837299..bc8d59c0 100644 --- a/backend/endpoints/auth.js +++ b/backend/endpoints/auth.js @@ -18,11 +18,11 @@ function authenticationEndpoints(app) { return; } - const onboardingUser = await User.get({ role: "root" }); - if (!onboardingUser) { - response.status(200).json({ completed: true }); - return; - } + // const onboardingUser = await User.get({ role: "root" }); + // if (!onboardingUser) { + // response.status(200).json({ completed: true }); + // return; + // } await Telemetry.sendTelemetry("onboarding_complete"); // Have to send here since we have no other hooks. response.status(200).json({ @@ -53,19 +53,6 @@ function authenticationEndpoints(app) { return; } - if (email === process.env.SYS_EMAIL) { - const completeSetup = (await User.count({ role: "admin" })) > 0; - if (completeSetup) { - response.status(200).json({ - user: null, - valid: false, - token: null, - message: "[004] Invalid login credentials.", - }); - return; - } - } - const existingUser = await User.get({ email: email }); if (!existingUser) { response.status(200).json({ diff --git a/backend/endpoints/system.js b/backend/endpoints/system.js index a5f31e93..83f43299 100644 --- a/backend/endpoints/system.js +++ b/backend/endpoints/system.js @@ -2,6 +2,7 @@ process.env.NODE_ENV === "development" ? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` }) : require("dotenv").config(); const { SystemSettings } = require("../models/systemSettings"); +const { User } = require("../models/user"); const { systemInit } = require("../utils/boot"); const { dumpENV } = require("../utils/env"); const { reqBody, userFromSession } = require("../utils/http"); @@ -125,6 +126,16 @@ function systemEndpoints(app) { } ); + app.get("/system/onboarding-complete", async (_, response) => { + try { + const completeSetup = (await User.count({ role: "admin" })) > 0; + response.status(200).json({ completed: completeSetup }); + } catch (e) { + console.log(e.message, e); + response.sendStatus(500).end(); + } + }); + app.get("/boot", async (_, response) => { try { await systemInit(); diff --git a/backend/utils/boot/index.js b/backend/utils/boot/index.js index f7e488ed..a0e0a407 100644 --- a/backend/utils/boot/index.js +++ b/backend/utils/boot/index.js @@ -3,7 +3,6 @@ process.env.NODE_ENV === "development" : require("dotenv").config(); const { Telemetry } = require("../../models/telemetry"); -const { User } = require("../../models/user"); const { getGitVersion } = require("../metrics"); function setupVectorCacheStorage() { @@ -45,31 +44,6 @@ async function systemInit() { try { setupVectorCacheStorage(); await setupTelemetry(); - const completeSetup = (await User.count({ role: "admin" })) > 0; - if (completeSetup) return; - - process.env.SYS_EMAIL = "root@vectoradmin.com"; - process.env.SYS_PASSWORD = "password"; - - const existingRootUser = await User.get({ - email: process.env.SYS_EMAIL, - role: "root", - }); - if (existingRootUser) return; - - const rootUser = await User.create({ - email: process.env.SYS_EMAIL, - password: process.env.SYS_PASSWORD, - role: "root", - }); - - if (!rootUser) { - console.error("FAILED TO CREATE ROOT USER!", message); - return; - } - - console.log("Root user created with credentials"); - return; } catch (e) { console.error("systemInit", e.message, e); return; diff --git a/frontend/src/models/system.ts b/frontend/src/models/system.ts index ca672aba..0d1ae367 100644 --- a/frontend/src/models/system.ts +++ b/frontend/src/models/system.ts @@ -72,6 +72,32 @@ const System = { return null; }); }, + onboardingComplete: async (): Promise => { + return fetch(`${API_BASE}/system/onboarding-complete`, { + method: 'GET', + cache: 'no-cache', + }) + .then((res) => res.json()) + .then((res) => res.completed) + .catch((e) => { + console.error(e); + return false; + }); + }, + + // getSetting: async (label: string): Promise<{ label: string; value: any }> => { + // return fetch(`${API_BASE}/system/setting/${label}`, { + // method: 'GET', + // cache: 'no-cache', + // headers: baseHeaders(), + // }) + // .then((res) => res.json()) + // .then((res) => res) + // .catch((e) => { + // console.error(e); + // return { label, value: null, error: e.message }; + // }); + // }, }; export default System; diff --git a/frontend/src/pages/Authentication/SignIn.tsx b/frontend/src/pages/Authentication/SignIn.tsx index 9937cc80..340bed41 100644 --- a/frontend/src/pages/Authentication/SignIn.tsx +++ b/frontend/src/pages/Authentication/SignIn.tsx @@ -8,6 +8,7 @@ import User from '@/models/user'; import { APP_NAME, STORE_TOKEN, STORE_USER } from '@/utils/constants'; import paths from '@/utils/paths'; import validateSessionTokenForUser from '@/utils/session'; +import System from '@/models/system'; type IStages = 'loading' | 'failed' | 'success' | 'ready'; type FormTypes = { @@ -52,12 +53,19 @@ const SignIn = () => { if (!!token && !!user) { window.localStorage.setItem(STORE_USER, JSON.stringify(user)); window.localStorage.setItem(STORE_TOKEN, token); - window.location.replace( - user.role === 'root' ? paths.onboardingSetup() : paths.dashboard() - ); + window.location.replace(paths.dashboard()); } }; + useEffect(() => { + async function checkOnboardingComplete() { + const completed = await System.onboardingComplete(); + if (completed) return; + window.location.replace(paths.onboardingSetup()); + } + checkOnboardingComplete(); + }, []); + useEffect(() => { async function checkAuth() { const currentToken = window.localStorage.getItem(STORE_TOKEN); diff --git a/frontend/src/pages/DocumentView/index.tsx b/frontend/src/pages/DocumentView/index.tsx index 7d5d4468..ce5d2c08 100644 --- a/frontend/src/pages/DocumentView/index.tsx +++ b/frontend/src/pages/DocumentView/index.tsx @@ -292,7 +292,7 @@ const CopyDocToModal = memo( className="rounded-lg border border-white/10 bg-main-2 px-2 py-2 text-white/60" > {workspaces - .filter((ws) => ws.id !== workspace.id) + .filter((ws) => ws.id !== workspace?.id) .map((ws: any) => { return ; })} From ab9e39afa686fc1282a8b01458a43942c521d5bd Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Thu, 8 Feb 2024 16:26:01 -0800 Subject: [PATCH 2/2] remove unneeded comments --- backend/endpoints/auth.js | 6 ------ frontend/src/models/system.ts | 14 -------------- 2 files changed, 20 deletions(-) diff --git a/backend/endpoints/auth.js b/backend/endpoints/auth.js index bc8d59c0..c4faef64 100644 --- a/backend/endpoints/auth.js +++ b/backend/endpoints/auth.js @@ -18,12 +18,6 @@ function authenticationEndpoints(app) { return; } - // const onboardingUser = await User.get({ role: "root" }); - // if (!onboardingUser) { - // response.status(200).json({ completed: true }); - // return; - // } - await Telemetry.sendTelemetry("onboarding_complete"); // Have to send here since we have no other hooks. response.status(200).json({ valid: true, diff --git a/frontend/src/models/system.ts b/frontend/src/models/system.ts index 0d1ae367..e2213aa2 100644 --- a/frontend/src/models/system.ts +++ b/frontend/src/models/system.ts @@ -84,20 +84,6 @@ const System = { return false; }); }, - - // getSetting: async (label: string): Promise<{ label: string; value: any }> => { - // return fetch(`${API_BASE}/system/setting/${label}`, { - // method: 'GET', - // cache: 'no-cache', - // headers: baseHeaders(), - // }) - // .then((res) => res.json()) - // .then((res) => res) - // .catch((e) => { - // console.error(e); - // return { label, value: null, error: e.message }; - // }); - // }, }; export default System;