import { ArrowsClockwise, Clipboard, Eye, EyeSlash } from 'phosphor-react'; import { useState } from 'react'; import { Algorithm, HASHING_ALGOS, HashingAlgoSlug, hashingAlgoSlugSchema, useLibraryMutation } from '@sd/client'; import { Button, Dialog, Input, Select, SelectOption, UseDialogProps, useDialog } from '@sd/ui'; import { useZodForm, z } from '@sd/ui/src/forms'; import { PasswordMeter, showAlertDialog } from '~/components'; import { generatePassword } from '~/util'; const schema = z.object({ masterPassword: z.string(), masterPassword2: z.string(), encryptionAlgo: z.string(), hashingAlgo: hashingAlgoSlugSchema }); export default (props: UseDialogProps) => { const changeMasterPassword = useLibraryMutation('keys.changeMasterPassword', { onSuccess: () => { showAlertDialog({ title: 'Success', value: 'Your master password was changed successfully' }); }, onError: () => { // this should never really happen showAlertDialog({ title: 'Master Password Change Error', value: 'There was an error while changing your master password.' }); } }); const [show, setShow] = useState({ masterPassword: false, masterPassword2: false }); const MP1CurrentEyeIcon = show.masterPassword ? EyeSlash : Eye; const MP2CurrentEyeIcon = show.masterPassword2 ? EyeSlash : Eye; const form = useZodForm({ schema, defaultValues: { encryptionAlgo: 'XChaCha20Poly1305', hashingAlgo: 'Argon2id-s', masterPassword: '', masterPassword2: '' } }); const onSubmit: Parameters[0] = (data) => { if (data.masterPassword !== data.masterPassword2) { showAlertDialog({ title: 'Error', value: 'Passwords are not the same, please try again.' }); } else { const hashing_algorithm = HASHING_ALGOS[data.hashingAlgo]; return changeMasterPassword.mutateAsync({ algorithm: data.encryptionAlgo as Algorithm, hashing_algorithm, password: data.masterPassword }); } }; return ( } /> setShow((old) => ({ ...old, masterPassword2: !old.masterPassword2 })) } size="icon" type="button" > } />
Encryption
Hashing
); };