Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
andrelandgraf avatar

Authentication Best Practices

  • 53 installs
  • 18 repo stars
  • Updated June 8, 2026
  • andrelandgraf/fullstackrecipes

authentication-best-practices is a Claude Code skill that reads Better Auth sessions client- and server-side, guards routes, and runs sign in/up/out flows in a Next.js app.

About

This skill shows how to read Better Auth sessions on the client and server, guard routes by redirecting unauthenticated users, and run sign in, sign up, and sign out flows in a Next.js app. Developers use it when gating pages or API routes on authentication or wiring auth flows. It assumes Better Auth is already set up and imports from canonical paths like @/lib/auth/client and @/lib/auth/server.

  • Client and server session patterns with Better Auth
  • Route guarding and redirect patterns for protected pages
  • Sign in / up / out flows including social (Google) login

Authentication Best Practices by the numbers

  • 53 all-time installs (skills.sh)
  • Ranked #1,290 of 2,203 Security skills by installs in the Skillselion catalog
  • Data as of Jul 28, 2026 (Skillselion catalog sync)
At a glance

authentication-best-practices capabilities & compatibility

Capabilities
auth setup · session management · route guard
Use cases
api development · security audit
From the docs

What authentication-best-practices says it does

Read sessions, protect routes, and run sign in/up/out with Better Auth.
SKILL.md
In Server Components and API routes, call `auth.api.getSession` with the request `headers`.
SKILL.md
Redirect unauthenticated users away from protected pages, and authenticated users away from auth pages.
SKILL.md
npx skills add https://github.com/andrelandgraf/fullstackrecipes --skill authentication-best-practices

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs53
repo stars18
Last updatedJune 8, 2026
Repositoryandrelandgraf/fullstackrecipes

What it does

Gate Next.js pages and API routes on authentication and run sign in, sign up, and sign out flows with Better Auth.

Who is it for?

Developers gating Next.js pages or API routes on auth and wiring Better Auth sign-in flows.

Skip if: Setting up Better Auth from scratch; it assumes the Better Auth Setup recipe is already complete.

When should I use this skill?

Gating pages or APIs on auth or wiring auth flows.

What you get

Sessions are read correctly on client and server, protected routes redirect unauthenticated users, and auth flows work end to end.

  • Session reading code
  • Route guard patterns
  • Sign in/up/out flow code

Files

SKILL.mdMarkdownGitHub ↗

Authentication Best Practices

Read sessions, protect routes, and run sign in/up/out with Better Auth.

Prerequisites

Complete these setup recipes first:

  • Better Auth Setup

Client-Side Sessions

Use the auth client hooks from @/lib/auth/client in client components.

"use client";

import { useSession, signOut } from "@/lib/auth/client";

export function UserMenu() {
  const { data: session, isPending } = useSession();

  if (isPending) return <div>Loading...</div>;
  if (!session) return <a href="/sign-in">Sign In</a>;

  return (
    <div>
      <span>{session.user.name}</span>
      <button onClick={() => signOut()}>Sign Out</button>
    </div>
  );
}

Server-Side Sessions

In Server Components and API routes, call auth.api.getSession with the request headers.

import { auth } from "@/lib/auth/server";
import { headers } from "next/headers";

const session = await auth.api.getSession({ headers: await headers() });

if (!session) {
  return new Response("Unauthorized", { status: 401 });
}

// session.user.id for queries...

Guarding Routes

Redirect unauthenticated users away from protected pages, and authenticated users away from auth pages.

import { redirect } from "next/navigation";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";

export default async function ProtectedPage() {
  const session = await auth.api.getSession({ headers: await headers() });
  if (!session) redirect("/sign-in");
  return <Dashboard user={session.user} />;
}

export default async function SignInPage() {
  const session = await auth.api.getSession({ headers: await headers() });
  if (session) redirect("/chats");
  return <SignIn />;
}

After validating the session, fetch user-specific data in parallel.

const [chats, profile] = await Promise.all([
  getUserChats(session.user.id),
  getUserProfile(session.user.id),
]);

Sign In / Up / Out

import { signIn, signUp, signOut } from "@/lib/auth/client";

await signIn.email({ email, password, callbackURL: "/chats" });
await signIn.social({ provider: "google", callbackURL: "/chats" });

await signUp.email({ email, password, name, callbackURL: "/verify-email" });

await signOut({
  fetchOptions: { onSuccess: () => router.push("/") },
});

---

References

Related skills

FAQ

How do I read the session on the server?

Call auth.api.getSession with the request headers from next/headers in Server Components and API routes.

How do I protect a page?

Get the session server-side and redirect unauthenticated users to /sign-in; redirect authenticated users away from auth pages.

Securityappsec

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.