53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
import { redirect } from "@remix-run/node";
|
|
import { getMe, getOAuthToken } from "~/utils";
|
|
import { Link } from "@remix-run/react";
|
|
import { getSession } from "~/sessions";
|
|
import { commitSession } from "~/sessions";
|
|
|
|
export async function action({ request }) {
|
|
const formData = await request.formData();
|
|
const county = formData.get("county");
|
|
|
|
if (typeof county !== "string" || !county || !countys.includes(county))
|
|
throw new Error("bad county");
|
|
}
|
|
|
|
export async function loader({ request }) {
|
|
const session = await getSession(request.headers.get("Cookie"));
|
|
if (session.has("userId")) {
|
|
return redirect("/");
|
|
}
|
|
|
|
const url = new URL(request.url);
|
|
const code = url.searchParams.get("code");
|
|
if (!code) return redirect("/");
|
|
|
|
const data = await getOAuthToken(code);
|
|
if (data.error) {
|
|
throw new Error("could not get access_token");
|
|
}
|
|
|
|
const me = await getMe(data.access_token);
|
|
if (!["FI", "AX"].includes(me.country.code)) {
|
|
throw new Error("your not finnish lil bro");
|
|
}
|
|
|
|
session.set("userId", data.access_token);
|
|
|
|
return redirect("/", {
|
|
headers: {
|
|
"Set-Cookie": await commitSession(session),
|
|
},
|
|
});
|
|
}
|
|
|
|
export function ErrorBoundary({ error }) {
|
|
return (
|
|
<div className="bg-red rounded p-4 text-white shadow">
|
|
<Link to={"/"}>Back</Link>
|
|
<h1>{error.message}</h1>
|
|
<code>{error.stack}</code>
|
|
</div>
|
|
);
|
|
}
|