forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathon-connect.js
More file actions
44 lines (33 loc) · 1.05 KB
/
on-connect.js
File metadata and controls
44 lines (33 loc) · 1.05 KB
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
export const customOnConnect =
({
createContext,
serverContext,
createI18n,
verifyToken,
userRequired,
loadUserByKey,
verifiedRequired,
}) =>
async (connectionParams, _webSocket, context) => {
const expandedContext = { ...serverContext, ...context }
const factoryFunc = createContext(expandedContext)
const language = connectionParams?.AcceptLanguage
const authorization = connectionParams?.authorization
const i18n = createI18n(language)
const verify = verifyToken({ i18n })
const token = authorization || ''
let userKey
if (token !== '') {
userKey = verify({ token }).userKey
}
const { query } = serverContext
const user = await userRequired({
i18n,
userKey,
loadUserByKey: loadUserByKey({ query, userKey }),
})()
verifiedRequired({ user })
console.info(`User: ${userKey}, connected to subscription.`)
const finalContext = await factoryFunc({ req: context.request, connection: { language, authorization } })
return finalContext
}