user.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const express = require('express');
  2. const router = express.Router();
  3. const bodyParser = require('body-parser');
  4. const users = require('../models/users');
  5. var Constants = require('../helpers/Constants.js');
  6. var UserService = require('../services/UserService');
  7. var FilterService = require('../services/FilterService');
  8. var RoutingService = require('../services/RoutingService')(router, 'Users');
  9. //const session = require('express-session');
  10. const bcrypt = require('bcrypt');
  11. const saltRounds = 10;
  12. var knex = require('../models/database');
  13. router.get('/me',function(req,res){
  14. if(req.user.role !== 0) {
  15. res.send(UserService.present(req.user));
  16. return;
  17. }
  18. res.status(401).send("Invalid token");
  19. });
  20. router.post('/login',function(req,res,next){
  21. var flag = 1;
  22. var hash;
  23. if (!req.body.email || ! req.body.password){
  24. res.status(400).send({error : true , message:"Please provide both username and password"});
  25. return;
  26. }
  27. console.log(req.body)
  28. var user = {
  29. email:req.body.email,
  30. password:req.body.password
  31. }
  32. users.Login(user).
  33. then((token) => res.status(200).send({ token: token, error:false, message:"login succesful"})).
  34. catch((err) => {
  35. console.log(err);
  36. res.status(401).send({ err: err });
  37. });
  38. });
  39. router.post('/register',function(req,res){
  40. if(!req.body.email || !req.body.username || !req.body.password){
  41. res.status(400).send({error : true , message:"Please provide all the required fields"});
  42. }
  43. //console.log("here")
  44. var user={
  45. username:req.body.username,
  46. email:req.body.email,
  47. password:req.body.password,
  48. role:1
  49. //created: today.toISOString();
  50. }
  51. users.Register(user).
  52. then((user) => res.send(user)).
  53. catch((err) => {
  54. res.status(400).send(err);
  55. });
  56. });
  57. // Creates Search URL like the commented underneath
  58. //DONT// router.post('/search/:page(\\d+)/:term', search);
  59. //DONT// router.post('/search/:term', search);
  60. RoutingService.search( 10, UserService.present);
  61. RoutingService.crud();
  62. module.exports = router;