diff --git a/server/middleware/authenticate.js b/server/middleware/authenticate.js new file mode 100644 index 0000000..4a96c9a --- /dev/null +++ b/server/middleware/authenticate.js @@ -0,0 +1,19 @@ +const { User } = require("./../models/user"); + +var authenticate = (res, res, next) => { + var token = req.header('x-auth'); + + User.findByToken(token).then((user) => { + if(!user) { + return Promise.reject(); + } + + req.user = user; + req.token = token; + next(); + }).catch(e => { + res.status(400).send(); + }); +}; + +module.exports = { authenticate }; \ No newline at end of file