{"size": 1127, "ext": "js", "max_stars_repo_path": "src/routers/auth-routes.js", "max_stars_repo_name": "Ahmad-AbuOsbeh/talkversity-backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/routers/auth-routes.js", "max_issues_repo_name": "Ahmad-AbuOsbeh/talkversity-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/routers/auth-routes.js", "max_forks_repo_name": "Ahmad-AbuOsbeh/talkversity-backend", "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.4791666667, "max_line_length": 71, "alphanum_fraction": 0.6539485359}
'use strict';

const express = require('express');

// use express Router
const authRouter = express.Router();

// import user model
const User = require('../models/usersModel');

// import middlewares
const basicAuth = require('../middlewares/basicAuth');
const bearerAuth = require('../middlewares/bearerAuth');
const permissions = require('../middlewares/acl');

// "/signup" route, and its callback function
authRouter.post('/signup', async (req, res, next) => {
  try {
    // create new user object
    let user = new User(req.body);

    // save user object
    const userRecord = await user.save();

    // get the user object and its token
    const output = {
      user: userRecord,
      token: userRecord.token,
    };

    // return the object and token to the client
    res.status(201).send(output);
  } catch (e) {
    next(e.message);
  }
});

// "/signin" route, and its callback function and basic auth middleware
authRouter.post('/signin', basicAuth, (req, res, next) => {
  const user = {
    user: req.user,
    token: req.user.token,
  };
  res.status(200).send(user);
});

module.exports = authRouter;
