-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
255 lines (226 loc) · 9.33 KB
/
index.jsx
File metadata and controls
255 lines (226 loc) · 9.33 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (c) 2026, salesforce.com, 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 React, {useState, useEffect} from 'react'
import PropTypes from 'prop-types'
import {useIntl} from 'react-intl'
// Components
import OtpAuth from '@salesforce/retail-react-app/app/components/otp-auth'
import {
Button,
FormControl,
FormLabel,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalHeader,
ModalOverlay,
Alert,
AlertIcon
} from '@salesforce/retail-react-app/app/components/shared/ui'
// Hooks
import {useForm} from 'react-hook-form'
import {useCurrentCustomer} from '@salesforce/retail-react-app/app/hooks/use-current-customer'
// Utils
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {arrayBufferToBase64Url} from '@salesforce/retail-react-app/app/utils/utils'
// SDK
import {AuthHelpers, useAuthHelper} from '@salesforce/commerce-sdk-react'
// Constants
import {API_ERROR_MESSAGE, INVALID_TOKEN_ERROR_MESSAGE} from '@salesforce/retail-react-app/app/constants'
/**
* Modal for registering a new passkey with a nickname
*/
const PasskeyRegistrationModal = ({isOpen, onClose}) => {
const {data: customer} = useCurrentCustomer()
const {formatMessage} = useIntl()
const [passkeyNickname, setPasskeyNickname] = useState('')
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState(null)
const [isOtpAuthOpen, setIsOtpAuthOpen] = useState(false)
const form = useForm()
const config = getConfig()
const webauthnConfig = config.app.login.passkey
const authorizeWebauthnRegistration = useAuthHelper(AuthHelpers.AuthorizeWebauthnRegistration)
const startWebauthnUserRegistration = useAuthHelper(AuthHelpers.StartWebauthnUserRegistration)
const finishWebauthnUserRegistration = useAuthHelper(AuthHelpers.FinishWebauthnUserRegistration)
const handleRegisterPasskey = async () => {
setIsLoading(true)
setError(null)
try {
await authorizeWebauthnRegistration.mutateAsync({
user_id: customer.email,
mode: webauthnConfig.mode,
...(webauthnConfig.mode === 'callback' && {
callback_uri: webauthnConfig.callbackURI
})
})
// Open OTP auth modal
onClose()
setIsOtpAuthOpen(true)
} catch (err) {
// Set error message for the passkey registration modal
setError(formatMessage(API_ERROR_MESSAGE))
} finally {
setIsLoading(false)
}
}
const handleOtpVerification = async (code) => {
setIsLoading(true)
setError(null)
try {
// Step 1: Start WebAuthn registration
const response = await startWebauthnUserRegistration.mutateAsync({
user_id: customer.email,
pwd_action_token: code,
...(passkeyNickname && {nick_name: passkeyNickname})
})
// Step 2: Convert response to WebAuthn PublicKeyCredentialCreationOptions format
const publicKey = window.PublicKeyCredential.parseCreationOptionsFromJSON(response)
// Step 3: Call navigator.credentials.create()
if (!navigator.credentials || !navigator.credentials.create) {
throw new Error('WebAuthn API not available in this browser')
}
// Step 4: navigator.credentials.create() will show a browser/system prompt
// This may appear to hang if the user doesn't interact with the prompt
const credential = await navigator.credentials.create({publicKey})
if (!credential) {
throw new Error('Failed to create credential: user cancelled or operation failed')
}
// Step 5: Convert credential to JSON format before sending to SLAS
// https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential/toJSON
let credentialJson
try {
credentialJson = credential.toJSON()
} catch (error) {
// Fallback to manual encoding if toJSON() fails
// Some passkey providers (e.g., 1Password) may not support the toJSON() method and return an error
const clientExtensionResults = credential.getClientExtensionResults?.() || {}
credentialJson = {
type: credential.type,
id: credential.id,
rawId: arrayBufferToBase64Url(credential.rawId),
response: {
attestationObject: arrayBufferToBase64Url(
credential.response.attestationObject
),
clientDataJSON: arrayBufferToBase64Url(credential.response.clientDataJSON)
},
...(Object.keys(clientExtensionResults).length > 0 && {clientExtensionResults})
}
}
// Step 6: Finish WebAuthn registration
await finishWebauthnUserRegistration.mutateAsync({
username: customer.email,
credential: credentialJson,
pwd_action_token: code
})
// Step 7: Close OTP modal and main modal on success
setIsOtpAuthOpen(false)
onClose()
return {success: true}
} catch (err) {
console.error('Error registering passkey:', err)
const message = /Unauthorized/i.test(err.message)
? formatMessage(INVALID_TOKEN_ERROR_MESSAGE)
: formatMessage(API_ERROR_MESSAGE)
// Return error result for OTP component to display
return {
success: false,
error: message
}
} finally {
setIsLoading(false)
}
}
const resetState = () => {
setPasskeyNickname('')
setError(null)
}
const handleClose = () => {
resetState()
onClose()
}
// Reset state when modal opens
useEffect(() => {
if (isOpen) {
resetState()
}
}, [isOpen])
return (
<>
<Modal isOpen={isOpen} onClose={handleClose} size="md">
<ModalOverlay />
<ModalContent>
<ModalHeader>
{formatMessage({
defaultMessage: 'Create Passkey',
id: 'passkey_registration.modal.title'
})}
</ModalHeader>
<ModalCloseButton
aria-label={formatMessage({
id: 'passkey_registration.modal.button.close.assistive_msg',
defaultMessage: 'Close passkey form'
})}
data-testid="passkey-registration-modal-close-button"
/>
<ModalBody pb={6}>
{error && (
<Alert status="error" mb={4}>
<AlertIcon />
{error}
</Alert>
)}
<FormControl>
<FormLabel>
{formatMessage({
defaultMessage: 'Passkey Nickname (optional)',
id: 'passkey_registration.modal.label.nickname'
})}
</FormLabel>
<Input
placeholder="e.g., 'iPhone', 'Personal Laptop'"
value={passkeyNickname}
onChange={(e) => setPasskeyNickname(e.target.value)}
mb={4}
isDisabled={isLoading}
/>
<Button
width="full"
colorScheme="blue"
onClick={handleRegisterPasskey}
isLoading={isLoading}
loadingText="Registering..."
>
{formatMessage({
defaultMessage: 'Register Passkey',
id: 'passkey_registration.modal.button.register'
})}
</Button>
</FormControl>
</ModalBody>
</ModalContent>
</Modal>
<OtpAuth
isOpen={isOtpAuthOpen}
onClose={() => setIsOtpAuthOpen(false)}
form={form}
handleSendEmailOtp={handleRegisterPasskey}
handleOtpVerification={handleOtpVerification}
isPasskeyRegistration={true}
hideCheckoutAsGuestButton={true}
/>
</>
)
}
PasskeyRegistrationModal.propTypes = {
isOpen: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired
}
export default PasskeyRegistrationModal