diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts index fb2b3fd..9472ae0 100644 --- a/src/app/actions/auth.ts +++ b/src/app/actions/auth.ts @@ -82,6 +82,12 @@ export async function register(_prevState: unknown, formData: FormData) { redirect("/notes"); } +export async function logOut() { + const cookieStore = await cookies(); + cookieStore.delete("session"); + redirect("/auth"); +} + export async function getAuth() { const cookieStore = await cookies(); const token = cookieStore.get("session")?.value; @@ -109,8 +115,10 @@ export async function getAuth() { } } -export async function logOut() { - const cookieStore = await cookies(); - cookieStore.delete("session"); - redirect("/auth"); +export async function requireAuth() { + const user = await getAuth(); + if (!user) { + redirect("/auth"); + } + return user; } diff --git a/src/app/actions/notes.ts b/src/app/actions/notes.ts index 2401749..f590d36 100644 --- a/src/app/actions/notes.ts +++ b/src/app/actions/notes.ts @@ -2,11 +2,13 @@ import { revalidatePath } from "next/cache"; import { redirect } from "next/navigation"; -import { desc, eq } from "drizzle-orm"; +import { desc, eq, and } from "drizzle-orm"; +import { INote, notesTable, usersTable } from "@/lib/db/schema"; import { db } from "@/lib/db"; -import { INote, IUser, notesTable, usersTable } from "@/lib/db/schema"; +import { requireAuth } from "./auth"; -export async function createNote(user: IUser) { +export async function createNote() { + const user = await requireAuth(); const result = await db .insert(notesTable) .values({ authorId: user.id }) @@ -15,7 +17,8 @@ export async function createNote(user: IUser) { redirect(`/notes/${id}`); } -export async function getNotes(user: IUser): Promise { +export async function getNotes(): Promise { + const user = await requireAuth(); return db .select() .from(notesTable) @@ -23,9 +26,11 @@ export async function getNotes(user: IUser): Promise { .orderBy(desc(notesTable.lastEdited)); } -export async function deleteNote(note: INote) { +export async function deleteNote(formData: FormData) { + const user = await requireAuth(); + const noteId = formData.get("noteId") as string; await db .delete(notesTable) - .where(eq(notesTable.id, note.id)); + .where(and(eq(notesTable.id, noteId), eq(notesTable.authorId, user.id))); revalidatePath("/notes"); } diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 7ce4435..4228bc4 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,6 +1,5 @@ import { Metadata } from "next"; import { Noto_Sans_Mono } from "next/font/google"; -import { getAuth } from "./actions/auth"; import Header from "@/components/Header"; import "./globals.css"; @@ -19,12 +18,10 @@ export default async function RootLayout({ }: Readonly<{ children: React.ReactNode; }>) { - const user = await getAuth(); - return ( -
+
{children}
diff --git a/src/app/notes/page.tsx b/src/app/notes/page.tsx index 401ed85..531662c 100644 --- a/src/app/notes/page.tsx +++ b/src/app/notes/page.tsx @@ -1,8 +1,7 @@ -import { getAuth } from "@/app/actions/auth"; +import { Metadata } from "next"; +import { requireAuth } from "@/app/actions/auth"; import { getNotes } from "@/app/actions/notes"; import NoteCard from "@/components/ui/NoteCard"; -import { Metadata } from "next"; -import { redirect } from "next/navigation"; export const metadata: Metadata = { title: "Notes - Rhyme", @@ -10,16 +9,17 @@ export const metadata: Metadata = { }; export default async function Notes() { - const user = await getAuth(); - if (!user) { - redirect("/auth"); - } - const notes = await getNotes(user); + const user = await requireAuth(); + const notes = await getNotes(); return ( <>

Notes of {user.username}:

- {notes.map((note) => )} + {notes.length === 0 ? ( + You have no notes yet + ) : ( + notes.map((note) => ) + )} ); } diff --git a/src/components/Header.tsx b/src/components/Header.tsx index e0a20cd..ec87581 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,13 +1,12 @@ -"use client"; - import Link from "next/link"; import { CircleQuestionMark, List, Plus, UserRound, UserRoundMinus } from "lucide-react"; -import { logOut } from "@/app/actions/auth"; +import { getAuth, logOut } from "@/app/actions/auth"; import { createNote } from "@/app/actions/notes"; -import { IUser } from "@/lib/db/schema"; import HeaderButton from "./ui/HeaderButton"; -export default function Header({ user }: { user: IUser | null }) { +export default async function Header() { + const user = await getAuth(); + return (
@@ -16,10 +15,12 @@ export default function Header({ user }: { user: IUser | null }) { {user && (
- } onClick={() => createNote(user)} /> - - } /> - +
+ } /> + + + } /> +
)} @@ -28,7 +29,9 @@ export default function Header({ user }: { user: IUser | null }) { } /> {user ? ( - } /> +
+ } /> + ) : ( } /> diff --git a/src/components/ui/NoteCard.tsx b/src/components/ui/NoteCard.tsx index ec11649..0a0fa47 100644 --- a/src/components/ui/NoteCard.tsx +++ b/src/components/ui/NoteCard.tsx @@ -14,11 +14,6 @@ function makeTimestamp(date: Date) { } export default function NoteCard({ note }: { note: INote }) { - const deleteNoteAction = async () => { - "use server"; - await deleteNote(note); - }; - return (
@@ -26,7 +21,10 @@ export default function NoteCard({ note }: { note: INote }) { Last time edited: {makeTimestamp(note.lastEdited)} Creation date: {makeTimestamp(note.creationTime)} - } onClick={deleteNoteAction} /> +
+ + } type="submit" /> +
); }