Create a login api in this login.js file. This is the creation model module.exports = {…
- User
Create a login api in this login.js file. This is the creation model module.exports = { async login(req, res) { const { email, password } = req.body;
codetry { let user = await Controler.findOne({ email }); if (!user) { return res.status(400).json({ msg: "No record found" }); } let isValidPassword = await bcrypt.compare(password, user.password); if (!isValidPassword) { return res.status(400).json({ msg: "Authentication faild" }); } await Controler.findOneAndUpdate( { _id: user._id }, { updatedAt: new Date() } ); return res.status(200).json({ status: 200, msg: "Login success" }); } catch (error) { return res.json({ status: "error", msg: "Internal server error" }); }}, } . You only provide controller api only
- Assistant
Certainly! Below is the login API controller code you provided, refactored a bit for clarity and adding the necessary dependencies:
javascript