Guest User

Untitled

a guest
Mar 4th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. router.post('/signup', async (req, res, next) => {
  2. if (validUser(req.body)) {
  3. const user = await User.getOneByEmail(req.body.email);
  4. if (!user) {
  5. const hash = await bcrypt.hash(req.body.password, 5);
  6. const user = {
  7. email: req.body.email,
  8. password: hash,
  9. created_at: new Date()
  10. };
  11.  
  12. const id = User.create(user);
  13.  
  14. jwt.sign({
  15. id
  16. }, process.env.TOKEN_SECRET, { expiresIn: '1h' }, (err, token) => {
  17. res.json({
  18. id,
  19. token,
  20. message: 'ok'
  21. })
  22. });
  23. } else {
  24. next(new Error('Email in use'))
  25. }
  26. } else {
  27. next(new Error('Invalid user'))
  28. }
  29. });
Add Comment
Please, Sign In to add comment