mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-11-04 14:03:17 +00:00
fix(frontend): actually fix login after signup
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import { AlertCircle, CheckCircle, Shield, UserPlus } from "lucide-react";
|
import { AlertCircle, CheckCircle, Shield, UserPlus } from "lucide-react";
|
||||||
import { useId, useState } from "react";
|
import { useId, useState } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useAuth } from "../contexts/AuthContext";
|
import { useAuth } from "../contexts/AuthContext";
|
||||||
|
|
||||||
const FirstTimeAdminSetup = () => {
|
const FirstTimeAdminSetup = () => {
|
||||||
const { login, setAuthState } = useAuth();
|
const { login, setAuthState } = useAuth();
|
||||||
|
const navigate = useNavigate();
|
||||||
const firstNameId = useId();
|
const firstNameId = useId();
|
||||||
const lastNameId = useId();
|
const lastNameId = useId();
|
||||||
const usernameId = useId();
|
const usernameId = useId();
|
||||||
@@ -98,13 +100,24 @@ const FirstTimeAdminSetup = () => {
|
|||||||
|
|
||||||
// If the response includes a token, use it to automatically log in
|
// If the response includes a token, use it to automatically log in
|
||||||
if (data.token && data.user) {
|
if (data.token && data.user) {
|
||||||
// Auto-login using the token from the setup response
|
// Set the authentication state immediately
|
||||||
setAuthState(data.token, data.user);
|
setAuthState(data.token, data.user);
|
||||||
setTimeout(() => {}, 2000);
|
// Navigate to dashboard after successful setup
|
||||||
|
setTimeout(() => {
|
||||||
|
navigate("/", { replace: true });
|
||||||
|
}, 100); // Small delay to ensure auth state is set
|
||||||
} else {
|
} else {
|
||||||
// Fallback to manual login if no token provided
|
// Fallback to manual login if no token provided
|
||||||
setTimeout(() => {
|
setTimeout(async () => {
|
||||||
login(formData.username.trim(), formData.password);
|
try {
|
||||||
|
await login(formData.username.trim(), formData.password);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Auto-login failed:", error);
|
||||||
|
setError(
|
||||||
|
"Account created but auto-login failed. Please login manually.",
|
||||||
|
);
|
||||||
|
setSuccess(false);
|
||||||
|
}
|
||||||
}, 2000);
|
}, 2000);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -132,8 +145,8 @@ const FirstTimeAdminSetup = () => {
|
|||||||
Admin Account Created!
|
Admin Account Created!
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-secondary-600 dark:text-secondary-300 mb-6">
|
<p className="text-secondary-600 dark:text-secondary-300 mb-6">
|
||||||
Your admin account has been successfully created. You will be
|
Your admin account has been successfully created and you are now
|
||||||
automatically logged in shortly.
|
logged in. Redirecting to the dashboard...
|
||||||
</p>
|
</p>
|
||||||
<div className="flex justify-center">
|
<div className="flex justify-center">
|
||||||
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary-600"></div>
|
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary-600"></div>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import { flushSync } from "react-dom";
|
||||||
import { AUTH_PHASES, isAuthPhase } from "../constants/authPhases";
|
import { AUTH_PHASES, isAuthPhase } from "../constants/authPhases";
|
||||||
|
|
||||||
const AuthContext = createContext();
|
const AuthContext = createContext();
|
||||||
@@ -268,11 +269,20 @@ export const AuthProvider = ({ children }) => {
|
|||||||
}, [authPhase, checkAdminUsersExist]);
|
}, [authPhase, checkAdminUsersExist]);
|
||||||
|
|
||||||
const setAuthState = (authToken, authUser) => {
|
const setAuthState = (authToken, authUser) => {
|
||||||
setToken(authToken);
|
// Use flushSync to ensure all state updates are applied synchronously
|
||||||
setUser(authUser);
|
flushSync(() => {
|
||||||
|
setToken(authToken);
|
||||||
|
setUser(authUser);
|
||||||
|
setNeedsFirstTimeSetup(false);
|
||||||
|
setAuthPhase(AUTH_PHASES.READY);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Store in localStorage after state is updated
|
||||||
localStorage.setItem("token", authToken);
|
localStorage.setItem("token", authToken);
|
||||||
localStorage.setItem("user", JSON.stringify(authUser));
|
localStorage.setItem("user", JSON.stringify(authUser));
|
||||||
setAuthPhase(AUTH_PHASES.READY); // Authentication complete, move to ready phase
|
|
||||||
|
// Fetch permissions immediately for the new authenticated user
|
||||||
|
fetchPermissions(authToken);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Computed loading state based on phase and permissions state
|
// Computed loading state based on phase and permissions state
|
||||||
|
|||||||
Reference in New Issue
Block a user