route.js 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // app/api/auth/me/route.js
  2. import { getSession } from "@/lib/auth/session";
  3. import { withErrorHandling, json } from "@/lib/api/errors";
  4. /**
  5. * Force dynamic execution (RHL-006):
  6. * - auth-related response must never be cached/shared accidentally.
  7. */
  8. export const dynamic = "force-dynamic";
  9. /**
  10. * GET /api/auth/me
  11. *
  12. * Purpose:
  13. * - Provide the current session identity for frontend consumers.
  14. *
  15. * Semantics (frontend-friendly):
  16. * - 200 with { user: null } when unauthenticated
  17. * - 200 with { user: { userId, role, branchId } } when authenticated
  18. *
  19. * This avoids using 401 as control-flow for basic "am I logged in?" checks.
  20. */
  21. export const GET = withErrorHandling(
  22. async function GET() {
  23. const session = await getSession();
  24. if (!session) {
  25. return json({ user: null }, 200);
  26. }
  27. return json(
  28. {
  29. user: {
  30. userId: session.userId,
  31. role: session.role,
  32. branchId: session.branchId ?? null,
  33. },
  34. },
  35. 200
  36. );
  37. },
  38. { logPrefix: "[api/auth/me]" }
  39. );