Skip to content

Commit

Permalink
feat: allow users to replace 2fa
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Oct 17, 2024
1 parent 2ae0fdd commit da387d5
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion app/routes/app.user.totp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {useLoaderData, useActionData} from '@remix-run/react'
import {invariant, omit} from '@arcath/utils'
import {generateTOTP, getTOTPAuthUri, verifyTOTP} from '@epic-web/totp'
import {toDataURL} from 'qrcode'
import {useState} from 'react'

import {ensureUser} from '~/lib/utils/ensure-user'
import {getPrisma} from '~/lib/prisma.server'
Expand Down Expand Up @@ -90,6 +91,7 @@ export const meta: MetaFunction = () => {
const UserTOTP = () => {
const {state, dataURL, genTotp} = useLoaderData<typeof loader>()
const data = useActionData<typeof action>()
const [open, setOpen] = useState(false)

if (data) {
if (data.result) {
Expand All @@ -100,7 +102,47 @@ const UserTOTP = () => {
}

if (state) {
return <div className="entry">2FA is Setup, Remove?</div>
return (
<div className="entry">
<h2>2FA</h2>
<p>2FA is Setup on your account.</p>
{open ? (
<div>
<img src={dataURL} alt="2FA QR Code" />
<form method="POST">
<Label>
Verification Code
<Input name="verify" />
</Label>
<input
type="hidden"
name="totp"
value={JSON.stringify(genTotp)}
/>
<Button className="bg-success">Replace 2FA</Button>
<Button
onClick={() => setOpen(false)}
type="button"
className="bg-danger"
>
Cancel
</Button>
</form>
</div>
) : (
<div>
<Button
onClick={() => {
setOpen(true)
}}
className="bg-info"
>
Replace Authenticator
</Button>
</div>
)}
</div>
)
}

return (
Expand Down

0 comments on commit da387d5

Please sign in to comment.