Skip to Content

verifyUser

verifyUser is a server-side utility for Next.js App Router pages and route handlers. It verifies the current session and returns the authenticated user, or redirects to the login page if unauthenticated.


Import

import { verifyUser } from "@phpcreation/frontend-crud-react-nextjs-bundle/utils/verifyUser";

Usage

In a Page Component

// app/[locale]/invoices/page.tsx import { verifyUser } from "..."; export default async function InvoicesPage() { const user = await verifyUser(); // Redirects to /login automatically if unauthenticated return <InvoiceListing user={user} />; }

In a Route Handler

// app/api/invoices/route.ts import { verifyUser } from "..."; export async function GET() { const user = await verifyUser(); // Returns 401 if not authenticated }

Parameters

ParamTypeDescription
options.redirectTostringCustom redirect path (default: "/login")
options.requiredbooleanRedirect vs return null when unauthenticated (default: true)

Returns

The authenticated User object, or redirects/throws if unauthenticated.

Use verifyUser at the top of server components and API routes. For client-side permission checks, use AuthContext and useAuthContext.


Last updated on