diff --git a/src/app/sign-in/page.tsx b/src/app/sign-in/page.tsx index 5929c67..8a4a5e4 100644 --- a/src/app/sign-in/page.tsx +++ b/src/app/sign-in/page.tsx @@ -1,18 +1,15 @@ import { OAuthSignInButton } from "@/components/auth/signin-button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" -import { auth } from "@/lib/auth/auth" +import { getSession } from "@/lib/auth/session" import { LogIn } from "lucide-react" -import { headers } from "next/headers" -import { redirect } from "next/navigation" export default async function SignInPage() { - const session = await auth.api.getSession({ - headers: await headers() - }) + const { session, redirect } = await getSession() if (session) { - redirect("/") + redirect("/dashboard") } + return (
diff --git a/src/lib/auth/session.ts b/src/lib/auth/session.ts new file mode 100644 index 0000000..bf6be32 --- /dev/null +++ b/src/lib/auth/session.ts @@ -0,0 +1,14 @@ +import { headers } from "next/headers" +import { auth } from "./auth" +import { redirect } from "next/navigation" + +export async function getSession() { + const session = await auth.api.getSession({ + headers: await headers() + }) + const redirectFunc = (path: string) => { + redirect(path) + } + + return { session, redirect: redirectFunc } +} \ No newline at end of file diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..fd4b54e --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,20 @@ +import { NextRequest, NextResponse } from "next/server" +import { auth } from "./lib/auth/auth" + +export async function middleware(request: NextRequest) { + const { nextUrl, headers, url } = request + + if (nextUrl.pathname.startsWith('/dashboard')) { + const session = await auth.api.getSession({ + headers: headers + }) + + if (!session) { + return NextResponse.redirect(new URL('/sign-in', url)) + } + } + + return NextResponse.next() +} + +export const config = { matcher: ['/dashboard/:path*'] }