Skip to content
Dinvolt
Esc
↑↓navigate↵open⌘Jpreview
On this page

Sessions and authorization

For Admin panel only projects, the login is /, the home page is /dashboard, and other panel pages omit the /admin prefix. The proxy matches /dashboard/:path*, /users/:path* and /settings/:path*. Components use @/components and @/components/ui. API endpoints such as /api/admin/users/export keep their paths. Examples below use the combined public website + admin panel layout; generated standalone guides use the app’s actual paths.

Read and protect a session

Use the client API

Client Components import authClient from @/lib/auth-client. For example, this helper signs in and clears data from any previous identity before navigating:

// src/lib/sign-in.ts
"use client";

import { authClient } from "@/lib/auth-client";
import { resetQueryCache } from "@/trpc/query-client";

export async function signIn(email: string, password: string) {
  const { error } = await authClient.signIn.email({ email, password });

  if (error) {
    return { error: "Unable to sign in. Check your email and password." };
  }

  await resetQueryCache();
  window.location.assign("/admin/home");
}

The built-in forms already handle this flow. Other client APIs include session observation, sign-out, and email/password sign-up. Sign-up creates an ordinary user; a public registration screen is application-specific.

Validate on the server

Client session checks control presentation. Validate a session again wherever server code reads private data or performs a protected operation:

// src/lib/current-user.ts
import "server-only";
import { headers } from "next/headers";

import { getAuth } from "@/lib/auth";

export async function getCurrentUser() {
  const session = await getAuth().api.getSession({
    headers: await headers(),
  });

  if (!session || session.user.banned) {
    return null;
  }

  return { id: session.user.id, name: session.user.name };
}

Keep session and role checks request-scoped, outside shared Next.js caches. Cookie caching is disabled so database role changes and bans are checked.

Require an administrator

The generated @/lib/admin-auth module provides:

Helper Use it for
getAdminSession() A request-scoped, validated, non-banned session
isAdmin(session) Exact membership of the admin role
requireAdmin() Admin pages/actions that redirect unauthorized visitors to /admin

The generated src/proxy.ts redirects requests without a session cookie to /admin. Its matcher covers /admin/:path*, with sign-in, forgot-password, reset-password and the generated Open Graph image explicitly allowed through. Public website pages, /_next assets and API routes are outside the matcher. Extend the matcher when adding protected sections outside its existing paths.

AdminGate validates the session and admin role before rendering AdminShell or its children. Its Suspense fallback is null, so missing, invalid or unauthorized sessions never show the panel or its skeletons. React cache() deduplicates getAdminSession() within the current server render; verified identity is hydrated for client components without another browser session request.

Call requireAdmin() at every protected page, data, or action boundary, even inside the protected layout. After the gate authorizes access, keep static headings and controls outside subsequent data waits.

Custom admin APIs must validate both the session and admin role. tRPC endpoints can use adminProcedure; see the tRPC guide.

A session cookie’s presence never proves access. The no-cookie shortcut only avoids unnecessary auth/database initialization; keep it aligned if you customize cookie names.

Was this page helpful?