forked from tetherto/pearpass-app-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
344 lines (311 loc) · 9.77 KB
/
Copy pathindex.jsx
File metadata and controls
344 lines (311 loc) · 9.77 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { useEffect, useMemo, useState } from 'react'
import { useLingui } from '@lingui/react/macro'
import { useForm } from '@tetherto/pear-apps-lib-ui-react-hooks'
import { Validator } from '@tetherto/pear-apps-utils-validator'
import {
AlertMessage,
Button,
InputField,
PageHeader,
PasswordField,
rawTokens,
useTheme
} from '@tetherto/pearpass-lib-ui-kit'
import { ReportProblem } from '@tetherto/pearpass-lib-ui-kit/icons'
import { useVault } from '@tetherto/pearpass-lib-vault'
import { validatePasswordChange } from '@tetherto/pearpass-utils-password-check'
import { StyleSheet, View } from 'react-native'
import Toast from 'react-native-toast-message'
import { TOAST_CONFIG } from '../../../constants/toast'
import { VAULT_ACTION } from '../../../constants/vaultActions'
import { useModal } from '../../../context/ModalContext'
import { logger } from '../../../utils/logger'
import {
getPasswordIndicatorVariant,
getPasswordsMatch
} from '../../../utils/passwordPolicy'
/**
* @param {{
* vaultId: string
* vaultName: string
* action?: 'name' | 'password'
* onSuccess?: () => void
* }} props
*/
export const ModifyVaultModalContent = ({
vaultId,
vaultName,
action = VAULT_ACTION.NAME,
onSuccess
}) => {
const { closeModal } = useModal()
const { t } = useLingui()
const { theme } = useTheme()
const isPasswordChangeAction = action === VAULT_ACTION.PASSWORD
const {
isVaultProtected,
updateUnprotectedVault,
updateProtectedVault,
refetch: refetchVault
} = useVault()
const [isProtected, setIsProtected] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const passwordErrors = useMemo(
() => ({
minLength: t`Password must be at least 8 characters long`,
hasLowerCase: t`Password must contain at least one lowercase letter`,
hasUpperCase: t`Password must contain at least one uppercase letter`,
hasNumbers: t`Password must contain at least one number`,
hasSymbols: t`Password must contain at least one special character`
}),
[t]
)
const schema = useMemo(() => {
if (isPasswordChangeAction) {
return Validator.object({
currentPassword: isProtected
? Validator.string().required(t`Current password is required`)
: Validator.string(),
newPassword: Validator.string().required(t`New password is required`),
repeatPassword: Validator.string().required(t`Please repeat password`)
})
}
return Validator.object({
name: Validator.string().required(t`Name is required`),
currentPassword: isProtected
? Validator.string().required(t`Current password is required`)
: Validator.string()
})
}, [isPasswordChangeAction, isProtected, t])
const initialValues = useMemo(
() =>
isPasswordChangeAction
? {
currentPassword: '',
newPassword: '',
repeatPassword: ''
}
: {
name: vaultName,
currentPassword: ''
},
[isPasswordChangeAction, vaultName]
)
const { register, handleSubmit, setErrors } = useForm({
initialValues,
validate: (values) => schema.validate(values)
})
const { onChange: onNameChange, ...nameField } = register('name')
const { onChange: onCurrentPasswordChange, ...currentPasswordField } =
register('currentPassword')
const { onChange: onNewPasswordChange, ...newPasswordField } =
register('newPassword')
const { onChange: onRepeatPasswordChange, ...repeatPasswordField } =
register('repeatPassword')
useEffect(() => {
const checkProtection = async () => {
const result = await isVaultProtected(vaultId)
setIsProtected(result)
}
checkProtection()
}, [isVaultProtected, vaultId])
const pageTitle = isPasswordChangeAction
? isProtected
? t`Update Vault Password`
: t`Set Vault Password`
: t`Rename Vault`
const pageSubtitle = isPasswordChangeAction
? isProtected
? t`Change the extra password that protects this vault when it is reopened or shared.`
: t`Add a dedicated password when this vault needs an extra encryption layer.`
: t`Update the identity shown across your devices, in sharing, and inside settings.`
const handleSave = async (values) => {
if (isPasswordChangeAction) {
const validation = validatePasswordChange({
currentPassword: values.currentPassword,
newPassword: values.newPassword,
repeatPassword: values.repeatPassword,
messages: {
newPasswordMustDiffer: t`New password must be different from the current password`,
passwordsDontMatch: t`Passwords do not match`
},
config: {
errors: passwordErrors
}
})
if (!validation.success) {
setErrors({
[validation.field || 'newPassword']:
validation.error || t`Password is invalid`
})
return
}
}
try {
setIsLoading(true)
const name = isPasswordChangeAction ? vaultName : values.name
const password = isPasswordChangeAction ? values.newPassword : undefined
if (isProtected) {
await updateProtectedVault(vaultId, {
name,
password,
currentPassword: values.currentPassword
})
} else {
await updateUnprotectedVault(vaultId, {
name,
password
})
}
await refetchVault?.(vaultId)
Toast.show({
type: 'baseToast',
text1:
action === VAULT_ACTION.NAME
? t`Vault renamed`
: t`Vault password updated`,
position: 'bottom',
bottomOffset: TOAST_CONFIG.BOTTOM_OFFSET
})
onSuccess?.()
closeModal()
} catch (error) {
logger.error('Error updating vault:', error)
setErrors({
currentPassword: t`Invalid password`
})
} finally {
setIsLoading(false)
}
}
return (
<View
style={[
styles.container,
{
backgroundColor: theme.colors.colorSurfacePrimary,
borderColor: theme.colors.colorBorderPrimary
}
]}
testID="modify-vault-modal"
>
<View style={styles.content}>
<PageHeader
title={pageTitle}
subtitle={pageSubtitle}
testID="modify-vault-modal-header"
/>
<View style={styles.fields}>
{!isPasswordChangeAction ? (
<InputField
label={t`Vault Name`}
placeholder={t`Enter Vault Name`}
value={nameField.value || ''}
onChangeText={onNameChange}
error={nameField.error}
testID="modify-vault-modal-name-input"
/>
) : null}
{isProtected ? (
<PasswordField
label={t`Current Password`}
placeholder={t`Enter Current Password`}
value={currentPasswordField.value || ''}
onChangeText={onCurrentPasswordChange}
error={currentPasswordField.error}
testID="modify-vault-modal-current-password-input"
/>
) : null}
{isPasswordChangeAction ? (
<>
<PasswordField
label={t`New Password`}
placeholder={t`Enter New Password`}
value={newPasswordField.value || ''}
onChangeText={onNewPasswordChange}
error={newPasswordField.error}
passwordIndicator={getPasswordIndicatorVariant(
newPasswordField.value,
passwordErrors
)}
testID="modify-vault-modal-new-password-input"
/>
<PasswordField
label={t`Repeat New Password`}
placeholder={t`Repeat New Password`}
value={repeatPasswordField.value || ''}
onChangeText={onRepeatPasswordChange}
error={repeatPasswordField.error}
passwordIndicator={
getPasswordsMatch(
newPasswordField.value,
repeatPasswordField.value
)
? 'match'
: undefined
}
testID="modify-vault-modal-repeat-password-input"
/>
<AlertMessage
variant="warning"
size="small"
title={t`Important`}
description={t`Vault passwords are separate from the master password. Keep them stored safely because they are required when reopening or sharing this vault.`}
icon={<ReportProblem />}
testID="modify-vault-modal-password-warning"
/>
</>
) : null}
</View>
</View>
<View
style={[
styles.footer,
{ borderTopColor: theme.colors.colorBorderPrimary }
]}
>
<Button
variant="secondary"
size="medium"
fullWidth
onClick={closeModal}
testID="modify-vault-modal-cancel-button"
>
{t`Cancel`}
</Button>
<Button
variant="primary"
size="medium"
fullWidth
isLoading={isLoading}
disabled={isLoading}
onClick={handleSubmit(handleSave)}
testID="modify-vault-modal-save-button"
>
{t`Save`}
</Button>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
width: '100%',
maxWidth: 480,
borderWidth: 1,
borderRadius: rawTokens.spacing20,
overflow: 'hidden'
},
content: {
padding: rawTokens.spacing16,
gap: rawTokens.spacing20
},
fields: {
gap: rawTokens.spacing16
},
footer: {
padding: rawTokens.spacing16,
gap: rawTokens.spacing12,
borderTopWidth: 1
}
})