diff --git a/src/app/(editor)/layout.tsx b/src/app/(editor)/layout.tsx index c63ac70..66c8588 100644 --- a/src/app/(editor)/layout.tsx +++ b/src/app/(editor)/layout.tsx @@ -7,7 +7,7 @@ export default function RootLayout({ }>) { return ( <> -
+
{children} ); diff --git a/src/app/(editor)/page.tsx b/src/app/(editor)/page.tsx index 0159d1e..231d4f6 100644 --- a/src/app/(editor)/page.tsx +++ b/src/app/(editor)/page.tsx @@ -1,7 +1,15 @@ import Link from "next/link"; +import { getAuth } from "@/lib/auth"; import Editor from "@/components/editor/Editor"; +import { redirect } from "next/navigation"; + +export default async function Home() { + const auth = await getAuth(); + + if (auth) { + redirect("/notes"); + } -export default function Home() { return (
diff --git a/src/app/(noneditor)/about/page.tsx b/src/app/(noneditor)/about/page.tsx index 526e2fa..9294323 100644 --- a/src/app/(noneditor)/about/page.tsx +++ b/src/app/(noneditor)/about/page.tsx @@ -1,7 +1,7 @@ import { Metadata } from "next"; export const metadata: Metadata = { - title: "About rhyme", + title: "About Rhyme", description: "Information about the Rhyme and its creators", }; diff --git a/src/app/(noneditor)/auth/page.tsx b/src/app/(noneditor)/auth/page.tsx index 6a43a88..6d05536 100644 --- a/src/app/(noneditor)/auth/page.tsx +++ b/src/app/(noneditor)/auth/page.tsx @@ -2,7 +2,7 @@ import { Metadata } from "next"; import AuthForm from "@/components/forms/AuthForm"; export const metadata: Metadata = { - title: "Authenticate - rhyme", + title: "Authenticate - Rhyme", description: "Register or log into Rhyme account to save, show and load notes", }; diff --git a/src/app/(noneditor)/notes/page.tsx b/src/app/(noneditor)/notes/page.tsx new file mode 100644 index 0000000..a6e6b8b --- /dev/null +++ b/src/app/(noneditor)/notes/page.tsx @@ -0,0 +1,14 @@ +import { Metadata } from "next"; + +export const metadata: Metadata = { + title: "My notes - Rhyme", + description: "View, create and edit your notes", +}; + +export default function Notes() { + return ( +
+

Notes

+
+ ); +} diff --git a/src/app/actions.ts b/src/app/actions.ts index 58e9e2a..f4a1bc3 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -13,6 +13,12 @@ const JWT_SECRET = process.env.JWT_SECRET!; export async function authenticate(_prevState: unknown, formData: FormData) { const username = formData.get("username") as string; const password = formData.get("password") as string; + + if (username.length < 3) { + return { error: "Username is too short!" }; + } else if (password.length < 8) { + return { error: "Password is too short!" }; + } const users = await db.select() .from(usersTable) @@ -36,5 +42,11 @@ export async function authenticate(_prevState: unknown, formData: FormData) { maxAge: 60 * 60 * 24 * 7, }); - redirect("/"); + redirect("/notes"); +} + +export async function logOut() { + const cookieStore = await cookies(); + cookieStore.delete("session"); + redirect("/auth"); } diff --git a/src/components/Header.tsx b/src/components/Header.tsx index e01553b..1cb83bc 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,29 +1,39 @@ import Link from "next/link"; -import { ArrowRightFromLine, CircleQuestionMark, List, Plus, UserRound } from "lucide-react"; +import { ArrowRightFromLine, CircleQuestionMark, List, Plus, UserRound, UserRoundMinus } from "lucide-react"; +import { getAuth } from "@/lib/auth"; import HeaderButton from "./HeaderButton"; +import { logOut } from "@/app/actions"; + +export default async function Header({ showToolbar = false }: { showToolbar?: boolean }) { + const auth = await getAuth(); -export default function Header({ showToolbar = true }: { showToolbar?: boolean }) { return (
- + - {showToolbar && ( -
- } /> - } /> - } /> -
- )} +
+ {auth && ( + <> + } /> + {showToolbar && } />} + + )} + {showToolbar && } />} +
} /> - - } /> - + {auth ? ( + } /> + ) : ( + + } /> + + )}
); diff --git a/src/components/HeaderButton.tsx b/src/components/HeaderButton.tsx index 59c6d83..411cf2e 100644 --- a/src/components/HeaderButton.tsx +++ b/src/components/HeaderButton.tsx @@ -1,8 +1,13 @@ -import { ReactNode } from "react"; +import { ButtonHTMLAttributes, ReactNode } from "react"; -export default function HeaderButton({ title, icon, className }: { title: string, icon?: ReactNode, className?: string }) { +interface HeaderButtonProps extends ButtonHTMLAttributes { + title: string; + icon?: ReactNode; +} + +export default function HeaderButton({ title, icon, className, ...props }: HeaderButtonProps) { return ( - diff --git a/src/lib/auth.ts b/src/lib/auth.ts index b4eeecb..d27d71d 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,4 +1,5 @@ import { cookies } from "next/headers"; +import jwt from "jsonwebtoken"; export async function getAuth() { const cookieStore = await cookies(); @@ -7,5 +8,6 @@ export async function getAuth() { return null; } - // TODO + const decodedToken = jwt.decode(token) as jwt.JwtPayload; + return { username: decodedToken.sub }; }