From fd8628aba335444eb7a917f464a93c9e04dfdae7 Mon Sep 17 00:00:00 2001 From: DirkSchlossmacher <62424946+DirkSchlossmacher@users.noreply.github.com> Date: Mon, 16 Sep 2024 13:45:02 +0200 Subject: [PATCH] log req --- middleware.ts | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/middleware.ts b/middleware.ts index c2d611ff1cf..a96ca6eb06f 100644 --- a/middleware.ts +++ b/middleware.ts @@ -9,34 +9,32 @@ // gpt-o1 middleware.ts -import { withAuth } from "next-auth/middleware"; +import { NextResponse } from 'next/server'; +import { withAuth } from 'next-auth/middleware'; + +// Custom middleware to log requests +export function middleware(req) { + console.log(`Request Method: ${req.method}, Request URL: ${req.url}`); + + if (req.method === 'POST' || req.method === 'PUT') { + req.clone().json().then(body => { + console.log('Request Payload:', body); + }).catch(() => { + console.log('Failed to parse request payload'); + }); + } + + return NextResponse.next(); +} +// Authentication middleware export default withAuth({ callbacks: { authorized: ({ token }) => { - // Allow access if the token exists - console.log("token.email", token); + console.log("token.email", token?.email); return !!token; }, }, - async middleware(req, res, next) { - // Log the request method and URL - console.log(`Request Method: ${req.method}, Request URL: ${req.url}`); - - // Log the request payload if it's a POST or PUT request - if (req.method === 'POST' || req.method === 'PUT') { - let body = ''; - req.on('data', chunk => { - body += chunk.toString(); - }); - req.on('end', () => { - console.log('Request Payload:', body); - }); - } - - // Call the next middleware or route handler - next(); - } }); export const config = {