PteroTheme/resources/scripts/components/dashboard/forms/ConfigureTwoFactorForm.tsx

48 lines
2.0 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { useStoreState } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import tw from 'twin.macro';
2022-07-02 23:53:03 +01:00
import { Button } from '@/components/elements/button/index';
import SetupTOTPDialog from '@/components/dashboard/forms/SetupTOTPDialog';
import RecoveryTokensDialog from '@/components/dashboard/forms/RecoveryTokensDialog';
import DisableTOTPDialog from '@/components/dashboard/forms/DisableTOTPDialog';
import { useFlashKey } from '@/plugins/useFlash';
export default () => {
const [tokens, setTokens] = useState<string[]>([]);
2022-07-02 23:53:03 +01:00
const [visible, setVisible] = useState<'enable' | 'disable' | null>(null);
const isEnabled = useStoreState((state: ApplicationStore) => state.user.data!.useTotp);
const { clearAndAddHttpError } = useFlashKey('account:two-step');
useEffect(() => {
return () => {
clearAndAddHttpError();
};
}, [visible]);
const onTokens = (tokens: string[]) => {
setTokens(tokens);
setVisible(null);
};
return (
<div>
<SetupTOTPDialog open={visible === 'enable'} onClose={() => setVisible(null)} onTokens={onTokens} />
<RecoveryTokensDialog tokens={tokens} open={tokens.length > 0} onClose={() => setTokens([])} />
<DisableTOTPDialog open={visible === 'disable'} onClose={() => setVisible(null)} />
<p css={tw`text-sm`}>
{isEnabled
2022-07-02 23:53:03 +01:00
? 'Two-step verification is currently enabled on your account.'
: 'You do not currently have two-step verification enabled on your account. Click the button below to begin configuring it.'}
</p>
<div css={tw`mt-6`}>
2022-07-02 23:53:03 +01:00
{isEnabled ? (
<Button.Danger onClick={() => setVisible('disable')}>Disable Two-Step</Button.Danger>
) : (
<Button onClick={() => setVisible('enable')}>Enable Two-Step</Button>
)}
</div>
</div>
);
};