import { MetaFunction, json, LoaderFunctionArgs, redirect, ActionFunctionArgs, } from "@remix-run/node"; import { Form as RemixForm, ShouldRevalidateFunction, useActionData, useLoaderData, } from "@remix-run/react"; import { Form, Button, ButtonGroup, ListGroup } from "react-bootstrap"; import { useTranslation } from "react-i18next"; import i18n from "~/i18next.server"; import serverRequest, { getToken, writeCookie } from "~/lib/request.server"; import { AuthResponse, AuthUrls } from "~/lib/api/auth"; import { ApiError, ErrorCode } from "~/lib/api/error"; import ErrorAlert from "~/components/ErrorAlert"; import { User } from "~/lib/api/user"; import { tokenCookieName } from "~/lib/utils"; export const meta: MetaFunction = ({ data }) => { return [{ title: `${data?.meta.title || "Log in"} • pronouns.cc` }]; }; export const shouldRevalidate: ShouldRevalidateFunction = ({ actionResult }) => { return !actionResult; }; export const loader = async ({ request }: LoaderFunctionArgs) => { const t = await i18n.getFixedT(request); const token = getToken(request); if (token) { try { await serverRequest("GET", "/users/@me", { token }); return redirect("/?err=already-logged-in", 303); } catch (e) { // ignore } } const urls = await serverRequest("POST", "/auth/urls", { isInternal: true }); return json({ meta: { title: t("log-in.title") }, urls, }); }; export const action = async ({ request }: ActionFunctionArgs) => { const body = await request.formData(); const email = body.get("email") as string | null; const password = body.get("password") as string | null; try { const resp = await serverRequest("POST", "/auth/email/login", { body: { email, password }, isInternal: true, }); return redirect("/", { status: 303, headers: { "Set-Cookie": writeCookie(tokenCookieName, resp.token), }, }); } catch (e) { return json({ error: e as ApiError }); } }; export default function LoginPage() { const { t } = useTranslation(); const { urls } = useLoaderData(); const actionData = useActionData(); return ( <>
{!urls.email_enabled &&
} {urls.email_enabled && (

{t("log-in.form-title")}

{actionData?.error && }
{t("log-in.email")} {t("log-in.password")}
)}

{t("log-in.3rd-party.title")}

{t("log-in.3rd-party.desc")}

{urls.discord && ( {t("log-in.3rd-party.discord")} )} {urls.google && ( {t("log-in.3rd-party.google")} )} {urls.tumblr && ( {t("log-in.3rd-party.tumblr")} )} {t("log-in.3rd-party.fediverse")}
{!urls.email_enabled &&
}
); } function LoginError({ error }: { error: ApiError }) { const { t } = useTranslation(); if (error.code !== ErrorCode.UserNotFound) return ; return <>{t("log-in.invalid-credentials")}; }