{"size": 1379, "ext": "js", "max_stars_repo_path": "routes/token.js", "max_stars_repo_name": "vinhla92/xyz", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/token.js", "max_issues_repo_name": "vinhla92/xyz", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/token.js", "max_forks_repo_name": "vinhla92/xyz", "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": 21.8888888889, "max_line_length": 75, "alphanum_fraction": 0.5844815083}
const env = require('../mod/env');

module.exports = { route, view };

function route(fastify) {
    
  fastify.route({
    method: 'GET',
    url: '/token',
    preValidation: fastify.auth([
      (req, res, next) => fastify.authToken(req, res, next, {
        login: true
      })
    ]),
    handler: view
  });

  fastify.route({
    method: 'POST',
    url: '/token',
    handler: (req, res) => fastify.login.post(req, res, {
      view: (req, res, token) => view(req, res, token, fastify)
    })
  });

};

async function view(req, res, token, fastify) {

  // Get user from ACL.
  var rows = await env.acl(`
    SELECT * FROM acl_schema.acl_table
    WHERE lower(email) = lower($1);`,
  [token.email]);
    
  if (rows.err) return res.redirect(env.path + '/login?msg=badconfig');
  
  const user = rows[0];
  
  if (!user
    || !user.verified
    || !user.approved
    || user.blocked) return res.code(401).send(new Error('Invalid token'));
  
  // Create signed api_token
  const api_token = fastify.jwt.sign({
    email: user.email,
    roles: [],
    api: true
  });
  
    // Store api_token in ACL.
  var rows = await env.acl(`
    UPDATE acl_schema.acl_table SET api = '${api_token}'
    WHERE lower(email) = lower($1);`,
  [user.email]);
    
  if (rows.err) return res.redirect(env.path + '/login?msg=badconfig');
  
  // Send ACL token.
  res.send(api_token);

}