Ваш код должен быть таким:
registerUser: async (req, res) => {
try {
const { username, email, password } = req.body
if (!username || !email || !password) {
return res.status(400).json({ message: "Username, email and password are must" })
}
if (!validator.isEmail(email)) {
return res.status(400).json({ message: "Invaid email" })
}
if (password.length < 6) {
return res.status(400).json({ message: "Password should be of at least 6 characters" })
}
return res.status(200).json({ user: await User.create({ username, email, password }) })
} catch (error) {
return res.status(500).json({ message: "Server error occurred" })
}
},
loginUser: async (req, res, next) => {
try {
const { email, password } = req.body
if (!email || !password) {
return res.status(400).json({ message: "Email and password are must" })
}
if (!validator.isEmail(email)) {
return res.status(400).json({ message: "Invalid email" })
}
const user = await User.findOne({ email });
if (!user) {
return res.status(402).json({ message: "User not found" })
}
if (!user.confirmPassword(password)) {
return res.status(402).json({ message: "incorrect password" })
}
return res.status(200).json({ message: "Login Success" })
} catch (error) {
return next(error)
}
}
Но вы добираетесь до проверки схем. тогда ваш код будет таким
registerUser: async (req, res) => {
try {
const { username, email, password } = req.body
return res.status(200).json({ user: await User.create({ username, email, password }) })
} catch (error) {
return res.status(500).json({ message: "Server error occurred" })
}
},
loginUser: async (req, res, next) => {
try {
const { email, password } = req.body
const user = await User.findOne({ email });
if (!user) {
return res.status(402).json({ message: "User not found" })
}
if (!user.confirmPassword(password)) {
return res.status(402).json({ message: "incorrect password" })
}
return res.status(200).json({ message: "Login Success" })
} catch (error) {
return next(error)
}
}