admin.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Controller for /admin resources */
  2. var jwt = require('jsonwebtoken');
  3. var ip = require('ip');
  4. var config = require('../config');
  5. var Admin = require('../models/Admin');
  6. var logger = require('../utils/logger');
  7. var passCrypt = require('../utils/passCrypt');
  8. var validation = require('../validation');
  9. /**
  10. * Function for creating an admin in the DB.
  11. * @param req The request object sent over from the route.
  12. * @param res The response object sent over from the route.
  13. */
  14. exports.createAdmin = function (req, res) {
  15. var username = req.body.username;
  16. var password = req.body.password;
  17. //Check that a valid username/password was supplied
  18. var valid = validation.validateUserPass(res, {username: username, password: password});
  19. //If valid
  20. if (valid) {
  21. //Make a new Admin obj with the given creds
  22. var admin = new Admin({
  23. _id: username,
  24. username: username,
  25. //Hash the password using the bcrypt util
  26. password: passCrypt.generateHash(password)
  27. });
  28. //Save the new admin in the DB
  29. admin.save(function (err) {
  30. if (err) {
  31. logger.info("[" + ip.address() + "] New admin creation failed. Admin username= " + username);
  32. res.status(422).send({message: "New admin creation failed. Admin username= " + username, errors: err});
  33. }
  34. else {
  35. logger.info("[" + ip.address() + "] New admin creation succeeded. Admin username= " + username);
  36. res.status(201).send({added: true});
  37. }
  38. });
  39. }
  40. };
  41. /**
  42. * Function for admin login
  43. * @param req The request object sent over from the route.
  44. * @param res The response object sent over from the route.
  45. */
  46. exports.login = function (req, res) {
  47. //Find the admin in the DB that matches the supplied username
  48. Admin.findOne({username: req.body.username}, function (err, admin) {
  49. //Following if statements check for general errors/bad username
  50. if (err)
  51. res.status(400).send({errors: err});
  52. if (!admin) {
  53. res.status(404).send({success: false, errors: "Authentication failed. User not found."});
  54. }
  55. //Otherwise, a matching admin was found
  56. else if (admin) {
  57. //If the passwords match (i.e. user gave the correct password)
  58. if (passCrypt.checkPass(req.body.password, admin.password)) {
  59. //Create a token
  60. var token = jwt.sign({username: admin.username}, config.secretKey, {
  61. expiresIn: "1d"
  62. });
  63. res.status(200).send({success: true, token: token});
  64. }
  65. //Passwords don't match (i.e. user gave the WRONG password)
  66. else {
  67. res.status(401).send({success: false, errors: "Authentication failed. Incorrect password."});
  68. }
  69. }
  70. });
  71. };