Remove auth on delete for now

This commit is contained in:
Jared Palmer 2023-06-02 14:28:05 -04:00
parent a7af3107b5
commit 01527d450b
4 changed files with 27 additions and 9 deletions

View file

@ -5,13 +5,22 @@ import { chats, db } from "@/lib/db/schema";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
export async function removeChat({ id, path }: { id: string; path: string }) { export async function removeChat({
const session = await auth(); id,
path,
userId,
}: {
id: string;
userId: string;
path: string;
}) {
// @todo next-auth doesn't work in server actions yet
// const session = await auth();
const userId = session?.user?.email; // const userId = session?.user?.email;
if (!userId || !id) { // if (!userId || !id) {
throw new Error("Unauthorized"); // throw new Error("Unauthorized");
} // }
await db await db
.delete(chats) .delete(chats)

View file

@ -11,10 +11,12 @@ export function SidebarItem({
title, title,
href, href,
id, id,
userId,
}: { }: {
title: string; title: string;
href: string; href: string;
id: string; id: string;
userId: string;
}) { }) {
const pathname = usePathname(); const pathname = usePathname();
const router = useRouter(); const router = useRouter();
@ -49,7 +51,7 @@ export function SidebarItem({
onClick={(e) => { onClick={(e) => {
e.preventDefault(); e.preventDefault();
startTransition(() => { startTransition(() => {
removeChat({ id, path: href }).then(() => { removeChat({ id, userId, path: href }).then(() => {
router.push("/"); router.push("/");
}); });
}); });

View file

@ -98,7 +98,7 @@ async function SidebarList({ session }: { session?: Session }) {
where: eq(chats.userId, session?.user?.email || ""), where: eq(chats.userId, session?.user?.email || ""),
}), }),
// @ts-ignore // @ts-ignore
[session?.user?.id || ""], [session?.user?.email ?? ""],
{ {
revalidate: 3600, revalidate: 3600,
} }
@ -106,6 +106,12 @@ async function SidebarList({ session }: { session?: Session }) {
)(); )();
return results.map((c) => ( return results.map((c) => (
<SidebarItem key={c.id} title={c.title} href={`/chat/${c.id}`} id={c.id} /> <SidebarItem
key={c.id}
title={c.title}
userId={session?.user?.email ?? ""}
href={`/chat/${c.id}`}
id={c.id}
/>
)); ));
} }

View file

@ -5,6 +5,7 @@ import { NextResponse } from "next/server";
export const { export const {
handlers: { GET, POST }, handlers: { GET, POST },
auth, auth,
CSRF_experimental,
} = NextAuth({ } = NextAuth({
// @ts-ignore // @ts-ignore
providers: [GitHub], providers: [GitHub],