import { json, redirect, type ActionFunctionArgs, type LoaderFunctionArgs, type TypedResponse, } from '@remix-run/cloudflare'; import { Form, useActionData } from '@remix-run/react'; import { verifyPassword } from '~/lib/.server/login'; import { createUserSession, isAuthenticated } from '~/lib/.server/sessions'; interface Errors { password?: string; } export async function loader({ request, context }: LoaderFunctionArgs) { const authenticated = await isAuthenticated(request, context.cloudflare.env); if (authenticated) { return redirect('/'); } return json({}); } export async function action({ request, context }: ActionFunctionArgs): Promise> { const formData = await request.formData(); const password = String(formData.get('password')); const errors: Errors = {}; if (!password) { errors.password = 'Please provide a password'; } if (!verifyPassword(password, context.cloudflare.env)) { errors.password = 'Invalid password'; } if (Object.keys(errors).length > 0) { return json({ errors }); } return redirect('/', await createUserSession(request, context.cloudflare.env)); } export default function Login() { const actionData = useActionData(); return (

Login

{actionData?.errors?.password ? (
{actionData?.errors.password}
) : null}
); }