⚡ BolkAuth

Server Actions can call requireAuth() to ensure the mutation runs for an authenticated user.

Create Server Helpers

// lib/auth-server.ts
import { createServerHelpers } from "@bolkauth/nextjs";
import { auth } from "@/lib/auth";
 
export const { getSession, getUser, requireAuth } = createServerHelpers(auth);

Protected Action

// app/settings/actions.ts
"use server";
 
import { requireAuth } from "@/lib/auth-server";
import { auth } from "@/lib/auth";
import { revalidatePath } from "next/cache";
 
export async function updateDisplayName(formData: FormData) {
  const user = await requireAuth();
  const name = String(formData.get("name") ?? "").trim();
 
  if (name.length < 2) {
    return { error: "Name must be at least 2 characters." };
  }
 
  await auth.config.adapter.updateUser(user.id, { name });
  revalidatePath("/settings");
 
  return { success: true };
}

Form Usage

import { updateDisplayName } from "./actions";
 
export function ProfileForm({ name }: { name?: string }) {
  return (
    <form action={updateDisplayName}>
      <input name="name" defaultValue={name ?? ""} />
      <button>Save</button>
    </form>
  );
}

Guidelines

PatternWhy
Use requireAuth() inside every protected actionMiddleware can protect routes, but actions should still enforce auth.
Validate form input before writingServer Actions receive untrusted user input.
Mutate through the adapter or your app database layerKeep business writes server-side.
Call revalidatePath() after writesRefresh stale Server Component output.

On this page