Skip to content

Instantly share code, notes, and snippets.

@alancasagrande
Last active November 16, 2020 12:07
Show Gist options
  • Save alancasagrande/ad4f30b1d7469c50cb4aa06a7a2920a9 to your computer and use it in GitHub Desktop.
Save alancasagrande/ad4f30b1d7469c50cb4aa06a7a2920a9 to your computer and use it in GitHub Desktop.
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