"use client"; import { confirmEmailAuthToken, requestAuthEmailToken, } from "actions/emailAuth"; import { loginWithEmailToken } from "actions/login"; import { ActionAfterSignIn } from "app/api/oauth/[route]/afterSignInActions"; import { getHomeDocs } from "app/(home-pages)/home/storage"; import { ButtonPrimary } from "components/Buttons"; import { ArrowRightTiny } from "components/Icons/ArrowRightTiny"; import { BlueskySmall } from "components/Icons/BlueskySmall"; import { Input } from "components/Input"; import { useSmoker, useToaster } from "components/Toast"; import React, { useState } from "react"; import { mutate } from "swr"; import { BlueskyTiny } from "components/Icons/BlueskyTiny"; export default function LoginForm(props: { noEmail?: boolean; redirectRoute?: string; action?: ActionAfterSignIn; text: React.ReactNode; }) { type FormState = | { stage: "email"; email: string; } | { stage: "code"; email: string; tokenId: string; confirmationCode: string; }; const [formState, setFormState] = useState({ stage: "email", email: "", }); const handleSubmitEmail = async (e: React.FormEvent) => { e.preventDefault(); const tokenId = await requestAuthEmailToken(formState.email); setFormState({ stage: "code", email: formState.email, tokenId, confirmationCode: "", }); }; let smoker = useSmoker(); let toaster = useToaster(); const handleSubmitCode = async (e: React.FormEvent) => { e.preventDefault(); let rect = e.currentTarget.getBoundingClientRect(); if (formState.stage !== "code") return; const confirmedToken = await confirmEmailAuthToken( formState.tokenId, formState.confirmationCode, ); if (!confirmedToken) { smoker({ error: true, text: "incorrect code!", position: { y: rect.bottom - 16, x: rect.right - 220, }, }); } else { let localLeaflets = getHomeDocs(); await loginWithEmailToken(localLeaflets.filter((l) => !l.hidden)); mutate("identity"); toaster({ content:
Logged in! Welcome!
, type: "success", }); } }; if (formState.stage === "code") { return (
Please enter the code we sent to
{formState.email}
setFormState({ ...formState, confirmationCode: e.target.value, }) } required /> {}} > Confirm
); } return (

Log In or Sign Up

{props.text}
{props.noEmail ? null : ( <>

or

setFormState({ ...formState, email: e.target.value, }) } required /> {" "}
)}
); } export function BlueskyLogin(props: { redirectRoute?: string; action?: ActionAfterSignIn; compact?: boolean; }) { const [signingWithHandle, setSigningWithHandle] = useState(false); const [handle, setHandle] = useState(""); return (
{props.action && ( )} {signingWithHandle ? (
setHandle(e.target.value)} required /> Sign In
) : (
{props.compact ? : } {props.compact ? "Link" : "Log In/Sign Up with"} Bluesky
)}
); }