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 { revalidatePath } from "next/cache";
export async function removeChat({ id, path }: { id: string; path: string }) {
const session = await auth();
export async function removeChat({
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;
if (!userId || !id) {
throw new Error("Unauthorized");
}
// const userId = session?.user?.email;
// if (!userId || !id) {
// throw new Error("Unauthorized");
// }
await db
.delete(chats)

View file

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

View file

@ -98,7 +98,7 @@ async function SidebarList({ session }: { session?: Session }) {
where: eq(chats.userId, session?.user?.email || ""),
}),
// @ts-ignore
[session?.user?.id || ""],
[session?.user?.email ?? ""],
{
revalidate: 3600,
}
@ -106,6 +106,12 @@ async function SidebarList({ session }: { session?: Session }) {
)();
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}
/>
));
}