Task Name: "Create User Login Api" Description: "Generate a Node.js User Login Api with…
- User
Task Name: "Create User Login Api" Description: "Generate a Node.js User Login Api with the specified dependencies" Requirements: 1. Database: mongoose 2. Output needed: Return only the function code as specified in dependencies
3. Tasks to implement: Create a User Login Api. Export: exports.userLogin = async () => {...},Accept data from user: email, password (hashed)., Use User schema from ../models/User.js., Verify user credentials and authenticate., If new user, send email verification using sendMail function from ../utils/sendEmail.js., Respond to user with a success message and authentication details., Handle errors like invalid credentials, missing fields, or database issues.
4. Supporting data Please generate the code for above tasks, following the instructions and including comments for clarity and logic for Dependencies must needed. While creating router for specifict file your have to read supporing file data and create for that **Output Formats**:Return dependencies like this
const bcrypt = require('bcrypt');in this formateconst bcrypt = require('bcrypt');Ensure this format is consistently followed in responses.
- Assistant
const bcrypt = require('bcrypt'); const mongoose = require('mongoose'); const jwt = require('jsonwebtoken'); const sendMail = require('../utils/sendEmail.js'); const User = require('../models/User.js');exports.userLogin = async (req, res) => { try { // Destructure email and password from the request body const { email, password } = req.body; // Check if both email and password are provided if (!email || !password) { return res.status(400).json({ message: 'Both email and password are required.' }); } // Find user in the database by email const user = await User.findOne({ email }); // If user is not found, respond with an error if (!user) { return res.status(401).json({ message: 'Invalid credentials.' }); } // Compare provided password with the hashed password in the database const isPasswordCorrect = await bcrypt.compare(password, user.password); // If password is incorrect, respond with an error if (!isPasswordCorrect) { return res.status(401).json({ message: 'Invalid credentials.' }); } // Check if user's email is verified if (!user.isVerified) { // Send email verification if user is not verified await sendMail(user.email, 'Please verify your email.', 'Verification link content'); return res.status(403).json({ message: 'Email verification sent. Please verify your email.' }); } // Generate authentication token const token = jwt.sign({ userId: user._id }, process.env.JWT_SECRET, { expiresIn: '1h', }); // Respond with success message and token return res.status(200).json({ message: 'Login successful.', token, user: { id: user._id, email: user.email, }, }); } catch (error) { // Respond with error message in case of any server/database issues return res.status(500).json({ message: 'An error occurred while logging in.', error: error.message }); } };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.