Added middleware and session helper
This commit is contained in:
@@ -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 (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-slate-50 to-slate-100 dark:from-slate-900 dark:to-slate-800 p-4">
|
||||
<Card className="w-full max-w-md shadow-xl">
|
||||
|
||||
14
src/lib/auth/session.ts
Normal file
14
src/lib/auth/session.ts
Normal file
@@ -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 }
|
||||
}
|
||||
20
src/middleware.ts
Normal file
20
src/middleware.ts
Normal file
@@ -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*'] }
|
||||
Reference in New Issue
Block a user