// components/Auth/WebAuthnComponent/WebAuthnComponent.tsx import { Button, Input } from "@/components"; import { useTheme } from "@/context"; import { useAuth } from "@/hooks"; import { startAuthentication, startRegistration, } from "@simplewebauthn/browser"; import { motion } from "framer-motion"; import type React from "react"; import { type CSSProperties, useState } from "react"; /** * WebAuthnComponent is a React functional component that provides * user authentication and registration using WebAuthn. * * @component * @example * return ( * * ) * * @returns {JSX.Element} The rendered component. * * @remarks * This component uses the `useAuth` hook to handle WebAuthn operations * such as initiating and completing login and registration. It also * utilizes the `useTheme` hook to apply theming styles. * * @function * @name WebAuthnComponent * * @hook * @name useAuth * @description Provides methods for WebAuthn operations. * * @hook * @name useTheme * @description Provides theming properties for styling the component. * * @state {string} email - The email input value. * * @async * @function * @name handleLogin * @description Initiates and completes the login process using WebAuthn. * * @async * @function * @name handleRegister * @description Initiates and completes the registration process using WebAuthn. */ export const WebAuthnComponent: React.FC = () => { const [email, setEmail] = useState(""); // const [error, setError] = useState(''); const { initiatePasskeyLogin, initiatePasskeyRegistration, completePasskeyLogin, completePasskeyRegistration, } = useAuth(); const theme = useTheme(); const handleLogin = async () => { try { const publicKey = await initiatePasskeyLogin(email); if (!publicKey) throw new Error("Failed to initiate login"); const credential = await startAuthentication(publicKey); if (!credential) throw new Error("Failed to start authentication"); const auth = await completePasskeyLogin(email, credential); if (!auth) throw new Error("Failed to complete login"); } catch (err: unknown) { if (err instanceof Error) { // setError(err.message); console.error(err.message); } else { console.error("An unknown error occurred"); // setError('An unknown error occurred'); } } }; const handleRegister = async () => { try { const pubKey = await initiatePasskeyRegistration(email); if (!pubKey) throw new Error("Failed to initiate registration"); const credential = await startRegistration(pubKey); if (!credential) throw new Error("Failed to start registration"); await completePasskeyRegistration(email, credential, pubKey); } catch (err: unknown) { if (err instanceof Error) { // setError(err.message); console.error(err.message); } else { console.error("An unknown error occurred"); // setError('An unknown error occurred'); } } }; return (

Login with WebAuthn

setEmail(e.target.value)} placeholder="Email" />
); };