text: const express = require("express");
  const router = express.Router();
  const bcrypt = require("bcryptjs");
  
  const signToken = require("../middleware/signToken");
  
  //Pull in knex helper models
  const AuthDB = require("../models/AuthModels.js");
  
  router.post("/register", (req, res) => {
    let user = req.body;
    const hash = bcrypt.hashSync(user.password, 12);
    user.password = hash;
  
    AuthDB.add(user)
      .then(saved => {
        res.status(201).json(saved);
      })
      .catch(err => {
        res.status(500).json(err);
      });
  });
  
  router.post("/login", (req, res) => {
    let { username, password } = req.body;
  
    AuthDB.getBy({username})
      .first()
      .then(user => {
        if (user && bcrypt.compareSync(password, user.password)) {
          const token = signToken(user);
          res.status(201).json({
            message: `Welcome ${user.username}`,
            token,
          });
        } else {
          res.status(401).json({ message: "Invalid Credentials" });
        }
      })
      .catch(err => {
        res.status(500).json({ message: "Error logging in", err });
      });
  });
  
  module.exports = router;
  