users.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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) => res.send(user)).
  20. catch((e) => {
  21. console.log(e);
  22. res.status(400);
  23. res.send(JSONError("Bad Credentials"));
  24. });
  25. });
  26. /* post users register. */
  27. router.post('/register', function(req, res, next) {
  28. const params = {...req.body};
  29. const {
  30. username,
  31. password
  32. } = params;
  33. console.log(username);
  34. Users.register({username, password}).
  35. then((user) => res.send(user)).
  36. catch((e) => {
  37. console.log(e);
  38. res.status(400);
  39. res.send(JSONError("Bad Credentials"));
  40. });
  41. });
  42. module.exports = router;