2024-11-24 15:55:29 +01:00
|
|
|
import { apiRequest } from "$api";
|
2024-11-28 21:35:55 +01:00
|
|
|
import ApiError, { ErrorCode } from "$api/error";
|
|
|
|
import type { CallbackResponse } from "$api/models/auth.js";
|
2024-11-24 15:55:29 +01:00
|
|
|
import { setToken } from "$lib";
|
2024-11-28 21:35:55 +01:00
|
|
|
import createRegisterAction from "$lib/actions/register.js";
|
|
|
|
import { redirect } from "@sveltejs/kit";
|
2024-11-24 15:55:29 +01:00
|
|
|
|
|
|
|
export const load = async ({ parent, params, url, fetch, cookies }) => {
|
|
|
|
const { meUser } = await parent();
|
|
|
|
if (meUser) redirect(303, `/@${meUser.username}`);
|
|
|
|
|
|
|
|
const code = url.searchParams.get("code") as string | null;
|
|
|
|
const state = url.searchParams.get("state") as string | null;
|
|
|
|
if (!code || !state) throw new ApiError(undefined, ErrorCode.BadRequest).obj;
|
|
|
|
|
|
|
|
const resp = await apiRequest<CallbackResponse>("POST", "/auth/fediverse/callback", {
|
|
|
|
body: { code, state, instance: params.instance },
|
|
|
|
isInternal: true,
|
|
|
|
fetch,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.has_account) {
|
|
|
|
setToken(cookies, resp.token!);
|
|
|
|
redirect(303, `/@${resp.user!.username}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
hasAccount: false,
|
|
|
|
instance: params.instance,
|
|
|
|
ticket: resp.ticket!,
|
|
|
|
remoteUser: resp.remote_username!,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
2024-11-28 21:35:55 +01:00
|
|
|
default: createRegisterAction("/auth/fediverse/register"),
|
2024-11-24 15:55:29 +01:00
|
|
|
};
|