Last active
November 16, 2020 12:07
-
-
Save alancasagrande/ad4f30b1d7469c50cb4aa06a7a2920a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback, useState } from 'react'; | |
import { verifyOtp } from './api'; | |
import Input from './Input'; | |
export default function OneTimePassword({ enabled }) { | |
const [verificationCode, setVerificationCode] = useState(''); | |
const [invalidCode, setInvalidCode] = useState(false); | |
const handleSubmit = useCallback( | |
async (e) => { | |
e.preventDefault(); | |
const result = await verifyOtp(verificationCode); | |
if (result) return (window.location = '/'); | |
setInvalidCode(true); | |
}, | |
[verificationCode] | |
); | |
return ( | |
<div> | |
{!enabled && ( | |
<div> | |
<p>Scan the QR code on your authenticator app</p> | |
<img src="/mfa_qr_code" /> | |
</div> | |
)} | |
<form onSubmit={handleSubmit}> | |
<Input | |
id="verificationCode" | |
label="Verification code" | |
type="text" | |
value={verificationCode} | |
onChange={(e) => setVerificationCode(e.target.value)} | |
/> | |
<button type="submit">Confirm</button> | |
{invalidCode && <p>Invalid verification code</p>} | |
</form> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment