authentication-best-practices
Read sessions client- and server-side, guard routes, and run sign in/up/out with Better Auth. Use when gating pages/APIs on auth or wiring auth flows.
How do I install this agent skill?
npx skills add https://github.com/andrelandgraf/fullstackrecipes --skill authentication-best-practicesIs this agent skill safe to install?
- Gen Agent Trust Hubpass
The skill provides standard coding practices and examples for implementing authentication using the Better Auth library in Next.js applications.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
What does this agent skill do?
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
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/andrelandgraf/fullstackrecipes/authentication-best-practices">View authentication-best-practices on skillZs</a>