{"size": 580, "ext": "js", "max_stars_repo_path": "src/auth/middleware/basic.js", "max_stars_repo_name": "Saynka/bearer-auth", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/auth/middleware/basic.js", "max_issues_repo_name": "Saynka/bearer-auth", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/auth/middleware/basic.js", "max_forks_repo_name": "Saynka/bearer-auth", "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": 23.2, "max_line_length": 59, "alphanum_fraction": 0.5793103448}
'use strict';

const base64 = require('base-64');
const User = require('../models/users.js');

module.exports = async (req, res, next) => {

  if (!req.headers.authorization) { next('invalid'); }

  let [basic, info] = req.headers.authorization.split(' ');
  if (basic === 'Bearer') {
    next();
  }
  else {
    let [user, pass] = base64.decode(info).split(':');
    console.log(user, pass);
    try {
      req.user = await User.authenticateBasic(user, pass)
      console.log(req.user);
      next();
    } catch (e) {
      res.status(403).send('Invalid Login');
    }
  }
};