-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuse-password-reset.js
More file actions
57 lines (52 loc) · 2.19 KB
/
use-password-reset.js
File metadata and controls
57 lines (52 loc) · 2.19 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
/*
* 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 {useAppOrigin} from '@salesforce/retail-react-app/app/hooks/use-app-origin'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {isAbsoluteURL} from '@salesforce/retail-react-app/app/page-designer/utils'
/**
* This hook provides commerce-react-sdk hooks to simplify the reset password flow.
*/
export const usePasswordReset = () => {
const showToast = useToast()
const {formatMessage} = useIntl()
const appOrigin = useAppOrigin()
const config = getConfig()
const resetPasswordCallback =
config.app.login?.resetPassword?.callbackURI || '/reset-password-callback'
const callbackURI = isAbsoluteURL(resetPasswordCallback)
? resetPasswordCallback
: `${appOrigin}${resetPasswordCallback}`
const getPasswordResetTokenMutation = useAuthHelper(AuthHelpers.GetPasswordResetToken)
const resetPasswordMutation = useAuthHelper(AuthHelpers.ResetPassword)
const getPasswordResetToken = async (email) => {
await getPasswordResetTokenMutation.mutateAsync({
user_id: email,
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}
}