update two-factor setup

This commit is contained in:
Abhinav 2022-07-01 13:45:08 +05:30
parent d53da12763
commit 5e6c82c1ca
2 changed files with 14 additions and 9 deletions

View file

@ -3,7 +3,9 @@ import { enableTwoFactor, setupTwoFactor } from 'services/userService';
import constants from 'utils/strings/constants'; import constants from 'utils/strings/constants';
import VerticallyCentered from 'components/Container'; import VerticallyCentered from 'components/Container';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import VerifyTwoFactor from 'components/TwoFactor/VerifyForm'; import VerifyTwoFactor, {
VerifyTwoFactorCallback,
} from 'components/TwoFactor/VerifyForm';
import { encryptWithRecoveryKey } from 'utils/crypto'; import { encryptWithRecoveryKey } from 'utils/crypto';
import { setData, LS_KEYS, getData } from 'utils/storage/localStorage'; import { setData, LS_KEYS, getData } from 'utils/storage/localStorage';
import { AppContext, FLASH_MESSAGE_TYPE } from 'pages/_app'; import { AppContext, FLASH_MESSAGE_TYPE } from 'pages/_app';
@ -13,6 +15,7 @@ import Card from '@mui/material/Card';
import { Box, CardContent, Typography } from '@mui/material'; import { Box, CardContent, Typography } from '@mui/material';
import { TwoFactorSetup } from 'components/TwoFactor/Setup'; import { TwoFactorSetup } from 'components/TwoFactor/Setup';
import LinkButton from 'components/pages/gallery/LinkButton'; import LinkButton from 'components/pages/gallery/LinkButton';
import { logError } from 'utils/sentry';
export enum SetupMode { export enum SetupMode {
QR_CODE, QR_CODE,
@ -34,21 +37,21 @@ export default function SetupTwoFactor() {
const twoFactorSecret = await setupTwoFactor(); const twoFactorSecret = await setupTwoFactor();
setTwoFactorSecret(twoFactorSecret); setTwoFactorSecret(twoFactorSecret);
} catch (e) { } catch (e) {
appContext.setDisappearingFlashMessage({ logError(e, 'failed to get two factor setup code');
message: constants.TWO_FACTOR_SETUP_FAILED,
type: FLASH_MESSAGE_TYPE.DANGER,
});
router.push(PAGES.GALLERY);
} }
}; };
main(); main();
}, []); }, []);
const onSubmit = async (otp: string) => { const onSubmit: VerifyTwoFactorCallback = async (
otp: string,
markSuccessful
) => {
const recoveryEncryptedTwoFactorSecret = await encryptWithRecoveryKey( const recoveryEncryptedTwoFactorSecret = await encryptWithRecoveryKey(
twoFactorSecret.secretCode twoFactorSecret.secretCode
); );
await enableTwoFactor(otp, recoveryEncryptedTwoFactorSecret); await enableTwoFactor(otp, recoveryEncryptedTwoFactorSecret);
await markSuccessful();
setData(LS_KEYS.USER, { setData(LS_KEYS.USER, {
...getData(LS_KEYS.USER), ...getData(LS_KEYS.USER),
isTwoFactorEnabled: true, isTwoFactorEnabled: true,

View file

@ -1,4 +1,6 @@
import VerifyTwoFactor from 'components/TwoFactor/VerifyForm'; import VerifyTwoFactor, {
VerifyTwoFactorCallback,
} from 'components/TwoFactor/VerifyForm';
import router from 'next/router'; import router from 'next/router';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { logoutUser, verifyTwoFactor } from 'services/userService'; import { logoutUser, verifyTwoFactor } from 'services/userService';
@ -33,7 +35,7 @@ export default function Home() {
main(); main();
}, []); }, []);
const onSubmit = async (otp: string) => { const onSubmit: VerifyTwoFactorCallback = async (otp) => {
try { try {
const resp = await verifyTwoFactor(otp, sessionID); const resp = await verifyTwoFactor(otp, sessionID);
const { keyAttributes, encryptedToken, token, id } = resp; const { keyAttributes, encryptedToken, token, id } = resp;