|
| 1 | +import { auth } from '@clerk/nextjs/server'; |
| 2 | +import { NextResponse } from 'next/server'; |
| 3 | + |
| 4 | +import { logger } from '@/lib/server/logger'; |
| 5 | + |
| 6 | +/** |
| 7 | + * API endpoint for testing Clerk authentication |
| 8 | + * Verifies/parses JWT and logs authenticated user information |
| 9 | + */ |
| 10 | +export async function GET() { |
| 11 | + const authObj = await auth(); |
| 12 | + |
| 13 | + // If not authenticated |
| 14 | + if (!authObj.userId) { |
| 15 | + return NextResponse.json( |
| 16 | + { error: 'Unauthorized request' }, |
| 17 | + { status: 401 }, |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + // Get the JWT token |
| 22 | + const token = await authObj.getToken(); |
| 23 | + |
| 24 | + // Extract user information from the auth object |
| 25 | + // Only include properties that exist on the auth object |
| 26 | + const userInfo = { |
| 27 | + userId: authObj.userId, |
| 28 | + sessionId: authObj.sessionId, |
| 29 | + orgId: authObj.orgId, |
| 30 | + orgRole: authObj.orgRole, |
| 31 | + // Note: To get additional user data like email, firstName, lastName, |
| 32 | + // you would need to use Clerk's methods like clerkClient.users.getUser() |
| 33 | + }; |
| 34 | + |
| 35 | + // Log the user information |
| 36 | + logger.info({ |
| 37 | + event: 'ping_endpoint_accessed', |
| 38 | + userInfo: { |
| 39 | + userId: authObj.userId, |
| 40 | + sessionId: authObj.sessionId, |
| 41 | + orgId: authObj.orgId, |
| 42 | + orgRole: authObj.orgRole, |
| 43 | + }, |
| 44 | + hasToken: !!token, // Just log if token exists, not the actual token for security |
| 45 | + }); |
| 46 | + |
| 47 | + // Return the user information |
| 48 | + return NextResponse.json({ |
| 49 | + authenticated: true, |
| 50 | + userInfo, |
| 51 | + }); |
| 52 | +} |
0 commit comments