summaryrefslogtreecommitdiffstats
path: root/backend/src/index.js
blob: 04d13cce9f599c27302dc34bdda20f103561308a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* eslint-disable */
require('dotenv').config({ path: 'variables.env' });
/* eslint-enable */
const jwt = require('jsonwebtoken');
const createServer = require('./createServer');

const server = createServer();

// 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;
  next();
});

server.start({ port: 4444 }, deets => {
  console.log(`Server is running on http://localhost:${deets.port}`);
});