diff options
| author | Randy Ridge <randyridge@gmail.com> | 2018-09-14 20:01:25 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-09-14 20:01:25 -0400 |
| commit | 908180c77cd38011eeedcae949613da2a3391e5e (patch) | |
| tree | b59bf1aae819a04d453cde72f6e601f5561a740f /finished-application/backend/src/index.js | |
| parent | 1f6667d233a4a2e9df977204d83a8f749c050c75 (diff) | |
| parent | b3bebda57ba187b7fa10398054b54c05d3fd3555 (diff) | |
Merge branch 'master' into randyridge/our
Diffstat (limited to 'finished-application/backend/src/index.js')
| -rw-r--r-- | finished-application/backend/src/index.js | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/finished-application/backend/src/index.js b/finished-application/backend/src/index.js index 2a1b433..804d6d6 100644 --- a/finished-application/backend/src/index.js +++ b/finished-application/backend/src/index.js @@ -1,30 +1,33 @@ -/* eslint-disable */ -require('dotenv').config({ path: 'variables.env' }); -/* eslint-enable */ +const cookieParser = require('cookie-parser'); const jwt = require('jsonwebtoken'); + +require('dotenv').config({ path: 'variables.env' }); const createServer = require('./createServer'); const db = require('./db'); -const cookieParser = require('cookie-parser'); const server = createServer(); server.express.use(cookieParser()); +// decode the JWT so we can get the user Id on each request server.express.use((req, res, next) => { const { token } = req.cookies; if (token) { const { userId } = jwt.verify(token, process.env.APP_SECRET); + // put the userId onto the req for future requests to access req.userId = userId; } next(); }); -// 2. Get User from their ID +// 2. Create a middleware that populates the user on each request + server.express.use(async (req, res, next) => { + // if they aren't logged in, skip this if (!req.userId) return next(); const user = await db.query.user( { where: { id: req.userId } }, - `{ id, permissions, email, name }` + '{ id, permissions, email, name }' ); req.user = user; next(); @@ -36,9 +39,8 @@ server.start( credentials: true, origin: process.env.FRONTEND_URL, }, - port: process.env.PORT, }, deets => { - console.log(`Server is running on http://localhost:${deets.port}`); + console.log(`Server is now running on port http://localhost:${deets.port}`); } ); |
