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) · 963 Bytes
/
user-required.js
File metadata and controls
32 lines (28 loc) · 963 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
import { t } from '@lingui/macro'
export const userRequired =
({ i18n, userKey, loadUserByKey }) =>
async () => {
if (typeof userKey === 'undefined') {
console.warn(
`User attempted to access controlled content, but userKey was undefined.`,
)
throw new Error(i18n._(t`Authentication error. Please sign in.`))
}
let user, userDoesNotExist
try {
user = await loadUserByKey.load(userKey)
if (typeof user === 'undefined') {
userDoesNotExist = true
}
} catch (err) {
console.error(`Database error occurred when running userRequired: ${err}`)
throw new Error(i18n._(t`Authentication error. Please sign in.`))
}
if (userDoesNotExist) {
console.warn(
`User: ${userKey} attempted to access controlled content, but no user is associated with that id.`,
)
throw new Error(i18n._(t`Authentication error. Please sign in.`))
}
return user
}