feat: better error messages for register notif
This commit is contained in:
@@ -17,46 +17,75 @@ router.post("/register", async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
|
|
||||||
if (!email) return res.status(400).send("Email is required");
|
// Validation checks with translation codes
|
||||||
if (!password || password.length < 6)
|
if (!email) {
|
||||||
return res
|
return res.status(400).json({
|
||||||
.status(400)
|
error: "VALIDATION_ERROR",
|
||||||
.send("Password is required and must be at least 6 characters");
|
code: "register.validation.email_required"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!password || password.length < 6) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "VALIDATION_ERROR",
|
||||||
|
code: "register.validation.password_requirements"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if user exists
|
||||||
let userExist = await User.findOne({ raw: true, where: { email } });
|
let userExist = await User.findOne({ raw: true, where: { email } });
|
||||||
if (userExist) return res.status(400).send("User already exists");
|
if (userExist) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "USER_EXISTS",
|
||||||
|
code: "register.error.user_exists"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const hashedPassword = await hashPassword(password);
|
const hashedPassword = await hashPassword(password);
|
||||||
|
|
||||||
|
// Create user
|
||||||
let user: any;
|
let user: any;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
user = await User.create({ email, password: hashedPassword });
|
user = await User.create({ email, password: hashedPassword });
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(`Could not create user - ${error}`);
|
console.log(`Could not create user - ${error}`);
|
||||||
return res.status(500).send(`Could not create user - ${error.message}`);
|
return res.status(500).json({
|
||||||
|
error: "DATABASE_ERROR",
|
||||||
|
code: "register.error.creation_failed"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check JWT secret
|
||||||
if (!process.env.JWT_SECRET) {
|
if (!process.env.JWT_SECRET) {
|
||||||
console.log("JWT_SECRET is not defined in the environment");
|
console.log("JWT_SECRET is not defined in the environment");
|
||||||
return res.status(500).send("Internal Server Error");
|
return res.status(500).json({
|
||||||
|
error: "SERVER_ERROR",
|
||||||
|
code: "register.error.server_error"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Success path
|
||||||
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET as string);
|
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET as string);
|
||||||
user.password = undefined as unknown as string;
|
user.password = undefined as unknown as string;
|
||||||
res.cookie("token", token, {
|
res.cookie("token", token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
capture("maxun-oss-user-registered", {
|
capture("maxun-oss-user-registered", {
|
||||||
email: user.email,
|
email: user.email,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
registeredAt: new Date().toISOString(),
|
registeredAt: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`User registered`);
|
console.log(`User registered`);
|
||||||
res.json(user);
|
res.json(user);
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(`Could not register user - ${error}`);
|
console.log(`Could not register user - ${error}`);
|
||||||
res.status(500).send(`Could not register user - ${error.message}`);
|
return res.status(500).json({
|
||||||
|
error: "SERVER_ERROR",
|
||||||
|
code: "register.error.generic"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user