Add simplified user registration endpoint.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 705f2157-ef97-4fbd-89e4-8c7f2ecaea90
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7ed01c5f-a82d-405a-b728-b2e3d127c60c/6d7c58e6-0cd9-4f69-81f7-091dce31abab.jpg
This commit is contained in:
alphaeusmote
2025-04-08 21:10:03 +00:00
parent 4d9f92778b
commit a1cc112435
2 changed files with 83 additions and 6 deletions
+17 -6
View File
@@ -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({
+66
View File
@@ -23,6 +23,72 @@ export async function registerRoutes(app: Express): Promise<Server> {
// 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) => {