forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-required.js
More file actions
32 lines (28 loc) · 863 Bytes
/
user-required.js
File metadata and controls
32 lines (28 loc) · 863 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
31
32
const userRequired = async (userId, userLoaderByKey) => {
if (typeof userId === 'undefined') {
console.warn(
`User attempted to access controlled content, but userId was undefined.`,
)
throw new Error('Authentication error. Please sign in.')
}
let user, userDoesNotExist
try {
user = await userLoaderByKey.load(userId)
if (typeof user === 'undefined') {
userDoesNotExist = true
}
} catch (err) {
console.error(`Database error occurred when running userRequired: ${err}`)
throw new Error('Authentication error. Please sign in.')
}
if (userDoesNotExist) {
console.warn(
`User: ${userId} attempted to access controlled content, but no user is associated with that id.`,
)
throw new Error('Authentication error. Please sign in.')
}
return user
}
module.exports = {
userRequired,
}