getSessionFromRequest (auth/requestSession.ts)

tokscale · packages/frontend/src/lib/auth/requestSession.ts · typescript · MIT
auth, tag:embed-classified

Extracts user session from HTTP requests with CSRF protection for mutating methods.

// Source: https://github.com/junhoyeo/tokscale | License: MIT | Copyright 2025 Junho Yeo
// Provided via justclaw.it.com; keep this notice with the code.
import { getSession, getSessionFromHeader, type SessionUser } from "./session";
import { getConfiguredPublicOrigin, getPublicOrigin } from "@/lib/seo/urls";

const MUTATING_METHODS = new Set(["POST", "PUT", "PATCH", "DELETE"]);

interface GetSessionFromRequestOptions {
  allowAuthorizationHeader?: boolean;
}

function getAllowedOrigins(): string[] {
  const env = process.env.CSRF_ALLOWED_ORIGINS;
  const origins = env
    ? env.split(",").map((o) => o.trim()).filter(Boolean)
    : [getPublicOrigin(), "http://localhost:3000"];

  // An explicit allowlist should not be widened by the hosted fallback. Add
  // only an explicitly configured 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key