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
53 lines (44 loc) · 1.11 KB
/
on-connect.js
File metadata and controls
53 lines (44 loc) · 1.11 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
45
46
47
48
49
50
51
52
53
export const customOnConnect =
({
context,
createI18n,
verifyToken,
userRequired,
loadUserByKey,
verifiedRequired,
}) =>
async (connectionParams, webSocket) => {
const enLangPos = String(
webSocket.upgradeReq.headers['accept-language'],
).indexOf('en')
const frLangPos = String(
webSocket.upgradeReq.headers['accept-language'],
).indexOf('fr')
let language = 'en'
if (frLangPos > enLangPos) {
language = 'fr'
}
let authorization
if (connectionParams.authorization) {
authorization = connectionParams.authorization
}
const i18n = createI18n(language)
const verify = verifyToken({ i18n })
const token = authorization || ''
let userKey
if (token !== '') {
userKey = verify({ token }).userKey
}
const { query } = context
const user = await userRequired({
i18n,
userKey,
loadUserByKey: loadUserByKey({ query, userKey }),
})()
verifiedRequired({ user })
console.info(`User: ${userKey}, connected to subscription.`)
return {
language,
authorization,
}
}