Skip to content

Instantly share code, notes, and snippets.

@spinningcat
Created February 26, 2025 13:41
Show Gist options
  • Save spinningcat/9452a6dccbb3f5607a29be19e89102c8 to your computer and use it in GitHub Desktop.
Save spinningcat/9452a6dccbb3f5607a29be19e89102c8 to your computer and use it in GitHub Desktop.
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