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 login api function as exported function it's self. function mode :-  exports.functionname = async () =>{...} , 
Accept two parmas request and response, 
Accept two creidentional from User one email and passowrd, 
User cridentional should validated by joi, , 
Api should build inder try catch method for error handeling., 
For compare passwords use bacrypt lirbary, , 
Data should be saved in User database., 
Use redis caching to store logged users. Storing key `logUser`, 
Response should send back in Json formate with 'status' code of 200
    - Previous Code 
// Entry point for Register Api for User
const bcrypt = require('bcrypt');
const Joi = require('joi');
const User = require('../models/User.js');

// Function to handle user registration
exports.registerUser = async (req, res) => {
const { email, password } = req.body;

// Validate user input
const schema = Joi.object({
email: Joi.string().email().required(),
password: Joi.string().min(6).required()
});

try {
const validation = await schema.validate({ email, password });
if (validation.error) {
throw new Error(validation.error.details[0].message);
}

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

// Create a new user
const newUser = new User({
email,
password: hashedPassword
});

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

// Send response
res.status(200).json({ status: 200, message: 'User registered successfully' });

} catch (error) {
// Handle errors
res.status(400).json({ status: 400, message: error.message });
}
};

// Import necessary dependencies
const Joi = require('joi');
const bcrypt = require('bcrypt');
const User = require('./models/User');
const redis = require('redis');

// Create Redis Client
const client = redis.createClient();

// Function to handle user login
exports.loginUser = async (req, res) => {
    const { email, password } = req.body;

    // Validate user input using Joi
    const schema = Joi.object({
        email: Joi.string().email().required(),
        password: Joi.string().min(6).required()
    });

    try {
        const validation = await schema.validate({ email, password });
        if (validation.error) {
            throw new Error(validation.error.details[0].message);
        }

        // Check if user exists in the database
        const existingUser = await User.findOne({ email });
        if (!existingUser) {
            throw new Error('User not found. Please register first.');
        }

        // Compare passwords using bcrypt
        const passwordMatch = await bcrypt.compare(password, existingUser.password);
        if (!passwordMatch) {
            throw new Error('Invalid password. Please try again.');
        }

        // Store logged-in user in Redis cache
        client.set('loggedInUser', email);

        // Send response
        res.status(200).json({ status: 200, message: 'User logged in successfully' });

    } catch (error) {
        // Handle errors
        res.status(400).json({ status: 400, message: error.message });
    }
};

// Import necessary dependencies if not already imported in task 1
const Joi = require('joi');
const bcrypt = require('bcrypt');
const User = require('./models/User');

// Function to handle user login
exports.loginUser = async (req, res) => {
    const { email, password } = req.body;
    
    try {
        // Validate user input
        const schema = Joi.object({
            email: Joi.string().email().required(),
            password: Joi.string().min(6).required()
        });
        
        const validation = await schema.validate({ email, password });
        if (validation.error) {
            throw new Error(validation.error.details[0].message);
        }

        // Check if user exists in the database (You might want to implement this logic)

        // Compare passwords using bcrypt
        // Assuming userExists is the retrieved user from the database
        const passwordMatch = await bcrypt.compare(password, userExists.password);
        if (!passwordMatch) {
            throw new Error('Invalid credentials');
        }

        // Store logged-in user in Redis cache
        // Assuming RedisClient is the Redis client instance
        RedisClient.set('loggedInUser', email);

        // Send response
        res.status(200).json({ status: 200, message: 'Login successful' });
        
    } catch (error) {
        // Handle errors
        res.status(400).json({ status: 400, message: error.message });
    }
};

    - 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: