From f84169b630b9fc2d802e8a0423dd8820361950ef Mon Sep 17 00:00:00 2001 From: John Richmond <5629+jr@users.noreply.github.com> Date: Tue, 13 May 2025 14:24:18 -0700 Subject: [PATCH] Copy Matt's ping endpoint --- src/app/api/ping/route.ts | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/app/api/ping/route.ts diff --git a/src/app/api/ping/route.ts b/src/app/api/ping/route.ts new file mode 100644 index 0000000000..ac768c175e --- /dev/null +++ b/src/app/api/ping/route.ts @@ -0,0 +1,52 @@ +import { auth } from '@clerk/nextjs/server'; +import { NextResponse } from 'next/server'; + +import { logger } from '@/lib/server/logger'; + +/** + * API endpoint for testing Clerk authentication + * Verifies/parses JWT and logs authenticated user information + */ +export async function GET() { + const authObj = await auth(); + + // If not authenticated + if (!authObj.userId) { + return NextResponse.json( + { error: 'Unauthorized request' }, + { status: 401 }, + ); + } + + // Get the JWT token + const token = await authObj.getToken(); + + // Extract user information from the auth object + // Only include properties that exist on the auth object + const userInfo = { + userId: authObj.userId, + sessionId: authObj.sessionId, + orgId: authObj.orgId, + orgRole: authObj.orgRole, + // Note: To get additional user data like email, firstName, lastName, + // you would need to use Clerk's methods like clerkClient.users.getUser() + }; + + // Log the user information + logger.info({ + event: 'ping_endpoint_accessed', + userInfo: { + userId: authObj.userId, + sessionId: authObj.sessionId, + orgId: authObj.orgId, + orgRole: authObj.orgRole, + }, + hasToken: !!token, // Just log if token exists, not the actual token for security + }); + + // Return the user information + return NextResponse.json({ + authenticated: true, + userInfo, + }); +}