{"size": 822, "ext": "js", "max_stars_repo_path": "server/controllers/auth.controller.js", "max_stars_repo_name": "iandeherdt/node-template", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/controllers/auth.controller.js", "max_issues_repo_name": "iandeherdt/node-template", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/controllers/auth.controller.js", "max_forks_repo_name": "iandeherdt/node-template", "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": 28.3448275862, "max_line_length": 132, "alphanum_fraction": 0.6812652068}
const authService = require('../services/auth.service');
const authHelpers = require('./../helpers/auth.helpers');

async function login(req, res, next) {
  try {
    const { username, password } = req.body;
    const result = await authService.login(username, password);
    return res.json(result);
  } catch (ex) {
    return next(ex);
  }
}

function redirectFacebook(req, res, next) {
  try {
    const userInfo = authHelpers.createTokenInfo(req.user);
    const token = authHelpers.generateAccessToken(userInfo);
    // Successful authentication, redirect to success page to pass token.
    return res.redirect(`/authComplete?token=${token}&id=${req.user.id}&user=${req.user.name}&registered=${!!req.user.registered}`);
  } catch (ex) {
    return next(ex);
  }
}

module.exports = {
  login,
  redirectFacebook,
};
