Skip to content

Commit

Permalink
log req
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkSchlossmacher committed Sep 16, 2024
1 parent 5c7dc11 commit fd8628a
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit fd8628a

Please sign in to comment.