Implement lists module and dev login setup
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import postgres from "postgres";
|
||||
import { getCurrentSession } from "@/lib/session";
|
||||
import { canAccessList } from "@/modules/lists/server/queries";
|
||||
import { listChannel } from "@/modules/lists/server/realtime";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
const { household } = await getCurrentSession();
|
||||
if (!(await canAccessList(id, household.id))) return new Response("Forbidden", { status: 403 });
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const sql = postgres(process.env["DATABASE_URL"]!, { max: 1 });
|
||||
let heartbeat: ReturnType<typeof setInterval> | undefined;
|
||||
let listener: { unlisten(): Promise<void> } | undefined;
|
||||
let closed = false;
|
||||
|
||||
async function cleanup() {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
if (heartbeat) clearInterval(heartbeat);
|
||||
await listener?.unlisten().catch(() => undefined);
|
||||
await sql.end({ timeout: 1 }).catch(() => undefined);
|
||||
}
|
||||
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
controller.enqueue(encoder.encode(": connected\n\n"));
|
||||
|
||||
listener = await sql.listen(listChannel(id), (payload) => {
|
||||
if (closed) return;
|
||||
controller.enqueue(encoder.encode(`data: ${payload}\n\n`));
|
||||
});
|
||||
|
||||
heartbeat = setInterval(() => {
|
||||
if (closed) return;
|
||||
controller.enqueue(encoder.encode(": heartbeat\n\n"));
|
||||
}, 25_000);
|
||||
|
||||
request.signal.addEventListener("abort", () => {
|
||||
void cleanup();
|
||||
});
|
||||
},
|
||||
cancel() {
|
||||
return cleanup();
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
"Cache-Control": "no-cache, no-transform",
|
||||
Connection: "keep-alive",
|
||||
"Content-Type": "text/event-stream",
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { notFound } from "next/navigation";
|
||||
import { ListDetail } from "@/modules/lists/components/list-detail";
|
||||
import { getList } from "@/modules/lists/server/queries";
|
||||
|
||||
export default async function ListPage({ params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
const list = await getList(id).catch(() => null);
|
||||
if (!list) notFound();
|
||||
|
||||
return <ListDetail initialList={list} />;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { ListsIndex } from "@/modules/lists/components/lists-index";
|
||||
import { listLists } from "@/modules/lists/server/queries";
|
||||
|
||||
export default async function ListsPage() {
|
||||
const lists = await listLists();
|
||||
return <ListsIndex lists={lists} />;
|
||||
}
|
||||
@@ -1,7 +1,13 @@
|
||||
import { signIn } from "@/lib/auth";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { DEV_LOGIN_COOKIE, isDevLoginEnabled } from "@/lib/dev-login-config";
|
||||
import { createDevSession } from "@/lib/dev-login";
|
||||
|
||||
export default function LoginPage() {
|
||||
const devLoginEnabled = isDevLoginEnabled();
|
||||
|
||||
return (
|
||||
<main className="flex min-h-screen flex-col items-center justify-center p-8">
|
||||
<div className="flex flex-col items-center gap-6">
|
||||
@@ -16,6 +22,26 @@ export default function LoginPage() {
|
||||
Sign in with SSO
|
||||
</Button>
|
||||
</form>
|
||||
{devLoginEnabled && (
|
||||
<form
|
||||
action={async () => {
|
||||
"use server";
|
||||
const { sessionToken, expires } = await createDevSession();
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set(DEV_LOGIN_COOKIE, sessionToken, {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
expires,
|
||||
});
|
||||
redirect("/");
|
||||
}}
|
||||
>
|
||||
<Button type="submit" variant="outline" size="lg">
|
||||
Dev login
|
||||
</Button>
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user