From 71d9884a863769828d75f2cd20a3b4089f7d1fdf Mon Sep 17 00:00:00 2001 From: tigattack <10629864+tigattack@users.noreply.github.com> Date: Thu, 25 Sep 2025 00:41:29 +0100 Subject: [PATCH 1/6] fix(frontend): imports are unused --- frontend/src/main.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/src/main.jsx b/frontend/src/main.jsx index 7666468..8ab4536 100644 --- a/frontend/src/main.jsx +++ b/frontend/src/main.jsx @@ -1,5 +1,4 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; -import React from "react"; import ReactDOM from "react-dom/client"; import { BrowserRouter } from "react-router-dom"; import App from "./App.jsx"; From e3aa28a8d9d4a890d1fa54ef03dab2e4c99abfb5 Mon Sep 17 00:00:00 2001 From: tigattack <10629864+tigattack@users.noreply.github.com> Date: Thu, 25 Sep 2025 08:57:58 +0100 Subject: [PATCH 2/6] fix: login after signup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also resolves entire user object being return to client, including password_hash... ⚠️ --- backend/src/routes/authRoutes.js | 14 +++++++++++++- .../src/components/FirstTimeAdminSetup.jsx | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/backend/src/routes/authRoutes.js b/backend/src/routes/authRoutes.js index d61934b..bcffc02 100644 --- a/backend/src/routes/authRoutes.js +++ b/backend/src/routes/authRoutes.js @@ -118,9 +118,21 @@ router.post( // Create default dashboard preferences for the new admin user await createDefaultDashboardPreferences(user.id, "admin"); + // Generate token for immediate login + const token = generateToken(user.id); + res.status(201).json({ message: "Admin user created successfully", - user: user, + token, + user: { + id: user.id, + username: user.username, + email: user.email, + role: user.role, + first_name: user.first_name, + last_name: user.last_name, + is_active: user.is_active, + }, }); } catch (error) { console.error("Error creating admin user:", error); diff --git a/frontend/src/components/FirstTimeAdminSetup.jsx b/frontend/src/components/FirstTimeAdminSetup.jsx index 38a8877..5d34624 100644 --- a/frontend/src/components/FirstTimeAdminSetup.jsx +++ b/frontend/src/components/FirstTimeAdminSetup.jsx @@ -3,7 +3,7 @@ import { useId, useState } from "react"; import { useAuth } from "../contexts/AuthContext"; const FirstTimeAdminSetup = () => { - const { login } = useAuth(); + const { login, setAuthState } = useAuth(); const firstNameId = useId(); const lastNameId = useId(); const usernameId = useId(); @@ -95,10 +95,18 @@ const FirstTimeAdminSetup = () => { if (response.ok) { setSuccess(true); - // Auto-login the user after successful setup - setTimeout(() => { - login(formData.username.trim(), formData.password); - }, 2000); + + // If the response includes a token, use it to automatically log in + if (data.token && data.user) { + // Auto-login using the token from the setup response + setAuthState(data.token, data.user); + setTimeout(() => {}, 2000); + } else { + // Fallback to manual login if no token provided + setTimeout(() => { + login(formData.username.trim(), formData.password); + }, 2000); + } } else { setError(data.error || "Failed to create admin user"); } From ba087eb23ed6429063d4cf78874c7c18a86c16b0 Mon Sep 17 00:00:00 2001 From: tigattack <10629864+tigattack@users.noreply.github.com> Date: Thu, 25 Sep 2025 08:59:28 +0100 Subject: [PATCH 3/6] chore: add types/bcryptjs --- package-lock.json | 8 ++++++++ package.json | 1 + 2 files changed, 9 insertions(+) diff --git a/package-lock.json b/package-lock.json index 4cfd878..867bb21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@biomejs/biome": "2.2.4", "@commitlint/cli": "^20.0.0", "@commitlint/config-conventional": "^20.0.0", + "@types/bcryptjs": "^2.4.6", "concurrently": "^8.2.2", "lefthook": "^1.13.4", "lint-staged": "^15.2.10", @@ -1337,6 +1338,13 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/bcryptjs": { + "version": "2.4.6", + "resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.6.tgz", + "integrity": "sha512-9xlo6R2qDs5uixm0bcIqCeMCE6HiQsIyel9KQySStiyqNl2tnj2mP3DX1Nf56MD6KMenNNlBBsy3LJ7gUEQPXQ==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/conventional-commits-parser": { "version": "5.0.1", "dev": true, diff --git a/package.json b/package.json index b3329b6..5f9c7bb 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "devDependencies": { "@biomejs/biome": "2.2.4", + "@types/bcryptjs": "^2.4.6", "@commitlint/cli": "^20.0.0", "@commitlint/config-conventional": "^20.0.0", "concurrently": "^8.2.2", From 5c6688773217d5545fda94eb3c746c21cdc6c60d Mon Sep 17 00:00:00 2001 From: tigattack <10629864+tigattack@users.noreply.github.com> Date: Thu, 25 Sep 2025 19:39:52 +0100 Subject: [PATCH 4/6] refactor(frontend): optimise auth process - Stops frontend trying to make calls that require auth before auth has occured - Stops frontend making calls that aren't necessary before auth has occured - Implements state machine to better handle auth phases --- frontend/src/App.jsx | 10 +++-- frontend/src/components/Layout.jsx | 9 ++++ frontend/src/constants/authPhases.js | 29 ++++++++++++ frontend/src/contexts/AuthContext.jsx | 45 ++++++++++++------- .../contexts/UpdateNotificationContext.jsx | 23 +++++++--- 5 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 frontend/src/constants/authPhases.js diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 4f015f6..e6a3efd 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -2,6 +2,7 @@ import { Route, Routes } from "react-router-dom"; import FirstTimeAdminSetup from "./components/FirstTimeAdminSetup"; import Layout from "./components/Layout"; import ProtectedRoute from "./components/ProtectedRoute"; +import { isAuthPhase } from "./constants/authPhases"; import { AuthProvider, useAuth } from "./contexts/AuthContext"; import { ThemeProvider } from "./contexts/ThemeContext"; import { UpdateNotificationProvider } from "./contexts/UpdateNotificationContext"; @@ -20,11 +21,14 @@ import Settings from "./pages/Settings"; import Users from "./pages/Users"; function AppRoutes() { - const { needsFirstTimeSetup, checkingSetup, isAuthenticated } = useAuth(); + const { needsFirstTimeSetup, authPhase, isAuthenticated } = useAuth(); const isAuth = isAuthenticated(); // Call the function to get boolean value - // Show loading while checking if setup is needed - if (checkingSetup) { + // Show loading while checking setup or initialising + if ( + isAuthPhase.initialising(authPhase) || + isAuthPhase.checkingSetup(authPhase) + ) { return (