database wf 11

This commit is contained in:
alma 2025-04-17 13:30:46 +02:00
parent ec5108520b
commit 68b01bbfb6
2 changed files with 28 additions and 16 deletions

View File

@ -60,30 +60,36 @@ export const authOptions: NextAuthOptions = {
}, },
cookies: { cookies: {
sessionToken: { sessionToken: {
name: `__Secure-next-auth.session-token`, name: process.env.NODE_ENV === 'production'
? `__Secure-next-auth.session-token`
: `next-auth.session-token`,
options: { options: {
httpOnly: true, httpOnly: true,
sameSite: 'lax', sameSite: 'lax',
path: '/', path: '/',
secure: true secure: process.env.NODE_ENV === 'production'
} }
}, },
callbackUrl: { callbackUrl: {
name: `__Secure-next-auth.callback-url`, name: process.env.NODE_ENV === 'production'
? `__Secure-next-auth.callback-url`
: `next-auth.callback-url`,
options: { options: {
httpOnly: true, httpOnly: true,
sameSite: 'lax', sameSite: 'lax',
path: '/', path: '/',
secure: true secure: process.env.NODE_ENV === 'production'
} }
}, },
csrfToken: { csrfToken: {
name: `__Host-next-auth.csrf-token`, name: process.env.NODE_ENV === 'production'
? `__Host-next-auth.csrf-token`
: `next-auth.csrf-token`,
options: { options: {
httpOnly: true, httpOnly: true,
sameSite: 'lax', sameSite: 'lax',
path: '/', path: '/',
secure: true secure: process.env.NODE_ENV === 'production'
} }
} }
}, },

View File

@ -3,8 +3,14 @@ import { NextResponse } from "next/server";
export default withAuth( export default withAuth(
function middleware(req) { function middleware(req) {
// Allow access to the root path and signin page // Allow access to public paths
if (req.nextUrl.pathname === "/" || req.nextUrl.pathname === "/signin") { if (
req.nextUrl.pathname === "/" ||
req.nextUrl.pathname === "/signin" ||
req.nextUrl.pathname.startsWith("/_next") ||
req.nextUrl.pathname.startsWith("/api/auth") ||
req.nextUrl.pathname.startsWith("/public")
) {
return NextResponse.next(); return NextResponse.next();
} }
@ -14,17 +20,17 @@ export default withAuth(
{ {
callbacks: { callbacks: {
authorized: ({ token, req }) => { authorized: ({ token, req }) => {
// Allow access to the root path and signin page // Allow access to public paths
if (req.nextUrl.pathname === "/" || req.nextUrl.pathname === "/signin") { if (
req.nextUrl.pathname === "/" ||
req.nextUrl.pathname === "/signin" ||
req.nextUrl.pathname.startsWith("/_next") ||
req.nextUrl.pathname.startsWith("/api/auth") ||
req.nextUrl.pathname.startsWith("/public")
) {
return true; return true;
} }
// Check if the request is for an API route
if (req.nextUrl.pathname.startsWith('/api/')) {
// For API routes, require a valid token
return !!token;
}
// For all other routes, require a valid token // For all other routes, require a valid token
return !!token; return !!token;
}, },