2021-06-04 07:57:48 +00:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2021-04-05 11:09:21 +00:00
|
|
|
import constants from 'utils/strings/constants';
|
2021-05-30 16:56:48 +00:00
|
|
|
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { KeyAttributes } from 'types';
|
|
|
|
import CryptoWorker, { setSessionKeys } from 'utils/crypto';
|
2021-05-11 05:03:09 +00:00
|
|
|
import SingleInputForm from 'components/SingleInputForm';
|
2021-04-07 09:22:59 +00:00
|
|
|
import MessageDialog from 'components/MessageDialog';
|
2021-05-11 05:03:09 +00:00
|
|
|
import Container from 'components/Container';
|
2021-05-30 16:56:48 +00:00
|
|
|
import { Card, Button } from 'react-bootstrap';
|
2021-06-04 07:57:48 +00:00
|
|
|
import { AppContext } from 'pages/_app';
|
2021-06-05 05:13:49 +00:00
|
|
|
import LogoImg from 'components/LogoImg';
|
2021-06-12 17:14:21 +00:00
|
|
|
import { logError } from 'utils/sentry';
|
2021-04-05 11:09:21 +00:00
|
|
|
|
2021-04-05 12:10:52 +00:00
|
|
|
export default function Recover() {
|
2021-04-05 11:09:21 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const [keyAttributes, setKeyAttributes] = useState<KeyAttributes>();
|
2021-04-05 12:10:52 +00:00
|
|
|
const [messageDialogView, SetMessageDialogView] = useState(false);
|
2021-06-04 07:57:48 +00:00
|
|
|
const appContext = useContext(AppContext);
|
|
|
|
|
2021-04-05 11:09:21 +00:00
|
|
|
useEffect(() => {
|
|
|
|
router.prefetch('/gallery');
|
|
|
|
const user = getData(LS_KEYS.USER);
|
|
|
|
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
|
|
|
|
if (!user?.token) {
|
|
|
|
router.push('/');
|
|
|
|
} else if (!keyAttributes) {
|
|
|
|
router.push('/generate');
|
|
|
|
} else {
|
|
|
|
setKeyAttributes(keyAttributes);
|
|
|
|
}
|
2021-06-05 05:13:49 +00:00
|
|
|
appContext.showNavBar(false);
|
2021-04-05 11:09:21 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const recover = async (recoveryKey: string, setFieldError) => {
|
|
|
|
try {
|
|
|
|
const cryptoWorker = await new CryptoWorker();
|
2021-05-29 06:27:52 +00:00
|
|
|
const masterKey: string = await cryptoWorker.decryptB64(
|
2021-04-05 11:09:21 +00:00
|
|
|
keyAttributes.masterKeyEncryptedWithRecoveryKey,
|
|
|
|
keyAttributes.masterKeyDecryptionNonce,
|
2021-05-29 06:27:52 +00:00
|
|
|
await cryptoWorker.fromHex(recoveryKey),
|
2021-04-05 11:09:21 +00:00
|
|
|
);
|
|
|
|
setSessionKeys(masterKey);
|
|
|
|
router.push('/changePassword');
|
|
|
|
} catch (e) {
|
2021-06-12 17:14:21 +00:00
|
|
|
logError(e);
|
2021-04-05 11:09:21 +00:00
|
|
|
setFieldError('passphrase', constants.INCORRECT_RECOVERY_KEY);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-04-05 12:10:52 +00:00
|
|
|
<>
|
2021-05-11 05:03:09 +00:00
|
|
|
<Container>
|
|
|
|
<Card
|
2021-06-05 05:13:49 +00:00
|
|
|
style={{ minWidth: '320px' }}
|
2021-05-11 05:03:09 +00:00
|
|
|
className="text-center"
|
|
|
|
>
|
2021-06-05 05:13:49 +00:00
|
|
|
<Card.Body style={{ padding: '40px 30px' }}>
|
|
|
|
<Card.Title style={{ marginBottom: '32px' }}>
|
|
|
|
<LogoImg src='/icon.svg' />
|
2021-05-11 05:03:09 +00:00
|
|
|
{constants.RECOVER_ACCOUNT}
|
|
|
|
</Card.Title>
|
|
|
|
<SingleInputForm
|
|
|
|
callback={recover}
|
|
|
|
fieldType="text"
|
|
|
|
placeholder={constants.RETURN_RECOVERY_KEY_HINT}
|
|
|
|
buttonText={constants.RECOVER}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
marginTop: '12px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
onClick={() => SetMessageDialogView(true)}
|
|
|
|
>
|
|
|
|
{constants.NO_RECOVERY_KEY}
|
|
|
|
</Button>
|
|
|
|
<Button variant="link" onClick={router.back}>
|
|
|
|
{constants.GO_BACK}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Card.Body>
|
|
|
|
</Card>
|
|
|
|
</Container>
|
2021-04-05 12:10:52 +00:00
|
|
|
<MessageDialog
|
2021-05-29 06:27:52 +00:00
|
|
|
size="lg"
|
2021-04-05 12:10:52 +00:00
|
|
|
show={messageDialogView}
|
|
|
|
onHide={() => SetMessageDialogView(false)}
|
|
|
|
attributes={{
|
|
|
|
title: constants.SORRY,
|
2021-04-07 07:18:45 +00:00
|
|
|
close: {},
|
2021-05-11 05:03:09 +00:00
|
|
|
content: constants.NO_RECOVERY_KEY_MESSAGE,
|
2021-04-05 12:10:52 +00:00
|
|
|
}}
|
2021-05-11 07:52:46 +00:00
|
|
|
/>
|
2021-04-05 12:10:52 +00:00
|
|
|
</>
|
2021-04-05 11:09:21 +00:00
|
|
|
);
|
|
|
|
}
|