Simplified form control in Login and Register. Fixed registration bug. Throw better errors

This commit is contained in:
Alex Holliday committed 2024-06-12 13:30:56 -07:00
1 parent 7ce28b7042
commit 08565c9eda
6 files changed
+8 -37

No files matched your search

+4
View File
@@ -5,6 +5,7 @@ const Check = require("../models/Check");
const Alert = require("../models/Alert");
const RecoveryToken = require("../models/RecoveryToken");
const crypto = require("crypto");
const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key
const connect = async () => {
try {
@@ -34,6 +35,9 @@ const insertUser = async (req, res) => {
await newUser.save();
return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find
} catch (error) {
if (error.code === DUPLICATE_KEY_CODE) {
throw new Error("Email already exists");
}
throw error;
}
};