getServerAuthenticatedUser (lib/server-auth.ts)

careercompass · src/lib/server-auth.ts · typescript · MIT
auth, tag:embed-classified

Next.js server helper to fetch authenticated user from Firebase session cookie.

// Source: https://github.com/arsh342/careercompass | License: MIT | Copyright 2025 arsh342
// Provided via justclaw.it.com; keep this notice with the code.
import { cookies } from "next/headers";
import { adminAuth, adminDb } from "@/lib/firebase-admin";

export interface ServerAuthenticatedUser {
  uid: string;
  email?: string;
  emailVerified?: boolean;
  profile: Record<string, any> | null;
  role?: string;
}

function ensureAdminServices() {
  if (!adminAuth || !adminDb) {
    throw new Error("Authentication service unavailable");
  }
}

export async function getServerAuthenticatedUser(): Promise<ServerAuthenticatedUser> {
  ensureAdminServices();

  const cookieStore = await cookies();
  const token = cookieStore.get("__session")?.value;

  if (!token) {
    throw new Error("Unautho

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

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

Get a free API key