forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-email-verification.js
More file actions
65 lines (58 loc) · 1.88 KB
/
Copy pathsend-email-verification.js
File metadata and controls
65 lines (58 loc) · 1.88 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
import { GraphQLString, GraphQLNonNull } from 'graphql'
import { mutationWithClientMutationId } from 'graphql-relay'
import { GraphQLEmailAddress } from 'graphql-scalars'
import { t } from '@lingui/macro'
export const sendEmailVerification = new mutationWithClientMutationId({
name: 'SendEmailVerification',
description:
'This mutation is used for re-sending a verification email if it failed during user creation.',
inputFields: () => ({
userName: {
type: GraphQLNonNull(GraphQLEmailAddress),
description:
'The users email address used for sending the verification email.',
},
}),
outputFields: () => ({
status: {
type: GraphQLString,
description: 'Informs the user if the email was sent successfully.',
resolve: async (payload) => {
return payload.status
},
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
request,
auth: { tokenize },
validators: { cleanseInput },
loaders: { loadUserByUserName },
notify: { sendVerificationEmail },
},
) => {
// Cleanse Input
const userName = cleanseInput(args.userName).toLowerCase()
// Get user from db
const user = await loadUserByUserName.load(userName)
if (typeof user !== 'undefined') {
const token = tokenize({
parameters: { userKey: user._key },
})
const verifyUrl = `https://${request.get('host')}/validate/${token}`
await sendVerificationEmail({ user, verifyUrl })
console.info(`User: ${user._key} successfully sent a verification email.`)
} else {
console.warn(
`A user attempted to send a verification email for ${userName} but no account is affiliated with this user name.`,
)
}
return {
status: i18n._(
t`If an account with this username is found, an email verification link will be found in your inbox.`,
),
}
},
})