FACN3 / spend500

week 7 project
0 stars 1 forks source link

Rethink handleAuth #38

Open finnhodgkin opened 6 years ago

finnhodgkin commented 6 years ago

There are a few things that I'd change about this file:

jwt.verify(token, 'secret');

Something like:

const jwt = require('jsonwebtoken');
const cookie = require('cookie');

const handleAuth = (cookie, callback) => {
  if (!cookie) {
    return callback(new Error('No cookie');
  } 
  const token = cookie.parse(cookie).token;
  jwt.verify(token, process.env.JWT_SECRET, callback);
}

module.exports = handleAuth;

This would mean changing the way the function is called in your routers also (the routes would need to be wrapped in the callback). #

MynahMarie commented 6 years ago

Good points but why would we need a callback to the handleAuth function?