forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-password-reset.js
More file actions
66 lines (60 loc) · 2 KB
/
Copy pathsend-password-reset.js
File metadata and controls
66 lines (60 loc) · 2 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
import { GraphQLNonNull, GraphQLString } from 'graphql'
import { mutationWithClientMutationId } from 'graphql-relay'
import { GraphQLEmailAddress } from 'graphql-scalars'
import { t } from '@lingui/macro'
export const sendPasswordResetLink = new mutationWithClientMutationId({
name: 'SendPasswordResetLink',
description:
'This mutation allows a user to provide their username and request that a password reset email be sent to their account with a reset token in a url.',
inputFields: () => ({
userName: {
type: GraphQLNonNull(GraphQLEmailAddress),
description:
'User name for the account you would like to receive a password reset link for.',
},
}),
outputFields: () => ({
status: {
type: GraphQLString,
description:
'Informs the user if the password reset email was sent successfully.',
resolve: async (payload) => {
return payload.status
},
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
request,
auth: { tokenize },
validators: { cleanseInput },
loaders: { loadUserByUserName },
notify: { sendPasswordResetEmail },
},
) => {
// Cleanse Input
const userName = cleanseInput(args.userName).toLowerCase()
const user = await loadUserByUserName.load(userName)
if (typeof user !== 'undefined') {
const token = tokenize({
parameters: { userKey: user._key, currentPassword: user.password },
})
const resetUrl = `https://${request.get('host')}/reset-password/${token}`
await sendPasswordResetEmail({ user, resetUrl })
console.info(
`User: ${user._key} successfully sent a password reset email.`,
)
} else {
console.warn(
`A user attempted to send a password reset email for ${userName} but no account is affiliated with this user name.`,
)
}
return {
status: i18n._(
t`If an account with this username is found, a password reset link will be found in your inbox.`,
),
}
},
})