diff --git a/client/src/pages/auth-page.tsx b/client/src/pages/auth-page.tsx index 1b8eb6b..84b67c2 100644 --- a/client/src/pages/auth-page.tsx +++ b/client/src/pages/auth-page.tsx @@ -100,18 +100,29 @@ export default function AuthPage() { }, }); - // Register mutation + // Register mutation - using our simplified endpoint const registerMutation = useMutation({ mutationFn: async (credentials: any) => { - const res = await apiRequest("POST", "/api/register", credentials); - return await res.json(); + try { + const res = await apiRequest("POST", "/api/simple-register", credentials); + if (!res.ok) { + const errorData = await res.json(); + throw new Error(errorData.message || "Registration failed"); + } + return await res.json(); + } catch (error) { + console.error("Registration error:", error); + throw error instanceof Error ? error : new Error("Unknown registration error"); + } }, - onSuccess: (user) => { + onSuccess: (response) => { toast({ title: "Registration successful", - description: `Welcome, ${user.username}!`, + description: response.message || `Welcome, ${response.username}!`, }); - navigate('/'); + // Switch to login tab automatically + const loginTab = document.querySelector('[data-state="inactive"][data-value="login"]') as HTMLElement; + if (loginTab) loginTab.click(); }, onError: (error: Error) => { toast({ diff --git a/server/routes.ts b/server/routes.ts index 6170219..01fb949 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -23,6 +23,72 @@ export async function registerRoutes(app: Express): Promise { // Initialize Role Based Access Control system await initializeRBAC(); + + // Simple registration endpoint that bypasses the complex auth system + app.post("/api/simple-register", async (req, res) => { + try { + console.log("Simple registration attempt:", { ...req.body, password: "***" }); + + const { username, password, email, fullName } = req.body; + + if (!username || !password) { + return res.status(400).json({ message: "Username and password are required" }); + } + + // Check for existing user + try { + const existingUser = await storage.getUserByUsername(username); + if (existingUser) { + console.log("Username already exists:", username); + return res.status(400).json({ message: "Username already exists" }); + } + } catch (error) { + console.error("Error checking for existing user:", error); + return res.status(500).json({ message: "Error checking for existing user" }); + } + + // Set a default role ID + // In our system, roleId 2 is the standard user role + const roleId = 2; // Regular user role + console.log("Using default role ID:", roleId); + + // Hash the password + let hashedPassword; + try { + const salt = require('crypto').randomBytes(16).toString('hex'); + const hash = require('crypto').scryptSync(password, salt, 64).toString('hex'); + hashedPassword = `${hash}.${salt}`; + } catch (error) { + console.error("Error hashing password:", error); + return res.status(500).json({ message: "Error processing password" }); + } + + // Create the user + try { + const user = await storage.createUser({ + username, + password: hashedPassword, + email: email || null, + fullName: fullName || null, + roleId: roleId, + }); + + console.log("User created successfully:", { id: user.id, username }); + + const userResponse = { ...user, password: undefined }; + return res.status(201).json({ + ...userResponse, + message: "Registration successful, please log in" + }); + } catch (error) { + console.error("Error creating user:", error); + return res.status(500).json({ message: "Error creating user in database" }); + } + } catch (error) { + console.error("Unexpected error during simple registration:", error); + return res.status(500).json({ message: "Internal server error during registration" }); + } + }); // Error handler for Zod validation errors const handleZodError = (err: ZodError, res: Response) => {