diff options
author | Paul Frazee <pfrazee@gmail.com> | 2024-04-22 19:18:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-22 19:18:13 -0700 |
commit | 710e913024bab2d1c4e4f6179b089afd8eb5ba9f (patch) | |
tree | c227ae63e4b3eb79f0ab7d936126e0eaf576b526 /src/view/screens/Settings/Email2FAToggle.tsx | |
parent | cbb817b5b707042afefbf8ca46a7104d62349492 (diff) | |
download | voidsky-710e913024bab2d1c4e4f6179b089afd8eb5ba9f.tar.zst |
Email auth factor (#3602)
* Add email 2fa toggle * Add UI elements needed for 2fa codes in login * Wire up to the server * Give a better failure message for bad 2fa code * Handle enter key in login form 2fa field * Trim spaces * Improve error message
Diffstat (limited to 'src/view/screens/Settings/Email2FAToggle.tsx')
-rw-r--r-- | src/view/screens/Settings/Email2FAToggle.tsx | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/view/screens/Settings/Email2FAToggle.tsx b/src/view/screens/Settings/Email2FAToggle.tsx new file mode 100644 index 000000000..93f1b2042 --- /dev/null +++ b/src/view/screens/Settings/Email2FAToggle.tsx @@ -0,0 +1,60 @@ +import React from 'react' +import {msg} from '@lingui/macro' +import {useLingui} from '@lingui/react' + +import {useModalControls} from '#/state/modals' +import {getAgent, useSession, useSessionApi} from '#/state/session' +import {ToggleButton} from 'view/com/util/forms/ToggleButton' +import {useDialogControl} from '#/components/Dialog' +import {DisableEmail2FADialog} from './DisableEmail2FADialog' + +export function Email2FAToggle() { + const {_} = useLingui() + const {currentAccount} = useSession() + const {updateCurrentAccount} = useSessionApi() + const {openModal} = useModalControls() + const disableDialogCtrl = useDialogControl() + + const enableEmailAuthFactor = React.useCallback(async () => { + if (currentAccount?.email) { + await getAgent().com.atproto.server.updateEmail({ + email: currentAccount.email, + emailAuthFactor: true, + }) + updateCurrentAccount({ + emailAuthFactor: true, + }) + } + }, [currentAccount, updateCurrentAccount]) + + const onToggle = React.useCallback(() => { + if (!currentAccount) { + return + } + if (currentAccount.emailAuthFactor) { + disableDialogCtrl.open() + } else { + if (!currentAccount.emailConfirmed) { + openModal({ + name: 'verify-email', + onSuccess: enableEmailAuthFactor, + }) + return + } + enableEmailAuthFactor() + } + }, [currentAccount, enableEmailAuthFactor, openModal, disableDialogCtrl]) + + return ( + <> + <DisableEmail2FADialog control={disableDialogCtrl} /> + <ToggleButton + type="default-light" + label={_(msg`Require email code to log into your account`)} + labelType="lg" + isSelected={!!currentAccount?.emailAuthFactor} + onPress={onToggle} + /> + </> + ) +} |