-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuse-password-reset.js
More file actions
56 lines (51 loc) · 2.2 KB
/
use-password-reset.js
File metadata and controls
56 lines (51 loc) · 2.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
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {AuthHelpers, useAuthHelper} from '@salesforce/commerce-sdk-react'
import {useToast} from '@salesforce/retail-react-app/app/hooks/use-toast'
import {useIntl} from 'react-intl'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import useMultiSite from '@salesforce/retail-react-app/app/hooks/use-multi-site'
import {getPasswordlessCallbackUrl} from '@salesforce/retail-react-app/app/utils/auth-utils'
/**
* This hook provides commerce-react-sdk hooks to simplify the reset password flow.
*/
export const usePasswordReset = () => {
const showToast = useToast()
const {formatMessage} = useIntl()
const {locale} = useMultiSite()
const config = getConfig().app.login?.resetPassword
const resetPasswordLandingPath = config?.landingPath
const callbackURI = getPasswordlessCallbackUrl(config?.callbackURI)
const getPasswordResetTokenMutation = useAuthHelper(AuthHelpers.GetPasswordResetToken)
const resetPasswordMutation = useAuthHelper(AuthHelpers.ResetPassword)
const getPasswordResetToken = async (email) => {
await getPasswordResetTokenMutation.mutateAsync({
user_id: email,
mode: config.mode,
locale: locale.id,
...(callbackURI && {callback_uri: callbackURI})
})
}
const resetPassword = async ({email, token, newPassword}) => {
await resetPasswordMutation.mutateAsync(
{user_id: email, pwd_action_token: token, new_password: newPassword},
{
onSuccess: () => {
showToast({
title: formatMessage({
defaultMessage: 'Password Reset Success',
id: 'password_reset_success.toast'
}),
status: 'success',
position: 'bottom-right'
})
}
}
)
}
return {getPasswordResetToken, resetPassword, resetPasswordLandingPath}
}