const express = require('express');
const passport = require('passport');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const schema = require('../helpers/schema');
const router = express.Router();

router.get('/', passport.authenticate('jwt', { session: false }), function(req, res, next) {
  res.json(req.user);
});

router.get('/me', passport.authenticate('jwt', { session: false }), function(req, res, next) {
  res.json(req.user);
});

router.get('/signup', function(req, res, next) {
  const email = req.query.email;
  const password = req.query.password;
  // TODO check if email exist already on the database
  req.validator.validate({ email, password }, schema.signup, (err, valid) => {
    if (valid) {
      const hash = bcrypt.hashSync(password, 10);
      const query = 'INSERT INTO users(email, password) values(${email}, ${hash})';
      req.db.none(query, { email, hash })
        .then(() => {
          const token = jwt.sign({ email }, process.env.JWT_SECRET);
          res.json({ token });
        })
        .catch(err => next(err));
    } else {
      res.status(400).json(req.validator.errorObject(err));
    }
  });
});

router.get('/login', function(req, res, next) {
  const email = req.query.email;
  const password = req.query.password;
  // TODO check if email and password are not empty
  const query = 'SELECT * FROM users WHERE "email" = ${email} LIMIT 1';
  req.db.query(query, { email })
    .then(users => {
      if (!users[0]) {
        res.status(400).json({ email: ['Email does not exist.'] });
      } else if (!bcrypt.compareSync(password, users[0].password)) {
        res.status(400).json({ password: ['Password does not match.'] });
      } else {
        const token = jwt.sign({ email }, process.env.JWT_SECRET);
        res.json({ token });
      }
    })
    .catch(err => next(err));
});

module.exports = router;

@@ -144,22 +144,25 @@
 ;%0Aconst 
-schema
+validator
  = requi
@@ -169,25 +169,17 @@
 re('
-../helpers/schema
+validator
 ');%0A
@@ -475,16 +475,22 @@
 signup',
+ async
  functio
@@ -587,159 +587,488 @@
 ;%0A  
-// TODO check if email exist already on the database%0A  req.validator.validate(%7B email, password %7D, schema.signup, (err, valid) =%3E %7B%0A    if (valid) %7B%0A  
+const query = 'SELECT * FROM users WHERE %22email%22 = $%7Bemail%7D LIMIT 1';%0A  const users = await req.db.query(query, %7B email %7D);%0A  if (users%5B0%5D) %7B%0A    res.status(400).json(%7B email: %5B'Email already exists.'%5D %7D);%0A  %7D else if (!validator.isEmail(email)) %7B%0A    res.status(400).json(%7B email: %5B'Email is not valid.'%5D %7D);%0A  %7D else if (!validator.isLength(password, %7Bmin: 6, max: 128%7D)) %7B%0A    res.status(400).json(%7B password: %5B'Passwords must be between 6 and 128 characters.'%5D %7D);%0A  %7D else %7B%0A
     
@@ -1111,26 +1111,24 @@
 d, 10);%0A    
- 
- 
 const query 
@@ -1197,18 +1197,16 @@
 )';%0A    
-  
 req.db.n
@@ -1236,37 +1236,10 @@
 h %7D)
+;
 %0A
-        .then(() =%3E %7B%0A      
     
@@ -1297,22 +1297,16 @@
 ECRET);%0A
-      
     res.
@@ -1328,135 +1328,9 @@
 ;%0A  
-      %7D)%0A        .catch(err =%3E next(err));%0A    %7D else %7B%0A      res.status(400).json(req.validator.errorObject(err));%0A    %7D%0A  %7D);
+%7D
 %0A%7D);
@@ -1351,16 +1351,22 @@
 /login',
+ async
  functio
@@ -1583,16 +1583,36 @@
 T 1';%0A  
+const users = await 
 req.db.q
@@ -1637,34 +1637,10 @@
 l %7D)
+;
 %0A
-    .then(users =%3E %7B%0A    
   if
@@ -1650,28 +1650,24 @@
 users%5B0%5D) %7B%0A
-    
     res.stat
@@ -1710,36 +1710,32 @@
 ot exist.'%5D %7D);%0A
-    
   %7D else if (!bc
@@ -1783,28 +1783,24 @@
 ord)) %7B%0A    
- 
-   
 res.status(4
@@ -1853,20 +1853,16 @@
 .'%5D %7D);%0A
-    
   %7D else
@@ -1864,20 +1864,16 @@
  else %7B%0A
-    
     cons
@@ -1927,20 +1927,16 @@
 ECRET);%0A
-    
     res.
@@ -1958,50 +1958,9 @@
 ;%0A  
-    %7D%0A    %7D)%0A    .catch(err =%3E next(err));
+%7D
 %0A%7D);

Fix #3. And refactor with await async and `validator`.