profile.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const express = require('express');
  2. const router = express.Router();
  3. var UserService = require('../services/UserService');
  4. const knex = require('../models/database');
  5. const nodemailer = require('nodemailer');
  6. const MailService = require('../services/MailService');
  7. router.get('/',(req,res) => {
  8. //console.log("first here")
  9. let user ={};
  10. if(!req.headers.token){
  11. res.sendStatus(403);
  12. //console.log("then here")
  13. }
  14. else {
  15. return UserService.getByToken(req.headers.token)
  16. .then((data)=>{
  17. user = UserService.present(data);
  18. return res.status(200).send(user);
  19. })
  20. .catch((e)=>{
  21. return res.status(403).send(e);
  22. })
  23. }
  24. });
  25. router.post('/',(req,res) => {
  26. let user = {};
  27. if(!req.headers.token){
  28. res.sendStatus(403);
  29. }
  30. else{
  31. user = UserService.Change(req.user,req.body)
  32. knex('Users').where('token',req.headers.token).update({
  33. username:user.username,
  34. email:user.email
  35. }).then((data)=>{
  36. return res.sendStatus(200);
  37. }).catch((e)=>{
  38. return res.status(403).send(e);
  39. })
  40. }
  41. })
  42. router.post('/restore',(req,res)=>{
  43. console.log(req);
  44. let subject = "Restore mail";
  45. let body = "testing server";
  46. console.log(req.body);
  47. //missing some work
  48. //we must use sender mail to restore account
  49. MailService.prepare(req.body.email,subject,body)
  50. .then((options)=>MailService.sendMail(options))
  51. .then((data)=>{
  52. return res.sendStatus(200);
  53. })
  54. .catch((e)=>{
  55. return res.sendStatus(403);
  56. })
  57. })
  58. module.exports = router;