2023-09-04 08:40:05 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingStatusContext } from 'shared/context/loading-status-context.mjs'
|
2023-03-24 16:33:14 +01:00
|
|
|
// Hooks
|
2023-09-04 12:18:52 +02:00
|
|
|
import { useState, useContext } from 'react'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-03-24 16:33:14 +01:00
|
|
|
import { useAccount } from 'shared/hooks/use-account.mjs'
|
|
|
|
import { useBackend } from 'shared/hooks/use-backend.mjs'
|
2023-02-19 20:49:15 +01:00
|
|
|
// Components
|
2023-02-19 20:58:06 +01:00
|
|
|
import { BackToAccountButton } from './shared.mjs'
|
2023-07-23 18:42:06 +02:00
|
|
|
import { Popout } from 'shared/components/popout/index.mjs'
|
2023-05-19 08:34:08 +02:00
|
|
|
import { Bullet } from 'shared/components/bullet.mjs'
|
2023-02-19 20:49:15 +01:00
|
|
|
|
|
|
|
export const ns = ['account']
|
|
|
|
|
|
|
|
const CodeInput = ({ code, setCode, t }) => (
|
|
|
|
<input
|
|
|
|
value={code}
|
|
|
|
onChange={(evt) => setCode(evt.target.value)}
|
|
|
|
className="input w-full text-4xl input-bordered input-lg flex flex-row text-center mb-8 tracking-widest"
|
|
|
|
type="number"
|
|
|
|
placeholder={t('000000')}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
export const MfaSettings = ({ title = false, welcome = false }) => {
|
|
|
|
// Hooks
|
2023-08-20 18:48:40 +02:00
|
|
|
const { account, setAccount } = useAccount()
|
|
|
|
const backend = useBackend()
|
2023-02-19 20:49:15 +01:00
|
|
|
const { t } = useTranslation(ns)
|
2023-09-04 08:40:05 +02:00
|
|
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
2023-02-19 20:49:15 +01:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// State
|
2023-02-19 20:49:15 +01:00
|
|
|
const [enable, setEnable] = useState(false)
|
|
|
|
const [disable, setDisable] = useState(false)
|
|
|
|
const [code, setCode] = useState('')
|
|
|
|
const [password, setPassword] = useState('')
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to enable MFA
|
2023-02-19 20:49:15 +01:00
|
|
|
const enableMfa = async () => {
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'processingUpdate'])
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await backend.enableMfa()
|
2023-08-19 18:01:22 +02:00
|
|
|
if (result.success) {
|
|
|
|
setEnable(result.data.mfa)
|
|
|
|
setLoadingStatus([true, 'settingsSaved', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-02-19 20:49:15 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to disable MFA
|
2023-02-19 20:49:15 +01:00
|
|
|
const disableMfa = async () => {
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'processingUpdate'])
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await backend.disableMfa({
|
|
|
|
mfa: false,
|
|
|
|
password,
|
|
|
|
token: code,
|
|
|
|
})
|
|
|
|
if (result) {
|
2023-03-24 16:33:14 +01:00
|
|
|
if (result.success) {
|
|
|
|
setAccount(result.data.account)
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'settingsSaved', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-02-19 20:49:15 +01:00
|
|
|
setDisable(false)
|
|
|
|
setEnable(false)
|
|
|
|
setCode('')
|
|
|
|
setPassword('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to confirm MFA
|
2023-02-19 20:49:15 +01:00
|
|
|
const confirmMfa = async () => {
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'processingUpdate'])
|
2023-02-19 20:49:15 +01:00
|
|
|
const result = await backend.confirmMfa({
|
|
|
|
mfa: true,
|
|
|
|
secret: enable.secret,
|
|
|
|
token: code,
|
|
|
|
})
|
2023-03-24 16:33:14 +01:00
|
|
|
if (result.success) {
|
|
|
|
setAccount(result.data.account)
|
2023-08-19 18:01:22 +02:00
|
|
|
setLoadingStatus([true, 'settingsSaved', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-02-19 20:49:15 +01:00
|
|
|
setEnable(false)
|
|
|
|
setCode('')
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Figure out what title to use
|
2023-03-24 16:33:14 +01:00
|
|
|
let titleText = account.mfaEnabled ? t('mfaEnabled') : t('mfaDisabled')
|
2023-02-19 20:49:15 +01:00
|
|
|
if (enable) titleText = t('mfaSetup')
|
|
|
|
|
|
|
|
return (
|
2023-03-27 19:21:27 +02:00
|
|
|
<div className="max-w-xl">
|
2023-02-19 20:49:15 +01:00
|
|
|
{title ? <h2 className="text-4xl">{titleText}</h2> : null}
|
|
|
|
{enable ? (
|
|
|
|
<>
|
2023-03-24 16:33:14 +01:00
|
|
|
<div className="flex flex-row items-center justify-center px-8 lg:px-36">
|
2023-02-19 20:58:06 +01:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: enable.qrcode }} />
|
2023-02-19 20:49:15 +01:00
|
|
|
</div>
|
|
|
|
<Bullet num="1">{t('mfaAdd')}</Bullet>
|
|
|
|
<Bullet num="2">{t('confirmWithMfa')}</Bullet>
|
|
|
|
<input
|
|
|
|
value={code}
|
|
|
|
onChange={(evt) => setCode(evt.target.value)}
|
|
|
|
className="input w-64 m-auto text-4xl input-bordered input-lg flex flex-row text-center mb-8 tracking-widest"
|
|
|
|
type="number"
|
|
|
|
placeholder={t('000000')}
|
|
|
|
/>
|
|
|
|
<button className="btn btn-success btn-lg block w-full" onClick={confirmMfa}>
|
|
|
|
{t('enableMfa')}
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
{disable ? (
|
|
|
|
<div className="my-8">
|
|
|
|
<Bullet num="1">
|
|
|
|
<h5>{t('confirmWithPassword')}</h5>
|
|
|
|
<input
|
|
|
|
value={password}
|
|
|
|
onChange={(evt) => setPassword(evt.target.value)}
|
|
|
|
className="input w-full input-bordered flex flex-row"
|
|
|
|
type="text"
|
|
|
|
placeholder={t('passwordPlaceholder')}
|
|
|
|
/>
|
|
|
|
</Bullet>
|
|
|
|
<Bullet num="2">
|
|
|
|
<h5>{t('confirmWithMfa')}</h5>
|
|
|
|
<CodeInput code={code} setCode={setCode} t={t} />
|
|
|
|
</Bullet>
|
|
|
|
<button
|
|
|
|
className="btn btn-error btn-lg block w-full"
|
|
|
|
onClick={disableMfa}
|
|
|
|
disabled={code.length < 4 || password.length < 3}
|
|
|
|
>
|
|
|
|
{t('disableMfa')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<div className="flex flex-row items-center mt-4">
|
2023-03-24 16:33:14 +01:00
|
|
|
{account.mfaEnabled ? (
|
2023-02-19 20:49:15 +01:00
|
|
|
disable ? null : (
|
|
|
|
<button className="btn btn-primary block w-full" onClick={() => setDisable(true)}>
|
|
|
|
{t('disableMfa')}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
) : enable ? null : (
|
|
|
|
<div>
|
|
|
|
<button className="btn btn-primary block w-full" onClick={enableMfa}>
|
|
|
|
{t('mfaSetup')}
|
|
|
|
</button>
|
|
|
|
<Popout tip>
|
|
|
|
<h5>{t('mfaTipTitle')}</h5>
|
|
|
|
<p>{t('mfaTipMsg')}</p>
|
|
|
|
</Popout>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-08-19 18:01:22 +02:00
|
|
|
{!welcome && <BackToAccountButton />}
|
2023-03-27 19:21:27 +02:00
|
|
|
</div>
|
2023-02-19 20:49:15 +01:00
|
|
|
)
|
|
|
|
}
|