{"size": 1334, "ext": "js", "max_stars_repo_path": "BuyersOrgApp/route-functions/login.js", "max_stars_repo_name": "Sumon2017/hyperledger-fabric-lc-project", "max_stars_repo_stars_event_min_datetime": "2021-04-28T05:47:40.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-28T05:47:40.000Z", "max_issues_repo_path": "BuyersOrgApp/route-functions/login.js", "max_issues_repo_name": "Sumon2017/hyperledger-fabric-lc-project", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "BuyersOrgApp/route-functions/login.js", "max_forks_repo_name": "Sumon2017/hyperledger-fabric-lc-project", "max_forks_repo_forks_event_min_datetime": "2021-12-23T19:47:48.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-23T19:47:48.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 24.7037037037, "max_line_length": 107, "alphanum_fraction": 0.4977511244}
const path = require('path');
const bcrypt = require('bcrypt');
var jwt = require('jsonwebtoken');
const requestIp = require('request-ip');
require('dotenv').config();
const users = require(path.join(__dirname,'..','db-schemas','users'));

const login = async (req,res) => {

    const cookieConfig = {
        httpOnly: true,
        //secure: true,
        signed: true
    };

    const { email , password } = req.body;

    let model = await users.find({email});

    if(model.length==0){
        res.json({
            msg : 'no account with this email'
        });
        return ;
    }

    model=model[0];

    const username = model.username;

    bcrypt.compare(password, model.password).then(function(result) {
        if(result){
            const ip = requestIp.getClientIp(req);
            jwt.sign({username,ip}, process.env.SERVER_SECRET , { expiresIn: '1h' }, async (err,token) => {
                if(err){
                    console.log(err);
                    return ;
                }
                res.cookie('lcprojecto', token , cookieConfig);
                res.json({
                    msg : 'you are logged in'
                });
            });
        }
        else{
            res.json({
                msg : 'incorrect password'
            });
        }
    });

}

module.exports=login;