From f0d14a2311f0f8761e7d9302dd451cb48b1105e7 Mon Sep 17 00:00:00 2001 From: Ravi Rajput Date: Fri, 3 Nov 2023 20:38:01 +0530 Subject: [PATCH] Added dashboard route and keep protected when session is not active --- apps/web/app/api/auth/[...nextauth]/route.ts | 2 +- apps/web/app/components/NavMenu.tsx | 2 +- apps/web/app/dashboard/page.tsx | 6 ++++++ apps/web/app/page.tsx | 14 +++++++++++--- apps/web/middleware.ts | 3 +++ 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 apps/web/middleware.ts diff --git a/apps/web/app/api/auth/[...nextauth]/route.ts b/apps/web/app/api/auth/[...nextauth]/route.ts index edeb8c85..6614e8ec 100644 --- a/apps/web/app/api/auth/[...nextauth]/route.ts +++ b/apps/web/app/api/auth/[...nextauth]/route.ts @@ -48,7 +48,7 @@ export const authOptions: NextAuthOptions = { secret: process.env.NEXTAUTH_SECRET, callbacks: { async redirect({ url, baseUrl }) { - return url; + return baseUrl; }, }, }; diff --git a/apps/web/app/components/NavMenu.tsx b/apps/web/app/components/NavMenu.tsx index 8378b7ed..a24adb7a 100644 --- a/apps/web/app/components/NavMenu.tsx +++ b/apps/web/app/components/NavMenu.tsx @@ -16,7 +16,7 @@ function AuthButton() { } return (
- Guest
+ Guest
); diff --git a/apps/web/app/dashboard/page.tsx b/apps/web/app/dashboard/page.tsx index 49ddc2ad..277c04ff 100644 --- a/apps/web/app/dashboard/page.tsx +++ b/apps/web/app/dashboard/page.tsx @@ -1,4 +1,5 @@ "use client"; + import { useSession } from "next-auth/react"; import Link from "next/link"; @@ -6,9 +7,14 @@ export default function Dashboard() { const { data: session } = useSession(); return ( + <>
Home +

{session?.user && <>Welcome {session?.user?.email}} +

+ + ); } diff --git a/apps/web/app/page.tsx b/apps/web/app/page.tsx index 955abf6a..7eac90f2 100644 --- a/apps/web/app/page.tsx +++ b/apps/web/app/page.tsx @@ -1,12 +1,20 @@ -import Image from 'next/image' +"use client" + import styles from './page.module.css' import Link from 'next/link' +import { useSession } from 'next-auth/react' export default function Home() { + const {data: session} = useSession(); + + const Dashboard = session ? Dashboard : "" + return ( + <>
- Dashboard - Hello + {Dashboard}
+ + ) } diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts new file mode 100644 index 00000000..e702b128 --- /dev/null +++ b/apps/web/middleware.ts @@ -0,0 +1,3 @@ +export { default } from "next-auth/middleware"; + +export const config = { matcher: ["/dashboard"] };