Skip to content

Current User (api/current.ts)

Endpoint Overview

Retrieves the currently authenticated user's information. Requires authentication and returns the user object with their details and permissions.

Request Handlers

GET

  • No parameters required
  • Response:
    {
      currentUser: {
        id: string;
        email: string;
        name: string;
        role: string;
        // other user properties
      }
    }
    
  • Status: 200 OK, 401 Unauthorized, 405 Method Not Allowed, 500 Server Error

Error Handling

try {
  const { currentUser } = await serverAuth(req, res);
  return res.status(200).json(currentUser);
} catch (error) {
  console.log(error);
  return res.status(500).end();
}

Common errors: - 401: No valid session - 405: Non-GET request method - 500: Server authentication failure

Usage Example

const response = await fetch("/api/current", {
  method: "GET",
  headers: { 
    "Authorization": `Bearer ${session.token}`
  }
});
const currentUser = await response.json();

Implementation Details

  • Uses NextAuth for authentication
  • Validates session token
  • Returns user data from database
  • Handles session expiry
  • Includes user permissions and roles

Pages/Components Referenced By

Notes

  • Rate limited to 100 requests/minute
  • Cached for 1 minute
  • Requires valid authentication token
  • Used for initial app load
  • Critical for authorization checks