From a75b2491758a451283e2a373bb3fbe886520b345 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Wed, 4 Apr 2018 10:38:21 -0400 Subject: force network policy --- backend/src/index.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'backend/src/index.js') diff --git a/backend/src/index.js b/backend/src/index.js index df5eb29..4f98c52 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -1,9 +1,30 @@ +const jwt = require('jsonwebtoken'); const createServer = require('./createServer'); const server = createServer(); -server.express.use((req, res, next, db) => { - console.log('MIDDLEWARE!'); +// 1. Check JWT +server.express.use((req, res, next) => { + const Authorization = req.get('Authorization'); + if (Authorization) { + const token = Authorization.replace('Bearer ', ''); + const { userId } = jwt.verify(token, process.env.APP_SECRET); + req.userId = userId; + } + next(); +}); + +// 2. Get User from their ID +server.express.use(async (req, res, next) => { + if (!req.userId) return next(); + const user = await server.context().db.query.user( + { where: { id: req.userId } }, + ` + { id, permissions, email, name } + ` + ); + req.user = user; + console.log(req.user); next(); }); -- cgit v1.3