User: Task: "Create Login Api for User"
    Description: "Generate a Node.js Login Api with with given dependenceis"
    Requirements:
    - Database: mongoose
    - Authentication: None specified
    - Output needed: Return only the function code as specified in dependencies
    - Dependencies: Create a function for user login. Export the function: exports.loginUser = async () => {...}, 
Accept parameters: email, password (in the request body)., 
Validate required fields: Ensure both email and password are provided., 
Use the User model to find the user by email., 
If user not found, respond with an error message indicating invalid credentials., 
Compare the provided password with the hashed password using bcrypt., 
Respond with the user details (excluding password)., 
Handle errors like invalid email, incorrect password, or database errors.
    - Previous Code 
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
// Assuming that you have a User model defined in your Mongoose models
const User = require('../models/User'); // Adjust the path as necessary

exports.registerUser = async (req, res) => {
    try {
        const { name, email, password } = req.body;
        
        // Validate required fields
        if (!name || !email || !password) {
            return res.status(400).json({ error: 'Name, email, and password are required.' });
        }

        // Check if the email is already registered
        const existingUser = await User.findOne({ email });
        if (existingUser) {
            return res.status(400).json({ error: 'Email is already registered.' });
        }

        // Hash the password before saving
        const saltRounds = 10;
        const hashedPassword = await bcrypt.hash(password, saltRounds);

        // Create a new user with status active
        const newUser = new User({
            name,
            email,
            password: hashedPassword,
            status: 'active'
        });

        // Save the new user to the database
        await newUser.save();

        // Respond with a success message containing user details (excluding password)
        res.status(201).json({ message: 'User registered successfully.', user: {
            id: newUser._id,
            name: newUser.name,
            email: newUser.email,
            status: newUser.status
        }});
    } catch (error) {
        // Handle errors like invalid email format or database issues
        res.status(500).json({ error: 'An error occurred while registering the user.' });
    }
};

// Assuming that you have a User model defined in your Mongoose models
// const User = require('./models/User'); // Adjust the path as necessary

exports.loginUser = async (req, res) => {
    try {
        const { email, password } = req.body;

        // Validate required fields
        if (!email || !password) {
            return res.status(400).json({ error: 'Email and password are required.' });
        }

        // Find user by email
        const user = await User.findOne({ email });
        if (!user) {
            // User not found
            return res.status(401).json({ error: 'Invalid credentials.' });
        }

        // Compare provided password with the stored hashed password
        const isPasswordValid = await bcrypt.compare(password, user.password);
        if (!isPasswordValid) {
            // Incorrect password
            return res.status(401).json({ error: 'Invalid credentials.' });
        }

        // Respond with user details, excluding the password
        res.status(200).json({
            message: 'Login successful.',
            user: {
                id: user._id,
                name: user.name,
                email: user.email,
                status: user.status
            }
        });
    } catch (error) {
        // Handle database or other errors
        res.status(500).json({ error: 'An error occurred during login.' });
    }
};

    - Supporting data 
    - Updating File undefined
    
    Please generate the code for this task, following the instructions and including comments for clarity.
    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 formate
```dependencies
const bcrypt = require('bcrypt');
```

Ensure this format is consistently followed in responses.
    
User: 


    
Assistant: