title: JWT error with placement of jwt.sign code
body: <p>I'm working on creating a authentication system and am trying to figure out how to use JWT. I'm using Node, express, passport and MYSQL. I tried creating the "var token" after the db.sync() method but that would only throw a error but then it works if i put the "var token" before db.sync() why is this? And should i do it this way? Thanks in advance! </p>
  
  <pre><code>var jwt = require('jsonwebtoken');
  
  var config = require('../config'),
      db = require('../services/database'),
      User = require('../models/user'),
      Organisation = require('../models/organisation'),
      Event = require('../models/event');
  
  // The authentication controller.
  var AuthController = {};
  
  // Register a user.
  AuthController.signUp = function(req, res) {
      if(!req.body.username || !req.body.password || !req.body.email) {
          res.json({ message: 'Please provide a username and a password.' });
      } else {
        var token = jwt.sign({ username: req.body.username }, config.keys.secret, { expiresIn: '30m' });  
          db.sync().then(function() {
  
              var newUser = {
                  username: req.body.username,
                  password: req.body.password,
                  email: req.body.email,
                  temporarytoken: token
              };
              console.log(newUser.temporarytoken);
              return User.create(newUser).then(function() {
                  res.status(201).json({ message: 'Account created!' });
              });
          }).catch(function(error) {
              res.status(403).json({ message: 'Username already exists!' });
          });
      }
  }
  </code></pre>