{"size": 961, "ext": "js", "max_stars_repo_path": "routes/api/auth.js", "max_stars_repo_name": "AxelDavid45/expressjs-course", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/api/auth.js", "max_issues_repo_name": "AxelDavid45/expressjs-course", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/api/auth.js", "max_forks_repo_name": "AxelDavid45/expressjs-course", "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.6944444444, "max_line_length": 75, "alphanum_fraction": 0.5858480749}
'use strict'
const express = require('express')
const router = express.Router()
const jwt = require('jsonwebtoken')
const passport = require('passport')
const boom = require('@hapi/boom')
const {
  auth: { jwtSecret },
} = require('../../config')
// basic strategy
require('../../auth/strategies/basic')

router.post('/token', async function (req, res, next) {
  passport.authenticate('basic', { session: false }, function (err, user) {
    try {
      if (err || !user) {
        return next(boom.unauthorized())
      }
      req.login(user, { session: false }, async function (err) {
        if (err) {
          return next(err)
        }
        const payload = { sub: user.username, email: user.email }
        const token = jwt.sign(payload, jwtSecret, {
          expiresIn: '15m',
        })
        return res.status(200).json({ access_token: token })
      })
    } catch (err) {
      next(err)
    }
  })(req, res,next)
})

module.exports = router
