add console logging

This commit is contained in:
Joseph J Helfenbein
2025-01-25 02:33:09 -05:00
parent 7c6f5bdfb3
commit f0c6b70cf9

View File

@@ -14,7 +14,10 @@ async function connectDB() {
}
export async function POST(req) {
console.log('Received request:', req); // Log the full request
if (req.method !== 'POST') {
console.log('Method not allowed'); // Log if method is not POST
return NextResponse.json(
{ message: 'Method Not Allowed' },
{ status: 405 }
@@ -25,11 +28,14 @@ export async function POST(req) {
const webhookSignature = req.headers.get('clerk-signature');
const payload = JSON.stringify(await req.json());
console.log('Webhook Payload:', payload); // Log the webhook payload
const hmac = crypto.createHmac('sha256', CLERK_WEBHOOK_SECRET);
hmac.update(payload);
const computedSignature = hmac.digest('hex');
if (computedSignature !== webhookSignature) {
console.log('Invalid webhook signature'); // Log if signature is invalid
return NextResponse.json(
{ message: 'Invalid webhook signature' },
{ status: 400 }
@@ -41,9 +47,12 @@ export async function POST(req) {
const { first_name, last_name, email_addresses } = await req.json();
const email = email_addresses && email_addresses[0] ? email_addresses[0].email_address : null;
console.log('Clerk Data:', { first_name, last_name, email });
const name = first_name && last_name ? `${first_name} ${last_name}` : first_name || last_name;
if (!name || !email) {
console.log('Missing name or email');
return NextResponse.json(
{ message: 'Name and email are required' },
{ status: 400 }
@@ -52,6 +61,7 @@ export async function POST(req) {
let user = await User.findOne({ email });
if (user) {
console.log('User already exists');
return NextResponse.json(
{ message: 'User already exists' },
{ status: 400 }
@@ -67,13 +77,14 @@ export async function POST(req) {
});
await user.save();
console.log('User created successfully');
return NextResponse.json(
{ message: 'User successfully created' },
{ status: 200 }
);
} catch (error) {
console.error(error);
console.error('Error:', error);
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }