From 0013832e066b9347c95156aa9316a906759f1795 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Fri, 7 Jun 2024 23:23:06 +0200 Subject: [PATCH] feat: better protected routes, move to login, env example file --- .env.example | 2 ++ src/app/(protected)/layout.tsx | 10 ++++++++++ src/app/{ => (protected)}/protected/page.tsx | 4 +--- src/app/{auth/signIn => (public)/auth/login}/page.tsx | 0 src/app/{ => (public)}/auth/signUp/page.tsx | 2 +- src/app/{ => (public)}/page.tsx | 0 src/components/app/NavBar/NavBar.tsx | 2 +- src/lib/auth/actions.ts | 2 +- 8 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .env.example create mode 100644 src/app/(protected)/layout.tsx rename src/app/{ => (protected)}/protected/page.tsx (71%) rename src/app/{auth/signIn => (public)/auth/login}/page.tsx (100%) rename src/app/{ => (public)}/auth/signUp/page.tsx (95%) rename src/app/{ => (public)}/page.tsx (100%) diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f79672a --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +# from the dev postgres containeer +DATABASE_URL=postgresql://postgres:dfsjhkdswkjntelsmldbfvsgknl5t@localhost:5555/postgres \ No newline at end of file diff --git a/src/app/(protected)/layout.tsx b/src/app/(protected)/layout.tsx new file mode 100644 index 0000000..ff22080 --- /dev/null +++ b/src/app/(protected)/layout.tsx @@ -0,0 +1,10 @@ +import { validateRequest } from "@/lib/auth"; +import { redirect } from "next/navigation"; + +export default async function Layout({ children }: { children: React.ReactNode }) { + const { user } = await validateRequest() + if (!user) { + return redirect('/auth/login') + } + return children +} \ No newline at end of file diff --git a/src/app/protected/page.tsx b/src/app/(protected)/protected/page.tsx similarity index 71% rename from src/app/protected/page.tsx rename to src/app/(protected)/protected/page.tsx index 9f777f2..575648b 100644 --- a/src/app/protected/page.tsx +++ b/src/app/(protected)/protected/page.tsx @@ -1,14 +1,12 @@ import { validateRequest } from "@/lib/auth"; -import { redirect } from "next/navigation"; export default async function Page() { const { user } = await validateRequest() - if (!user) return redirect('/auth/signIn') return (
You are actually on a protected route!
-Your ID is: {user.id}
+Your ID is: {user!.id}