2024-09-15 00:03:15 +02:00
|
|
|
import {
|
|
|
|
ActionFunctionArgs,
|
|
|
|
json,
|
|
|
|
redirect,
|
|
|
|
LoaderFunctionArgs,
|
|
|
|
MetaFunction,
|
|
|
|
} from "@remix-run/node";
|
2024-11-03 13:53:16 +01:00
|
|
|
import { type ApiError, ErrorCode } from "~/lib/api/error";
|
|
|
|
import serverRequest, { getToken, writeCookie } from "~/lib/request.server";
|
2024-09-14 16:37:27 +02:00
|
|
|
import { AuthResponse, CallbackResponse } from "~/lib/api/auth";
|
|
|
|
import {
|
|
|
|
Form as RemixForm,
|
|
|
|
Link,
|
|
|
|
useActionData,
|
|
|
|
useLoaderData,
|
|
|
|
ShouldRevalidateFunction,
|
2024-09-30 21:40:28 +02:00
|
|
|
useNavigate,
|
2024-09-14 16:37:27 +02:00
|
|
|
} from "@remix-run/react";
|
2024-09-13 14:56:38 +02:00
|
|
|
import { Trans, useTranslation } from "react-i18next";
|
2024-11-03 13:53:16 +01:00
|
|
|
import { Form, Button } from "react-bootstrap";
|
2024-09-15 00:03:15 +02:00
|
|
|
import i18n from "~/i18next.server";
|
2024-09-30 20:14:03 +02:00
|
|
|
import { tokenCookieName } from "~/lib/utils";
|
2024-09-30 21:40:28 +02:00
|
|
|
import { useEffect } from "react";
|
2024-11-03 02:07:07 +01:00
|
|
|
import RegisterError from "~/components/RegisterError";
|
2024-11-03 13:53:16 +01:00
|
|
|
import { AuthMethod } from "~/lib/api/user";
|
|
|
|
import { errorCodeDesc } from "~/components/ErrorAlert";
|
2024-09-15 00:03:15 +02:00
|
|
|
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => {
|
2024-09-25 15:14:48 +02:00
|
|
|
return [{ title: `${data?.meta.title || "Log in"} • pronouns.cc` }];
|
2024-09-15 00:03:15 +02:00
|
|
|
};
|
2024-09-14 16:37:27 +02:00
|
|
|
|
|
|
|
export const shouldRevalidate: ShouldRevalidateFunction = ({ actionResult }) => {
|
|
|
|
return !actionResult;
|
|
|
|
};
|
2024-09-13 14:56:38 +02:00
|
|
|
|
|
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
2024-09-15 00:03:15 +02:00
|
|
|
const t = await i18n.getFixedT(request);
|
2024-09-13 14:56:38 +02:00
|
|
|
const url = new URL(request.url);
|
|
|
|
|
|
|
|
const code = url.searchParams.get("code");
|
|
|
|
const state = url.searchParams.get("state");
|
|
|
|
|
2024-11-03 13:53:16 +01:00
|
|
|
const token = getToken(request);
|
|
|
|
|
2024-09-13 14:56:38 +02:00
|
|
|
if (!code || !state)
|
|
|
|
throw { status: 400, code: ErrorCode.BadRequest, message: "Missing code or state" } as ApiError;
|
|
|
|
|
2024-11-03 13:53:16 +01:00
|
|
|
if (token) {
|
|
|
|
try {
|
|
|
|
const resp = await serverRequest<AuthMethod>("POST", "/auth/discord/add-account/callback", {
|
|
|
|
body: { code, state },
|
|
|
|
token,
|
|
|
|
isInternal: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return json({
|
|
|
|
isLinkRequest: true,
|
|
|
|
meta: { title: t("log-in.callback.title.discord-link") },
|
|
|
|
error: null,
|
|
|
|
hasAccount: false,
|
|
|
|
user: null,
|
|
|
|
ticket: null,
|
|
|
|
remoteUser: null,
|
|
|
|
newAuthMethod: resp,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return json({
|
|
|
|
isLinkRequest: true,
|
|
|
|
meta: { title: t("log-in.callback.title.discord-link") },
|
|
|
|
error: e as ApiError,
|
|
|
|
hasAccount: false,
|
|
|
|
user: null,
|
|
|
|
ticket: null,
|
|
|
|
remoteUser: null,
|
|
|
|
newAuthMethod: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 14:56:38 +02:00
|
|
|
const resp = await serverRequest<CallbackResponse>("POST", "/auth/discord/callback", {
|
2024-09-14 16:37:27 +02:00
|
|
|
body: { code, state },
|
2024-10-02 00:15:14 +02:00
|
|
|
isInternal: true,
|
2024-09-13 14:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.has_account) {
|
|
|
|
return json(
|
2024-09-15 00:03:15 +02:00
|
|
|
{
|
2024-11-03 13:53:16 +01:00
|
|
|
isLinkRequest: false,
|
2024-09-15 00:03:15 +02:00
|
|
|
meta: { title: t("log-in.callback.title.discord-success") },
|
2024-11-03 13:53:16 +01:00
|
|
|
error: null,
|
2024-09-15 00:03:15 +02:00
|
|
|
hasAccount: true,
|
|
|
|
user: resp.user!,
|
|
|
|
ticket: null,
|
|
|
|
remoteUser: null,
|
2024-11-03 13:53:16 +01:00
|
|
|
newAuthMethod: null,
|
2024-09-15 00:03:15 +02:00
|
|
|
},
|
2024-09-13 14:56:38 +02:00
|
|
|
{
|
|
|
|
headers: {
|
2024-09-30 20:14:03 +02:00
|
|
|
"Set-Cookie": writeCookie(tokenCookieName, resp.token!),
|
2024-09-14 16:37:27 +02:00
|
|
|
},
|
|
|
|
},
|
2024-09-13 14:56:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json({
|
2024-11-03 13:53:16 +01:00
|
|
|
isLinkRequest: false,
|
2024-09-15 00:03:15 +02:00
|
|
|
meta: { title: t("log-in.callback.title.discord-register") },
|
2024-11-03 13:53:16 +01:00
|
|
|
error: null,
|
2024-09-13 14:56:38 +02:00
|
|
|
hasAccount: false,
|
|
|
|
user: null,
|
|
|
|
ticket: resp.ticket!,
|
2024-09-14 16:37:27 +02:00
|
|
|
remoteUser: resp.remote_username!,
|
2024-11-03 13:53:16 +01:00
|
|
|
newAuthMethod: null,
|
2024-09-13 14:56:38 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-09-14 16:37:27 +02:00
|
|
|
export const action = async ({ request }: ActionFunctionArgs) => {
|
|
|
|
const data = await request.formData();
|
|
|
|
const username = data.get("username") as string | null;
|
|
|
|
const ticket = data.get("ticket") as string | null;
|
|
|
|
|
|
|
|
if (!username || !ticket)
|
|
|
|
return json({
|
|
|
|
error: {
|
|
|
|
status: 403,
|
|
|
|
code: ErrorCode.BadRequest,
|
|
|
|
message: "Invalid username or ticket",
|
|
|
|
} as ApiError,
|
|
|
|
user: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resp = await serverRequest<AuthResponse>("POST", "/auth/discord/register", {
|
|
|
|
body: { username, ticket },
|
2024-10-02 00:15:14 +02:00
|
|
|
isInternal: true,
|
2024-09-14 16:37:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return redirect("/auth/welcome", {
|
|
|
|
headers: {
|
2024-09-30 20:14:03 +02:00
|
|
|
"Set-Cookie": writeCookie(tokenCookieName, resp.token),
|
2024-09-14 16:37:27 +02:00
|
|
|
},
|
|
|
|
status: 303,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
JSON.stringify(e);
|
|
|
|
|
|
|
|
return json({ error: e as ApiError });
|
|
|
|
}
|
|
|
|
};
|
2024-09-13 14:56:38 +02:00
|
|
|
|
|
|
|
export default function DiscordCallbackPage() {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const data = useLoaderData<typeof loader>();
|
2024-09-14 16:37:27 +02:00
|
|
|
const actionData = useActionData<typeof action>();
|
2024-09-30 21:40:28 +02:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (data.hasAccount) {
|
|
|
|
navigate(`/@${data.user!.username}`);
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2024-09-13 14:56:38 +02:00
|
|
|
|
2024-11-03 13:53:16 +01:00
|
|
|
if (data.isLinkRequest) {
|
|
|
|
if (data.error) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h1>{t("log-in.callback.link-error")}</h1>
|
|
|
|
<p>{errorCodeDesc(t, data.error.code)}</p>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const authMethod = data.newAuthMethod!;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h1>{t("log-in.callback.discord-link-success")}</h1>
|
|
|
|
<p>
|
|
|
|
{t("log-in.callback.discord-link-success-hint", {
|
|
|
|
username: authMethod.remote_username ?? authMethod.remote_id,
|
|
|
|
})}
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-13 14:56:38 +02:00
|
|
|
if (data.hasAccount) {
|
|
|
|
const username = data.user!.username;
|
2024-09-14 16:37:27 +02:00
|
|
|
|
2024-09-13 14:56:38 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h1>{t("log-in.callback.success")}</h1>
|
|
|
|
<p>
|
2024-09-14 16:37:27 +02:00
|
|
|
<Trans
|
|
|
|
t={t}
|
|
|
|
i18nKey={"log-in.callback.success-link"}
|
|
|
|
values={{ username: data.user!.username }}
|
|
|
|
>
|
2024-09-13 14:56:38 +02:00
|
|
|
{/* @ts-expect-error react-i18next handles interpolation here */}
|
2024-09-14 16:37:27 +02:00
|
|
|
Welcome back, <Link to={`/@${data.user!.username}`}>@{{ username }}</Link>!
|
2024-09-13 14:56:38 +02:00
|
|
|
</Trans>
|
|
|
|
<br />
|
|
|
|
{t("log-in.callback.redirect-hint")}
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RemixForm method="POST">
|
|
|
|
<Form as="div">
|
2024-09-14 16:37:27 +02:00
|
|
|
{actionData?.error && <RegisterError error={actionData.error} />}
|
2024-09-13 14:56:38 +02:00
|
|
|
<Form.Group className="mb-3" controlId="remote-username">
|
|
|
|
<Form.Label>{t("log-in.callback.remote-username.discord")}</Form.Label>
|
|
|
|
<Form.Control type="text" readOnly={true} value={data.remoteUser!} />
|
|
|
|
</Form.Group>
|
|
|
|
<Form.Group className="mb-3" controlId="username">
|
|
|
|
<Form.Label>{t("log-in.callback.username")}</Form.Label>
|
|
|
|
<Form.Control name="username" type="text" required />
|
|
|
|
</Form.Group>
|
|
|
|
<input type="hidden" name="ticket" value={data.ticket!} />
|
|
|
|
<Button variant="primary" type="submit">
|
|
|
|
{t("log-in.callback.sign-up-button")}
|
|
|
|
</Button>
|
|
|
|
</Form>
|
|
|
|
</RemixForm>
|
|
|
|
);
|
|
|
|
}
|