users.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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('/', function(req, res, next) {
  7. res.send('respond with a resource');
  8. });
  9. /* post users login. */
  10. router.post('/login', function(req, res, next) {
  11. const params = {...req.body};
  12. const {
  13. username,
  14. password
  15. } = params;
  16. console.log(username);
  17. Users.login({username, password}).
  18. then((user) => res.send(user)).
  19. catch((e) => {
  20. console.log(e);
  21. res.status(400);
  22. res.send(JSONError("Bad Credentials"));
  23. });
  24. });
  25. /* post users register. */
  26. router.post('/register', function(req, res, next) {
  27. const params = {...req.body};
  28. const {
  29. username,
  30. password
  31. } = params;
  32. console.log(username);
  33. Users.register({username, password}).
  34. then((user) => res.send(user)).
  35. catch((e) => {
  36. console.log(e);
  37. res.status(400);
  38. res.send(JSONError("Bad Credentials"));
  39. });
  40. });
  41. module.exports = router;