2021-06-24 08:31:05 +00:00
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { DeadCenter, SetLoading } from 'pages/gallery';
|
2021-06-29 13:45:59 +00:00
|
|
|
import { AppContext } from 'pages/_app';
|
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2021-06-24 08:31:05 +00:00
|
|
|
import { Button } from 'react-bootstrap';
|
2021-06-29 11:49:34 +00:00
|
|
|
import { disableTwoFactor, getTwoFactorStatus } from 'services/userService';
|
2021-06-24 08:31:05 +00:00
|
|
|
import styled from 'styled-components';
|
|
|
|
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
|
|
|
|
import constants from 'utils/strings/constants';
|
|
|
|
import MessageDialog, { SetDialogMessage } from './MessageDialog';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
show: boolean;
|
|
|
|
onHide: () => void;
|
|
|
|
setDialogMessage: SetDialogMessage;
|
|
|
|
setLoading: SetLoading
|
2021-06-29 13:45:59 +00:00
|
|
|
closeSidebar: () => void;
|
2021-06-24 08:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Row = styled.div`
|
|
|
|
display:flex;
|
|
|
|
align-items:center;
|
|
|
|
margin-bottom:20px;
|
|
|
|
flex:1
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Label = styled.div`
|
|
|
|
width:70%;
|
|
|
|
`;
|
|
|
|
function TwoFactorModal(props: Props) {
|
|
|
|
const router = useRouter();
|
|
|
|
const [isTwoFactorEnabled, setTwoFactorStatus] = useState(false);
|
2021-06-29 13:45:59 +00:00
|
|
|
const appContext = useContext(AppContext);
|
2021-06-24 08:31:05 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!props.show) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const isTwoFactorEnabled = getData(LS_KEYS.USER).isTwoFactorEnabled ?? false;
|
|
|
|
setTwoFactorStatus(isTwoFactorEnabled);
|
2021-06-29 11:49:34 +00:00
|
|
|
const main = async () => {
|
|
|
|
const isTwoFactorEnabled = await getTwoFactorStatus();
|
|
|
|
setTwoFactorStatus(isTwoFactorEnabled);
|
|
|
|
};
|
|
|
|
main();
|
2021-06-24 08:31:05 +00:00
|
|
|
}, [props.show]);
|
|
|
|
const warnTwoFactorDisable = async () => {
|
|
|
|
props.setDialogMessage({
|
2021-06-26 04:56:53 +00:00
|
|
|
title: constants.DISABLE_TWO_FACTOR,
|
2021-06-24 08:31:05 +00:00
|
|
|
staticBackdrop: true,
|
2021-06-26 04:56:53 +00:00
|
|
|
content: constants.DISABLE_TWO_FACTOR_MESSAGE,
|
2021-06-24 08:31:05 +00:00
|
|
|
close: { text: constants.CANCEL },
|
|
|
|
proceed: {
|
|
|
|
variant: 'danger',
|
|
|
|
text: constants.DISABLE,
|
|
|
|
action: twoFactorDisable,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const twoFactorDisable = async () => {
|
2021-06-29 13:45:59 +00:00
|
|
|
try {
|
|
|
|
await disableTwoFactor();
|
|
|
|
setData(LS_KEYS.USER, { ...getData(LS_KEYS.USER), isTwoFactorEnabled: false });
|
|
|
|
props.onHide();
|
|
|
|
props.closeSidebar();
|
|
|
|
appContext.setFlashMessage({ message: constants.TWO_FACTOR_DISABLE_SUCCESS, severity: 'info' });
|
|
|
|
} catch (e) {
|
|
|
|
appContext.setFlashMessage({ message: constants.TWO_FACTOR_DISABLE_FAILED, severity: 'danger' });
|
|
|
|
}
|
2021-06-24 08:31:05 +00:00
|
|
|
};
|
|
|
|
const warnTwoFactorReconfigure = async () => {
|
|
|
|
props.setDialogMessage({
|
2021-06-26 04:56:53 +00:00
|
|
|
title: constants.UPDATE_TWO_FACTOR,
|
2021-06-24 08:31:05 +00:00
|
|
|
staticBackdrop: true,
|
2021-06-25 20:34:56 +00:00
|
|
|
content: constants.UPDATE_TWO_FACTOR_MESSAGE,
|
2021-06-24 08:31:05 +00:00
|
|
|
close: { text: constants.CANCEL },
|
|
|
|
proceed: {
|
|
|
|
variant: 'success',
|
|
|
|
text: constants.UPDATE,
|
|
|
|
action: reconfigureTwoFactor,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const reconfigureTwoFactor = async () => {
|
2021-06-26 04:58:01 +00:00
|
|
|
router.push('/two-factor/setup');
|
2021-06-24 08:31:05 +00:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<MessageDialog
|
|
|
|
show={props.show}
|
|
|
|
onHide={props.onHide}
|
|
|
|
{...(!isTwoFactorEnabled && { size: 'lg' })}
|
|
|
|
attributes={{
|
2021-06-25 19:59:38 +00:00
|
|
|
title: constants.TWO_FACTOR_AUTHENTICATION,
|
2021-06-24 08:31:05 +00:00
|
|
|
staticBackdrop: true,
|
|
|
|
}}
|
|
|
|
|
|
|
|
>
|
|
|
|
<div {...(!isTwoFactorEnabled ? { style: { padding: '10px 40px 30px 40px' } } : { style: { padding: '10px' } })}>
|
|
|
|
{
|
|
|
|
isTwoFactorEnabled ?
|
|
|
|
<>
|
|
|
|
<Row>
|
2021-06-26 04:56:53 +00:00
|
|
|
<Label>{constants.DISABLE_TWO_FACTOR_HINT} </Label><Button variant={'outline-danger'} style={{ width: '30%' }} onClick={warnTwoFactorDisable}>{constants.DISABLE}</Button>
|
2021-06-24 08:31:05 +00:00
|
|
|
</Row>
|
|
|
|
<Row>
|
2021-06-26 04:56:53 +00:00
|
|
|
<Label>{constants.UPDATE_TWO_FACTOR_HINT}</Label> <Button variant={'outline-success'} style={{ width: '30%' }} onClick={warnTwoFactorReconfigure}>{constants.RECONFIGURE}</Button>
|
2021-06-24 08:31:05 +00:00
|
|
|
</Row>
|
|
|
|
</> : (
|
|
|
|
<DeadCenter>
|
|
|
|
<p>{constants.TWO_FACTOR_INFO}</p>
|
|
|
|
<div style={{ height: '10px' }} />
|
2021-06-26 04:58:01 +00:00
|
|
|
<Button variant="outline-success" onClick={() => router.push('/two-factor/setup')}>{constants.ENABLE_TWO_FACTOR}</Button>
|
2021-06-24 08:31:05 +00:00
|
|
|
</DeadCenter>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</MessageDialog >
|
|
|
|
);
|
|
|
|
}
|
|
|
|
export default TwoFactorModal;
|