forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-account.js
More file actions
106 lines (96 loc) · 3.13 KB
/
Copy pathverify-account.js
File metadata and controls
106 lines (96 loc) · 3.13 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { GraphQLNonNull, GraphQLString } from 'graphql'
import { mutationWithClientMutationId } from 'graphql-relay'
import { t } from '@lingui/macro'
export const verifyAccount = new mutationWithClientMutationId({
name: 'VerifyAccount',
description:
'This mutation allows the user to verify their account through a token sent in an email.',
inputFields: () => ({
verifyTokenString: {
type: GraphQLNonNull(GraphQLString),
description: 'Token sent via email, and located in url.',
},
}),
outputFields: () => ({
status: {
type: GraphQLString,
description: 'Informs user if account was successfully verified.',
resolve: async (payload) => {
return payload.status
},
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
userKey,
auth: { verifyToken },
loaders: { userLoaderByKey },
validators: { cleanseInput },
},
) => {
// Cleanse Input
const verifyTokenString = cleanseInput(args.verifyTokenString)
if (typeof userKey === 'undefined') {
console.warn(
`User attempted to verify their account, but the userKey is undefined.`,
)
throw new Error(i18n._(t`Unable to verify account. Please try again.`))
}
// Check if user exists
const user = await userLoaderByKey.load(userKey)
if (typeof user === 'undefined') {
console.warn(
`User: ${userKey} attempted to verify account, however no account is associated with this id.`,
)
throw new Error(i18n._(t`Unable to verify account. Please try again.`))
}
// Get info from token
const tokenParameters = verifyToken({ token: verifyTokenString })
// Check to see if userKey exists in tokenParameters
if (
tokenParameters.userKey === 'undefined' ||
typeof tokenParameters.userKey === 'undefined'
) {
console.warn(
`When validating account user: ${user._key} attempted to verify account, but userKey is not located in the token parameters.`,
)
throw new Error(
i18n._(t`Unable to verify account. Please request a new email.`),
)
}
// Make sure user ids match
if (tokenParameters.userKey !== user._key) {
console.warn(
`User: ${user._key} attempted to verify their account, but the user id's do not match.`,
)
throw new Error(
i18n._(t`Unable to verify account. Please request a new email.`),
)
}
// Verify users account
try {
await query`
UPSERT { _key: ${user._key} }
INSERT { emailValidated: true, tfaSendMethod: 'email' }
UPDATE { emailValidated: true, tfaSendMethod: 'email' }
IN users
`
} catch (err) {
console.error(
`Database error occurred when upserting email validation for user: ${user._key}: ${err}`,
)
throw new Error(i18n._(t`Unable to verify account. Please try again.`))
}
console.info(
`User: ${user._key} successfully email validated their account.`,
)
return {
status: i18n._(
t`Successfully email verified account, and set TFA send method to email.`,
),
}
},
})