forked from fourTheorem/slic-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-util.js
45 lines (38 loc) · 948 Bytes
/
user-util.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
'use strict'
const jwt = require('jsonwebtoken')
const { rword } = require('rword')
const { createUser, deleteUser } = require('./cognito-util')
const stage = process.env.SLIC_STAGE || 'local'
let userPromise
async function createActualUser() {
if (stage === 'local') {
const userId = rword.generate(3).join('-')
const email = `${userId}@example.com`
// SLIC backend with serverless-offline will derive the
// user context by JWT-decoding the Authorization header
return {
idToken: jwt.sign({ 'cognito:username': userId, email }, 'dummy-key'),
email,
userId
}
} else {
return createUser()
}
}
function getUser() {
if (!userPromise) {
userPromise = createActualUser()
}
return userPromise
}
async function removeUser() {
const user = await getUser()
if (stage !== 'local') {
await deleteUser(user)
userPromise = null
}
}
module.exports = {
getUser,
removeUser
}