{"size": 1564, "ext": "js", "max_stars_repo_path": "api/routes/auth/login.js", "max_stars_repo_name": "Averek7/Alumni-Portal", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/routes/auth/login.js", "max_issues_repo_name": "Averek7/Alumni-Portal", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/routes/auth/login.js", "max_forks_repo_name": "Averek7/Alumni-Portal", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.5084745763, "max_line_length": 76, "alphanum_fraction": 0.4801790281}
const User = require("../.././models/User");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");

module.exports = async (req, res) => {
    console.log("in login backend");
    const { email, password } = req.body;

    try {
        let user = await User.findOne({ email });

        if (!user) {
            return res.send({
                success: false,
                data: "User is not registered.",
            });
        }

        if (!user.isVerified) {
            return res.send({
                success: false,
                data: "User is not verified yet, check inbox",
            });
        }
        if (!user.isVerifiedByAdmin) {
            return res.send({
                success: false,
                data: "User is not verified by admin yet.",
            });
        }
        const isMatch = await bcrypt.compare(password, user.password);

        if (!isMatch) {
            return res.send({ success: false, data: "Incorrect Password" });
        }

        //IMP
        const payload = {
            user: { id: user._id, email: user.email },
        };

        const token = jwt.sign(payload, process.env.JWT_SECRET, {
            expiresIn: "720h",
        });

        console.log(token);

        return res.send({
            success: true,
            data: "User successfully logged in!",
            token: token,
            user: user,
        });
    } catch (err) {
        console.log(`Error : ${err.message}`);
        return res.send({ success: false, data: "Server Error" });
    }
};
