forked from covidgreen/covid-green-lambdas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.js
More file actions
30 lines (22 loc) · 715 Bytes
/
Copy pathtoken.js
File metadata and controls
30 lines (22 loc) · 715 Bytes
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
const jwt = require('jsonwebtoken')
const SQL = require('@nearform/sql')
const { getDatabase, getJwtSecret, runIfDev } = require('./utils')
exports.handler = async function(event) {
const { description, type } = event
if (!description) {
throw new Error('Description is missing')
}
const sql = SQL`
INSERT INTO tokens (type, description)
VALUES (${type || 'push'}, ${description})
RETURNING id`
const secret = await getJwtSecret()
const client = await getDatabase()
const { rowCount, rows } = await client.query(sql)
if (rowCount === 0) {
throw new Error('Unable to create token')
}
const [{ id }] = rows
return jwt.sign({ id }, secret)
}
runIfDev(exports.handler)