text: const express = require('express');
  const router = express.Router();
  
  // A library to help you hash passwords
  const bcrypt = require('bcryptjs');
  
  // Verify and generate JWT
  const jwt = require('jsonwebtoken');
  
  // Model
  const User = require('../models/User');
  
  /* GET home page. */
  router.get('/', (req, res, next) => {
    res.render('index', { title: 'Express' });
  });
  
  router.post('/register', (req, res, next) => {
    const { username, password } = req.body;
  
    bcrypt.hash(password, 10).then(hash => {
      const user = new User({
        username,
        password: hash
      });
  
      const promise = user.save();
  
      promise
        .then(data => {
          res.json(data);
        })
        .catch(err => {
          res.json(err);
        });
    });
  });
  
  router.post('/authenticate', (req, res, next) => {
    const { username, password } = req.body;
  
    User.findOne(
      {
        username
      },
      (err, user) => {
        if (err) {
          throw err;
        }
  
        if (!user) {
          res.json({
            status: false,
            message: 'Authentication failed, user not found.'
          });
        } else {
          bcrypt.compare(password, user.password).then(result => {
            if (!result) {
              res.json({
                status: false,
                message: 'Authentication failed, wrong password.'
              });
            } else {
              const payload = {
                username
              };
              const token = jwt.sign(payload, req.app.get('api_secret_key'), {
                expiresIn: 720 // 12 Hours
              });
              res.json({
                status: true,
                token
              });
            }
          });
        }
      }
    );
  });
  
  module.exports = router;
  