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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
import {useState} from 'react'
import {useWindowDimensions, View} from 'react-native'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import * as EmailValidator from 'email-validator'
import {cleanError, isNetworkError} from '#/lib/strings/errors'
import {checkAndFormatResetCode} from '#/lib/strings/password'
import {logger} from '#/logger'
import {isNative} from '#/platform/detection'
import {useAgent, useSession} from '#/state/session'
import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'
import {android, atoms as a, web} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog'
import * as TextField from '#/components/forms/TextField'
import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'
enum Stages {
RequestCode = 'RequestCode',
ChangePassword = 'ChangePassword',
Done = 'Done',
}
export function ChangePasswordDialog({
control,
}: {
control: Dialog.DialogControlProps
}) {
const {height} = useWindowDimensions()
return (
<Dialog.Outer
control={control}
nativeOptions={android({minHeight: height / 2})}>
<Dialog.Handle />
<Inner />
</Dialog.Outer>
)
}
function Inner() {
const {_} = useLingui()
const {currentAccount} = useSession()
const agent = useAgent()
const control = Dialog.useDialogContext()
const [stage, setStage] = useState(Stages.RequestCode)
const [isProcessing, setIsProcessing] = useState(false)
const [resetCode, setResetCode] = useState('')
const [newPassword, setNewPassword] = useState('')
const [error, setError] = useState('')
const uiStrings = {
RequestCode: {
title: _(msg`Change your password`),
message: _(
msg`If you want to change your password, we will send you a code to verify that this is your account.`,
),
},
ChangePassword: {
title: _(msg`Enter code`),
message: _(
msg`Please enter the code you received and the new password you would like to use.`,
),
},
Done: {
title: _(msg`Password changed`),
message: _(
msg`Your password has been changed successfully! Please use your new password when you sign in to Bluesky from now on.`,
),
},
}
const onRequestCode = async () => {
if (
!currentAccount?.email ||
!EmailValidator.validate(currentAccount.email)
) {
return setError(_(msg`Your email appears to be invalid.`))
}
setError('')
setIsProcessing(true)
try {
await agent.com.atproto.server.requestPasswordReset({
email: currentAccount.email,
})
setStage(Stages.ChangePassword)
} catch (e: any) {
if (isNetworkError(e)) {
setError(
_(
msg`Unable to contact your service. Please check your internet connection and try again.`,
),
)
} else {
logger.error('Failed to request password reset', {safeMessage: e})
setError(cleanError(e))
}
} finally {
setIsProcessing(false)
}
}
const onChangePassword = async () => {
const formattedCode = checkAndFormatResetCode(resetCode)
if (!formattedCode) {
setError(
_(
msg`You have entered an invalid code. It should look like XXXXX-XXXXX.`,
),
)
return
}
if (!newPassword) {
setError(
_(msg`Please enter a password. It must be at least 8 characters long.`),
)
return
}
if (newPassword.length < 8) {
setError(_(msg`Password must be at least 8 characters long.`))
return
}
setError('')
setIsProcessing(true)
try {
await agent.com.atproto.server.resetPassword({
token: formattedCode,
password: newPassword,
})
setStage(Stages.Done)
} catch (e: any) {
if (isNetworkError(e)) {
setError(
_(
msg`Unable to contact your service. Please check your internet connection and try again.`,
),
)
} else if (e?.toString().includes('Token is invalid')) {
setError(_(msg`This confirmation code is not valid. Please try again.`))
} else {
logger.error('Failed to set new password', {safeMessage: e})
setError(cleanError(e))
}
} finally {
setIsProcessing(false)
}
}
const onBlur = () => {
const formattedCode = checkAndFormatResetCode(resetCode)
if (!formattedCode) {
return
}
setResetCode(formattedCode)
}
return (
<Dialog.ScrollableInner
label={_(msg`Change password dialog`)}
style={web({maxWidth: 400})}>
<View style={[a.gap_xl]}>
<View style={[a.gap_sm]}>
<Text style={[a.font_heavy, a.text_2xl]}>
{uiStrings[stage].title}
</Text>
{error ? (
<View style={[a.rounded_sm, a.overflow_hidden]}>
<ErrorMessage message={error} />
</View>
) : null}
<Text style={[a.text_md, a.leading_snug]}>
{uiStrings[stage].message}
</Text>
</View>
{stage === Stages.ChangePassword && (
<View style={[a.gap_md]}>
<View>
<TextField.LabelText>
<Trans>Confirmation code</Trans>
</TextField.LabelText>
<TextField.Root>
<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>
<TextField.LabelText>
<Trans>New password</Trans>
</TextField.LabelText>
<TextField.Root>
<TextField.Input
label={_(msg`New password`)}
placeholder={_(msg`At least 8 characters`)}
value={newPassword}
onChangeText={setNewPassword}
secureTextEntry
autoCapitalize="none"
autoComplete="new-password"
/>
</TextField.Root>
</View>
</View>
)}
<View style={[a.gap_sm]}>
{stage === Stages.RequestCode ? (
<>
<Button
label={_(msg`Request code`)}
color="primary"
size="large"
disabled={isProcessing}
onPress={onRequestCode}>
<ButtonText>
<Trans>Request code</Trans>
</ButtonText>
{isProcessing && <ButtonIcon icon={Loader} />}
</Button>
<Button
label={_(msg`Already have a code?`)}
onPress={() => setStage(Stages.ChangePassword)}
size="large"
color="primary_subtle"
disabled={isProcessing}>
<ButtonText>
<Trans>Already have a code?</Trans>
</ButtonText>
</Button>
{isNative && (
<Button
label={_(msg`Cancel`)}
color="secondary"
size="large"
disabled={isProcessing}
onPress={() => control.close()}>
<ButtonText>
<Trans>Cancel</Trans>
</ButtonText>
</Button>
)}
</>
) : stage === Stages.ChangePassword ? (
<>
<Button
label={_(msg`Change password`)}
color="primary"
size="large"
disabled={isProcessing}
onPress={onChangePassword}>
<ButtonText>
<Trans>Change password</Trans>
</ButtonText>
{isProcessing && <ButtonIcon icon={Loader} />}
</Button>
<Button
label={_(msg`Back`)}
color="secondary"
size="large"
disabled={isProcessing}
onPress={() => {
setResetCode('')
setStage(Stages.RequestCode)
}}>
<ButtonText>
<Trans>Back</Trans>
</ButtonText>
</Button>
</>
) : stage === Stages.Done ? (
<Button
label={_(msg`Close`)}
color="primary"
size="large"
onPress={() => control.close()}>
<ButtonText>
<Trans>Close</Trans>
</ButtonText>
</Button>
) : null}
</View>
</View>
<Dialog.Close />
</Dialog.ScrollableInner>
)
}
|