diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-04-04 10:38:21 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-04-04 10:38:21 -0400 |
| commit | a75b2491758a451283e2a373bb3fbe886520b345 (patch) | |
| tree | b45d3c6e0685106c0b08049687244fbc8169b27a /backend/src/index.js | |
| parent | 00df450c84f126c75c4adcd322947f8b77a589d9 (diff) | |
force network policy
Diffstat (limited to 'backend/src/index.js')
| -rw-r--r-- | backend/src/index.js | 25 |
1 files changed, 23 insertions, 2 deletions
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(); }); |
