From b1600adec47a04f60e1da07d94a3ed3906ff5aee Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 14 Jun 2018 16:44:21 -0400 Subject: starter files --- finished-application/backend/src/index.js | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 finished-application/backend/src/index.js (limited to 'finished-application/backend/src/index.js') diff --git a/finished-application/backend/src/index.js b/finished-application/backend/src/index.js new file mode 100644 index 0000000..94911ec --- /dev/null +++ b/finished-application/backend/src/index.js @@ -0,0 +1,41 @@ +/* eslint-disable */ +require('dotenv').config({ path: 'variables.env' }); +/* eslint-enable */ +const jwt = require('jsonwebtoken'); +const createServer = require('./createServer'); +const db = require('./db'); +const cookieParser = require('cookie-parser'); + +const server = createServer(); + +server.express.use(cookieParser()); + +server.express.use((req, res, next) => { + const { token } = req.cookies; + if (token) { + 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 db.query.user({ where: { id: req.userId } }, `{ id, permissions, email, name }`); + req.user = user; + next(); +}); + +server.start( + { + cors: { + credentials: true, + origin: process.env.FRONTEND_URL, + }, + port: 4444, + }, + deets => { + console.log(`Server is running on http://localhost:${deets.port}`); + } +); -- cgit v1.3