-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
57 lines (43 loc) · 1.28 KB
/
auth.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express')
const jwt = require('jsonwebtoken')
const fetch = require('node-fetch')
const { body, validationResult } = require('express-validator')
const router = express.Router()
const { oracle_url, jwt_secret } = require('../config')
router.post('/auth', [body('token').isString()], async (req, res) => {
// takes a JWT with admin field and verifies it against oracle
const requestOk = validationResult(req)
if (!requestOk.isEmpty()) {
res.status(400).json({
message: 'invalid_values',
fields: requestOk.array(),
})
return
}
const authResult = await fetch(oracle_url, {
headers: {
Authorization: 'Token ' + req.body.token,
},
})
// check that the JWT is valid
if (!authResult.ok) {
res.status(403).json({
message: 'unauthorized',
})
return
}
const userData = await authResult.json()
const token = jwt.sign(
{
admin: userData.user.admin,
user_id: userData.user.id,
team_id: userData.user.team?.id,
competition_id: userData.competition?.id ?? -1,
},
jwt_secret
)
res.status(200).json({
token,
})
})
module.exports = router