aboutsummaryrefslogtreecommitdiffstats
path: root/login-service/src/index.js
blob: 0de5931137b7b95bf716f054fed97591418ab76f (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
const express = require('express');
var jwt = require('jsonwebtoken');

const app = express();
app.use(express.json());
app.use((req, res, next) => {
  res.set('Access-Control-Allow-Origin', '*');
  res.set('Access-Control-Allow-Headers', '*');
  next();
});

const port = 4000;

const JWT_SECRET = process.env.JWT_SECRET;

const loginWithUsernameAndPassword = (username, password) => {
  // TODO
  const now = Math.round(new Date().getTime() / 1000);

  return {
    role: 'todo_user',
    exp: now + 30 * 60, // 30 minutes
  };
};

app.get('/', (req, res) => res.send('Login service API'));
app.post('/login', (req, res) => {
  const payload = req.body;
  const { username, password } = payload;
  const claims = loginWithUsernameAndPassword(username, password);
  const signedJWT = jwt.sign(claims, JWT_SECRET);
  res.json({ jwt: signedJWT });
});

app.listen(port, () => console.log(`Listening at http://localhost:${port}`));