|
| 1 | +import { useClerk, useReverification, useSession } from '@clerk/shared/react'; |
| 2 | +import type { UserResource } from '@clerk/shared/types'; |
| 3 | + |
| 4 | +import { useEnvironment, useSignOutContext, withCoreSessionSwitchGuard } from '@/ui/contexts'; |
| 5 | +import { Col, descriptors, Flow, localizationKeys, useLocalizations } from '@/ui/customizables'; |
| 6 | +import { Card } from '@/ui/elements/Card'; |
| 7 | +import { useCardState, withCardStateProvider } from '@/ui/elements/contexts'; |
| 8 | +import { Form } from '@/ui/elements/Form'; |
| 9 | +import { Header } from '@/ui/elements/Header'; |
| 10 | +import { useConfirmPassword } from '@/ui/hooks'; |
| 11 | +import { useMultipleSessions } from '@/ui/hooks/useMultipleSessions'; |
| 12 | +import { handleError } from '@/ui/utils/errorHandler'; |
| 13 | +import { createPasswordError } from '@/ui/utils/passwordUtils'; |
| 14 | +import { useFormControl } from '@/ui/utils/useFormControl'; |
| 15 | + |
| 16 | +import { withTaskGuard } from './withTaskGuard'; |
| 17 | + |
| 18 | +const TaskResetPasswordInternal = () => { |
| 19 | + const { signOut, user } = useClerk(); |
| 20 | + const { session } = useSession(); |
| 21 | + const card = useCardState(); |
| 22 | + const { |
| 23 | + userSettings: { passwordSettings }, |
| 24 | + authConfig: { reverification }, |
| 25 | + } = useEnvironment(); |
| 26 | + |
| 27 | + const { t, locale } = useLocalizations(); |
| 28 | + const { otherSessions } = useMultipleSessions({ user }); |
| 29 | + const { navigateAfterSignOut, navigateAfterMultiSessionSingleSignOutUrl } = useSignOutContext(); |
| 30 | + const updatePasswordWithReverification = useReverification( |
| 31 | + (user: UserResource, opts: Parameters<UserResource['updatePassword']>) => user.updatePassword(...opts), |
| 32 | + ); |
| 33 | + |
| 34 | + const currentPasswordRequired = user && user.passwordEnabled && !reverification; |
| 35 | + |
| 36 | + const handleSignOut = () => { |
| 37 | + if (otherSessions.length === 0) { |
| 38 | + return signOut(navigateAfterSignOut); |
| 39 | + } |
| 40 | + |
| 41 | + return signOut(navigateAfterMultiSessionSingleSignOutUrl, { sessionId: session?.id }); |
| 42 | + }; |
| 43 | + |
| 44 | + const currentPasswordField = useFormControl('currentPassword', '', { |
| 45 | + type: 'password', |
| 46 | + label: localizationKeys('formFieldLabel__currentPassword'), |
| 47 | + isRequired: true, |
| 48 | + }); |
| 49 | + |
| 50 | + const passwordField = useFormControl('newPassword', '', { |
| 51 | + type: 'password', |
| 52 | + label: localizationKeys('formFieldLabel__newPassword'), |
| 53 | + isRequired: true, |
| 54 | + validatePassword: true, |
| 55 | + buildErrorMessage: errors => createPasswordError(errors, { t, locale, passwordSettings }), |
| 56 | + }); |
| 57 | + |
| 58 | + const confirmField = useFormControl('confirmPassword', '', { |
| 59 | + type: 'password', |
| 60 | + label: localizationKeys('formFieldLabel__confirmPassword'), |
| 61 | + isRequired: true, |
| 62 | + }); |
| 63 | + |
| 64 | + const sessionsField = useFormControl('signOutOfOtherSessions', '', { |
| 65 | + type: 'checkbox', |
| 66 | + label: localizationKeys('formFieldLabel__signOutOfOtherSessions'), |
| 67 | + defaultChecked: true, |
| 68 | + }); |
| 69 | + |
| 70 | + const { setConfirmPasswordFeedback, isPasswordMatch } = useConfirmPassword({ |
| 71 | + passwordField, |
| 72 | + confirmPasswordField: confirmField, |
| 73 | + }); |
| 74 | + |
| 75 | + const canSubmit = isPasswordMatch; |
| 76 | + |
| 77 | + const validateForm = () => { |
| 78 | + if (passwordField.value) { |
| 79 | + setConfirmPasswordFeedback(confirmField.value); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + const resetPassword = async () => { |
| 84 | + if (!user) { |
| 85 | + return; |
| 86 | + } |
| 87 | + passwordField.clearFeedback(); |
| 88 | + confirmField.clearFeedback(); |
| 89 | + try { |
| 90 | + await updatePasswordWithReverification(user, [ |
| 91 | + { |
| 92 | + newPassword: passwordField.value, |
| 93 | + signOutOfOtherSessions: sessionsField.checked, |
| 94 | + }, |
| 95 | + ]); |
| 96 | + } catch (e) { |
| 97 | + return handleError(e, [currentPasswordField, passwordField, confirmField], card.setError); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + const identifier = user?.primaryEmailAddress?.emailAddress ?? user?.username; |
| 102 | + |
| 103 | + return ( |
| 104 | + <Flow.Root flow='taskResetPassword'> |
| 105 | + <Flow.Part part='resetPassword'> |
| 106 | + <Card.Root> |
| 107 | + <Card.Content> |
| 108 | + <Header.Root showLogo> |
| 109 | + <Header.Title localizationKey={localizationKeys('signIn.resetPassword.title')} /> |
| 110 | + </Header.Root> |
| 111 | + <Card.Alert>{card.error}</Card.Alert> |
| 112 | + <Col |
| 113 | + elementDescriptor={descriptors.main} |
| 114 | + gap={8} |
| 115 | + > |
| 116 | + <Form.Root |
| 117 | + onSubmit={() => { |
| 118 | + void resetPassword(); |
| 119 | + }} |
| 120 | + onBlur={validateForm} |
| 121 | + gap={8} |
| 122 | + > |
| 123 | + <Col gap={6}> |
| 124 | + {/* For password managers */} |
| 125 | + <input |
| 126 | + readOnly |
| 127 | + data-testid='hidden-identifier' |
| 128 | + id='identifier-field' |
| 129 | + name='identifier' |
| 130 | + value={session?.publicUserData.identifier || ''} |
| 131 | + style={{ display: 'none' }} |
| 132 | + /> |
| 133 | + {currentPasswordRequired && ( |
| 134 | + <Form.ControlRow elementId={currentPasswordField.id}> |
| 135 | + <Form.PasswordInput |
| 136 | + {...currentPasswordField.props} |
| 137 | + minLength={6} |
| 138 | + isRequired |
| 139 | + autoFocus |
| 140 | + /> |
| 141 | + </Form.ControlRow> |
| 142 | + )} |
| 143 | + <Form.ControlRow elementId={passwordField.id}> |
| 144 | + <Form.PasswordInput |
| 145 | + {...passwordField.props} |
| 146 | + isRequired |
| 147 | + minLength={6} |
| 148 | + /> |
| 149 | + </Form.ControlRow> |
| 150 | + <Form.ControlRow elementId={confirmField.id}> |
| 151 | + <Form.PasswordInput |
| 152 | + {...confirmField.props} |
| 153 | + onChange={e => { |
| 154 | + if (e.target.value) { |
| 155 | + setConfirmPasswordFeedback(e.target.value); |
| 156 | + } |
| 157 | + return confirmField.props.onChange(e); |
| 158 | + }} |
| 159 | + /> |
| 160 | + </Form.ControlRow> |
| 161 | + <Form.ControlRow elementId={sessionsField.id}> |
| 162 | + <Form.Checkbox {...sessionsField.props} /> |
| 163 | + </Form.ControlRow> |
| 164 | + </Col> |
| 165 | + <Col gap={3}> |
| 166 | + <Form.SubmitButton |
| 167 | + isDisabled={!canSubmit} |
| 168 | + localizationKey={localizationKeys('signIn.resetPassword.formButtonPrimary')} |
| 169 | + /> |
| 170 | + </Col> |
| 171 | + </Form.Root> |
| 172 | + </Col> |
| 173 | + </Card.Content> |
| 174 | + |
| 175 | + <Card.Footer> |
| 176 | + <Card.Action |
| 177 | + elementId='signOut' |
| 178 | + gap={2} |
| 179 | + justify='center' |
| 180 | + sx={() => ({ width: '100%' })} |
| 181 | + > |
| 182 | + {identifier && ( |
| 183 | + <Card.ActionText |
| 184 | + truncate |
| 185 | + localizationKey={localizationKeys('taskChooseOrganization.signOut.actionText', { |
| 186 | + identifier, |
| 187 | + })} |
| 188 | + /> |
| 189 | + )} |
| 190 | + <Card.ActionLink |
| 191 | + sx={() => ({ flexShrink: 0 })} |
| 192 | + onClick={() => { |
| 193 | + void handleSignOut(); |
| 194 | + }} |
| 195 | + localizationKey={localizationKeys('taskChooseOrganization.signOut.actionLink')} |
| 196 | + /> |
| 197 | + </Card.Action> |
| 198 | + </Card.Footer> |
| 199 | + </Card.Root> |
| 200 | + </Flow.Part> |
| 201 | + </Flow.Root> |
| 202 | + ); |
| 203 | +}; |
| 204 | + |
| 205 | +export const TaskResetPassword = withCoreSessionSwitchGuard( |
| 206 | + withTaskGuard(withCardStateProvider(TaskResetPasswordInternal)), |
| 207 | +); |
0 commit comments