Use TokenField for password change and reset forms

This commit is contained in:
DS Boyce
2026-08-06 11:03:22 -07:00
parent 7750882fd0
commit 9fe53fad87
4 changed files with 59 additions and 97 deletions
-5
View File
@@ -965,11 +965,6 @@
"count": 1 "count": 1
} }
}, },
"src/screens/Settings/components/ChangePasswordDialog.tsx": {
"typescript/no-explicit-any": {
"count": 2
}
},
"src/screens/Settings/components/CopyButton.tsx": { "src/screens/Settings/components/CopyButton.tsx": {
"typescript/no-floating-promises": { "typescript/no-floating-promises": {
"count": 1 "count": 1
@@ -15,10 +15,23 @@ export function isValidCode(value?: string) {
} }
export function TokenField({ export function TokenField({
testID,
autoFocus,
editable,
value, value,
onChangeText, onChangeText,
onFocus,
onSubmitEditing, onSubmitEditing,
}: Pick<TextInputProps, 'value' | 'onChangeText' | 'onSubmitEditing'>) { }: Pick<
TextInputProps,
| 'testID'
| 'autoFocus'
| 'editable'
| 'value'
| 'onChangeText'
| 'onFocus'
| 'onSubmitEditing'
>) {
const {t: l} = useLingui() const {t: l} = useLingui()
const isInvalid = Boolean(value && value.length > 10 && !isValidCode(value)) const isInvalid = Boolean(value && value.length > 10 && !isValidCode(value))
@@ -31,14 +44,18 @@ export function TokenField({
<TextField.Root> <TextField.Root>
<TextField.Icon icon={Shield} /> <TextField.Icon icon={Shield} />
<TextField.Input <TextField.Input
testID={testID}
autoComplete="off" autoComplete="off"
autoCorrect={false} autoCorrect={false}
autoFocus={autoFocus}
editable={editable}
isInvalid={isInvalid} isInvalid={isInvalid}
label={l`Confirmation code`} label={l`Confirmation code`}
maxLength={11} maxLength={11}
placeholder="XXXXX-XXXXX" placeholder="XXXXX-XXXXX"
value={value} value={value}
onChangeText={handleOnChangeText} onChangeText={handleOnChangeText}
onFocus={onFocus}
onSubmitEditing={onSubmitEditing} onSubmitEditing={onSubmitEditing}
/> />
</TextField.Root> </TextField.Root>
+3 -23
View File
@@ -9,9 +9,9 @@ import {logger} from '#/logger'
import {atoms as a, web} from '#/alf' import {atoms as a, web} from '#/alf'
import {Admonition} from '#/components/Admonition' import {Admonition} from '#/components/Admonition'
import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {TokenField} from '#/components/dialogs/EmailDialog/components/TokenField'
import * as TextField from '#/components/forms/TextField' import * as TextField from '#/components/forms/TextField'
import {Lock_Stroke2_Corner0_Rounded as Lock} from '#/components/icons/Lock' import {Lock_Stroke2_Corner0_Rounded as Lock} from '#/components/icons/Lock'
import {Ticket_Stroke2_Corner0_Rounded as Ticket} from '#/components/icons/Ticket'
import {Loader} from '#/components/Loader' import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
import {useAnalytics} from '#/analytics' import {useAnalytics} from '#/analytics'
@@ -87,17 +87,6 @@ export const SetNewPasswordForm = ({
} }
} }
const onBlur = () => {
const formattedCode = checkAndFormatResetCode(resetCode)
if (!formattedCode) {
setError(
l`You have entered an invalid code. It should look like XXXXX-XXXXX.`,
)
return
}
setResetCode(formattedCode)
}
return ( return (
<FormContainer <FormContainer
testID="setNewPasswordForm" testID="setNewPasswordForm"
@@ -112,23 +101,14 @@ export const SetNewPasswordForm = ({
<TextField.LabelText> <TextField.LabelText>
<Trans>Reset code</Trans> <Trans>Reset code</Trans>
</TextField.LabelText> </TextField.LabelText>
<TextField.Root> <TokenField
<TextField.Icon icon={Ticket} />
<TextField.Input
testID="resetCodeInput" testID="resetCodeInput"
label={l`Looks like XXXXX-XXXXX`}
autoCapitalize="none"
autoFocus={true} autoFocus={true}
autoCorrect={false} editable={!isProcessing}
autoComplete="off"
value={resetCode} value={resetCode}
onChangeText={setResetCode} onChangeText={setResetCode}
onFocus={() => setError('')} onFocus={() => setError('')}
onBlur={onBlur}
editable={!isProcessing}
accessibilityHint={l`Input code sent to your email for password reset`}
/> />
</TextField.Root>
</View> </View>
<View> <View>
<TextField.LabelText> <TextField.LabelText>
@@ -1,8 +1,6 @@
import {useState} from 'react' import {useState} from 'react'
import {useWindowDimensions, View} from 'react-native' import {useWindowDimensions, View} from 'react-native'
import {msg} from '@lingui/core/macro' import {Trans, useLingui} from '@lingui/react/macro'
import {useLingui} from '@lingui/react'
import {Trans} from '@lingui/react/macro'
import * as EmailValidator from 'email-validator' import * as EmailValidator from 'email-validator'
import {cleanError, isNetworkError} from '#/lib/strings/errors' import {cleanError, isNetworkError} from '#/lib/strings/errors'
@@ -14,6 +12,7 @@ import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'
import {android, atoms as a, web} from '#/alf' import {android, atoms as a, web} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog' import * as Dialog from '#/components/Dialog'
import {TokenField} from '#/components/dialogs/EmailDialog/components/TokenField'
import * as TextField from '#/components/forms/TextField' import * as TextField from '#/components/forms/TextField'
import {Loader} from '#/components/Loader' import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
@@ -44,7 +43,7 @@ export function ChangePasswordDialog({
} }
function Inner() { function Inner() {
const {_} = useLingui() const {t: l} = useLingui()
const {currentAccount} = useSession() const {currentAccount} = useSession()
const client = usePdsClient() const client = usePdsClient()
const control = Dialog.useDialogContext() const control = Dialog.useDialogContext()
@@ -57,22 +56,16 @@ function Inner() {
const uiStrings = { const uiStrings = {
RequestCode: { RequestCode: {
title: _(msg`Change your password`), title: l`Change your password`,
message: _( message: l`If you want to change your password, we will send you a code to verify that this is your account.`,
msg`If you want to change your password, we will send you a code to verify that this is your account.`,
),
}, },
ChangePassword: { ChangePassword: {
title: _(msg`Enter code`), title: l`Enter code`,
message: _( message: l`Please enter the code you received and the new password you would like to use.`,
msg`Please enter the code you received and the new password you would like to use.`,
),
}, },
Done: { Done: {
title: _(msg`Password changed`), title: l`Password changed`,
message: _( message: l`Your password has been changed successfully! Please use your new password when you sign in to Bluesky from now on.`,
msg`Your password has been changed successfully! Please use your new password when you sign in to Bluesky from now on.`,
),
}, },
} }
@@ -81,7 +74,7 @@ function Inner() {
!currentAccount?.email || !currentAccount?.email ||
!EmailValidator.validate(currentAccount.email) !EmailValidator.validate(currentAccount.email)
) { ) {
return setError(_(msg`Your email appears to be invalid.`)) return setError(l`Your email appears to be invalid.`)
} }
setError('') setError('')
@@ -91,12 +84,11 @@ function Inner() {
email: currentAccount.email, email: currentAccount.email,
}) })
setStage(Stages.ChangePassword) setStage(Stages.ChangePassword)
} catch (e: any) { } catch (error) {
const e = error as Error
if (isNetworkError(e)) { if (isNetworkError(e)) {
setError( setError(
_( l`Unable to contact your service. Please check your internet connection and try again.`,
msg`Unable to contact your service. Please check your internet connection and try again.`,
),
) )
} else { } else {
logger.error('Failed to request password reset', {safeMessage: e}) logger.error('Failed to request password reset', {safeMessage: e})
@@ -111,20 +103,18 @@ function Inner() {
const formattedCode = checkAndFormatResetCode(resetCode) const formattedCode = checkAndFormatResetCode(resetCode)
if (!formattedCode) { if (!formattedCode) {
setError( setError(
_( l`You have entered an invalid code. It should look like XXXXX-XXXXX.`,
msg`You have entered an invalid code. It should look like XXXXX-XXXXX.`,
),
) )
return return
} }
if (!newPassword) { if (!newPassword) {
setError( setError(
_(msg`Please enter a password. It must be at least 8 characters long.`), l`Please enter a password. It must be at least 8 characters long.`,
) )
return return
} }
if (newPassword.length < 8) { if (newPassword.length < 8) {
setError(_(msg`Password must be at least 8 characters long.`)) setError(l`Password must be at least 8 characters long.`)
return return
} }
@@ -136,17 +126,16 @@ function Inner() {
password: newPassword, password: newPassword,
}) })
setStage(Stages.Done) setStage(Stages.Done)
} catch (e: any) { } catch (error) {
const e = error as Error
if (isNetworkError(e)) { if (isNetworkError(e)) {
setError( setError(
_( l`Unable to contact your service. Please check your internet connection and try again.`,
msg`Unable to contact your service. Please check your internet connection and try again.`,
),
) )
} else if ( } else if (
matchXrpcError(e, com.atproto.server.resetPassword) === 'InvalidToken' matchXrpcError(e, com.atproto.server.resetPassword) === 'InvalidToken'
) { ) {
setError(_(msg`This confirmation code is not valid. Please try again.`)) setError(l`This confirmation code is not valid. Please try again.`)
} else { } else {
logger.error('Failed to set new password', {safeMessage: e}) logger.error('Failed to set new password', {safeMessage: e})
setError(cleanError(e)) setError(cleanError(e))
@@ -156,17 +145,9 @@ function Inner() {
} }
} }
const onBlur = () => {
const formattedCode = checkAndFormatResetCode(resetCode)
if (!formattedCode) {
return
}
setResetCode(formattedCode)
}
return ( return (
<Dialog.ScrollableInner <Dialog.ScrollableInner
label={_(msg`Change password dialog`)} label={l`Change password dialog`}
style={web({maxWidth: 400})}> style={web({maxWidth: 400})}>
<View style={[a.gap_xl]}> <View style={[a.gap_xl]}>
<View style={[a.gap_sm]}> <View style={[a.gap_sm]}>
@@ -190,18 +171,7 @@ function Inner() {
<TextField.LabelText> <TextField.LabelText>
<Trans>Confirmation code</Trans> <Trans>Confirmation code</Trans>
</TextField.LabelText> </TextField.LabelText>
<TextField.Root> <TokenField value={resetCode} onChangeText={setResetCode} />
<TextField.Input
label={_(msg`Confirmation code`)}
placeholder="XXXXX-XXXXX"
value={resetCode}
onChangeText={setResetCode}
onBlur={onBlur}
autoCapitalize="none"
autoCorrect={false}
autoComplete="one-time-code"
/>
</TextField.Root>
</View> </View>
<View> <View>
<TextField.LabelText> <TextField.LabelText>
@@ -209,8 +179,8 @@ function Inner() {
</TextField.LabelText> </TextField.LabelText>
<TextField.Root> <TextField.Root>
<TextField.Input <TextField.Input
label={_(msg`New password`)} label={l`New password`}
placeholder={_(msg`At least 8 characters`)} placeholder={l`At least 8 characters`}
value={newPassword} value={newPassword}
onChangeText={setNewPassword} onChangeText={setNewPassword}
secureTextEntry secureTextEntry
@@ -227,18 +197,18 @@ function Inner() {
{stage === Stages.RequestCode ? ( {stage === Stages.RequestCode ? (
<> <>
<Button <Button
label={_(msg`Request code`)} label={l`Request code`}
color="primary" color="primary"
size="large" size="large"
disabled={isProcessing} disabled={isProcessing}
onPress={onRequestCode}> onPress={() => void onRequestCode()}>
<ButtonText> <ButtonText>
<Trans>Request code</Trans> <Trans>Request code</Trans>
</ButtonText> </ButtonText>
{isProcessing && <ButtonIcon icon={Loader} />} {isProcessing && <ButtonIcon icon={Loader} />}
</Button> </Button>
<Button <Button
label={_(msg`Already have a code?`)} label={l`Already have a code?`}
onPress={() => setStage(Stages.ChangePassword)} onPress={() => setStage(Stages.ChangePassword)}
size="large" size="large"
color="primary_subtle" color="primary_subtle"
@@ -249,7 +219,7 @@ function Inner() {
</Button> </Button>
{IS_NATIVE && ( {IS_NATIVE && (
<Button <Button
label={_(msg`Cancel`)} label={l`Cancel`}
color="secondary" color="secondary"
size="large" size="large"
disabled={isProcessing} disabled={isProcessing}
@@ -263,18 +233,18 @@ function Inner() {
) : stage === Stages.ChangePassword ? ( ) : stage === Stages.ChangePassword ? (
<> <>
<Button <Button
label={_(msg`Change password`)} label={l`Change password`}
color="primary" color="primary"
size="large" size="large"
disabled={isProcessing} disabled={isProcessing}
onPress={onChangePassword}> onPress={() => void onChangePassword()}>
<ButtonText> <ButtonText>
<Trans>Change password</Trans> <Trans>Change password</Trans>
</ButtonText> </ButtonText>
{isProcessing && <ButtonIcon icon={Loader} />} {isProcessing && <ButtonIcon icon={Loader} />}
</Button> </Button>
<Button <Button
label={_(msg`Back`)} label={l`Back`}
color="secondary" color="secondary"
size="large" size="large"
disabled={isProcessing} disabled={isProcessing}
@@ -289,7 +259,7 @@ function Inner() {
</> </>
) : stage === Stages.Done ? ( ) : stage === Stages.Done ? (
<Button <Button
label={_(msg`Close`)} label={l`Close`}
color="primary" color="primary"
size="large" size="large"
onPress={() => control.close()}> onPress={() => control.close()}>