{"size": 1383, "ext": "js", "max_stars_repo_path": "backend/routes/login.js", "max_stars_repo_name": "vasusharma7/distrideochat", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "backend/routes/login.js", "max_issues_repo_name": "vasusharma7/distrideochat", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "backend/routes/login.js", "max_forks_repo_name": "vasusharma7/distrideochat", "max_forks_repo_forks_event_min_datetime": "2020-07-08T19:44:54.000Z", "max_forks_repo_forks_event_max_datetime": "2020-07-08T19:44:54.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 27.66, "max_line_length": 75, "alphanum_fraction": 0.5249457701}
const router = require('express').Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const config = require('config');
const userLogins = require('../models/UserLogin.model');

router.post('/', async (req, res) => {
  const username = req.body.username;
  const password = req.body.password;
  userLogins.findOne({ username: username }, async function (err, user) {
    if (err) {
      return res.status(400).json({ err: 'Invalid Credentials' });
    }
    if (!user) {
      return res.status(400).json({ err: 'Invalid Credentials' });
    }
    console.log(user);
    if (password === user.password) {
      //       return res.status(200).json({ msg: "Correct Credentials" });
      const payload = {
        user: {
          id: user.username
        }
      };
      try {
        jwt.sign(
          payload,
          config.get('jwtSecret'),
          { expiresIn: 3600 },
          (err, token) => {
            if (err) throw err;
            res.status(200).json({
              token,
              user_details: {
                userId: user.username
              }
            });
          }
        );
      } catch (e) {
        res.status(400).json({ err: e });
      }
    } else {
      return res.status(400).json({ err: 'Invalid Credentials' });
    }
  });
});
module.exports = router;
