getSession

This section covers the getSession server function.

The getSession function is a key component in managing user sessions in your application. It is designed to retrieve the current session details for a user, ensuring that the session management is seamless and efficient.

Usage

import { getSession } from "auth";

async function handleRequest(req, res) {
  const session = await getSession(req);

  if (session) {
    // User is authenticated
    res
      .status(200)
      .json({ message: "Session retrieved successfully", session });
  } else {
    // User is not authenticated
    res.status(401).json({ message: "No active session" });
  }
}

Parameters

  • req: The request object, typically provided by the server framework (e.g., Next.js, Express).
  • res: The response object,