users.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var express = require('express');
  2. var router = express.Router();
  3. let JSONError = require('../helpers').JSONError;
  4. let Users = require('../database').Repositories.Users;
  5. /* GET users listing. */
  6. router.get('/me', function(req, res, next) {
  7. console.log(req.user);
  8. res.send(req.user);
  9. });
  10. /* post users login. */
  11. router.post('/login', function(req, res, next) {
  12. const params = {...req.body};
  13. const {
  14. username,
  15. password
  16. } = params;
  17. console.log(username);
  18. Users.login({username, password}).
  19. then((user) => {
  20. let {
  21. username,
  22. token
  23. } = user;
  24. res.send({username, token});
  25. }).
  26. catch((e) => {
  27. console.log(e);
  28. res.status(400);
  29. res.send(JSONError("Bad Credentials"));
  30. });
  31. });
  32. /* post users register. */
  33. router.post('/register', function(req, res, next) {
  34. const params = {...req.body};
  35. const {
  36. username,
  37. password
  38. } = params;
  39. console.log(username);
  40. Users.register({username, password}).
  41. then((user) => res.send(user)).
  42. catch((e) => {
  43. console.log(e);
  44. res.status(400);
  45. res.send(JSONError("Bad Credentials"));
  46. });
  47. });
  48. module.exports = router;