Created
February 26, 2025 13:41
-
-
Save spinningcat/9452a6dccbb3f5607a29be19e89102c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/api/register', async (req, res) => { | |
const { email, name, password, userType } = req.body; | |
// Check if all required fields are present | |
if (!email || !name || !password || !userType) { | |
return res.status(400).json({ success: false, message: "All fields are required" }); | |
} | |
// Check if the email is already registered | |
const existingUser = await prisma.user.findUnique({ | |
where: { Email: email }, | |
}); | |
if (existingUser) { | |
return res.status(400).json({ success: false, message: "Email is already taken" }); | |
} | |
try { | |
// Hash the password with bcrypt (using a salt rounds of 10) | |
const hashedPassword = await bcrypt.hash(password, 10); | |
// Create new user in the database | |
const newUser = await prisma.user.create({ | |
data: { | |
Email: email, | |
Name: name, | |
Password: hashedPassword, | |
UserType: userType, | |
}, | |
}); | |
// Return success response | |
return res.status(201).json({ | |
success: true, | |
message: "User successfully registered", | |
user: { | |
id: newUser.id, | |
email: newUser.Email, | |
name: newUser.Name, | |
userType: newUser.UserType, | |
}, | |
}); | |
} catch (error) { | |
console.error(error); | |
return res.status(500).json({"success": false, message: messageObj["ServerError"], object: null }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment