route.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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, email, mustChangePassword } } 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. email: session.email ?? null,
  34. mustChangePassword: session.mustChangePassword === true,
  35. },
  36. },
  37. 200,
  38. );
  39. },
  40. { logPrefix: "[api/auth/me]" },
  41. );