Authentication
Next.js authentication Better Auth ya Auth.js ke saath, session cookies, Data Access Layer checks aur proxy ke optimistic redirect ka sahi tareeqa.
On this page
1. Authentication kya hai?
Next.js mein auth teen hisson mein samjhein:
| Hissa | Kaam |
|---|---|
| Authentication | Login: email password, Google, GitHub waghera |
| Session management | Login ke baad har request par user ko yaad rakhna, aksar httpOnly cookie se |
| Authorization | Har page, Server Action aur Route Handler mein permission check |
Khud se poora auth likhna mushkil aur risky hai. Is liye Better Auth ya Auth.js jaisi library use karein jo sessions, OAuth aur password hashing sambhal leti hai.
2. Kyun zaroori hai?
| Sirf proxy ya layout mein check | Data Access Layer ke saath |
|---|---|
| Proxy sirf cookie ki maujoodgi dekhta hai | DAL har dafa session verify karta hai |
| Server Action seedha call ho kar bypass | Har action mein verifySession |
| Layout navigation par dobara nahi chalta | Check data ke bilkul paas |
| Har jagah alag auth logic | Ek file, ek jagah se sab checks |
3. Kahan use hota hai?
| Jagah | Kya check karein |
|---|---|
proxy.ts | Optimistic redirect: cookie nahi to /login |
| Server Component page | verifySession() phir user ka data |
| Server Action | Session + ownership, mutation se pehle |
| Route Handler | Session na ho to 401, role galat to 403 |
| Client Component | Sirf UI dikhana ya chhupana, security nahi |
4. Kaise kaam karta hai?
5. Code example
Step 1: Better Auth setup
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import { db } from "@/lib/db";
export const auth = betterAuth({
database: prismaAdapter(db, { provider: "postgresql" }),
emailAndPassword: { enabled: true },
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
plugins: [nextCookies()],
});app/api/auth/[...all]/route.ts mein library ka handler lagayein:
import { toNextJsHandler } from "better-auth/next-js";
import { auth } from "@/lib/auth";
export const { GET, POST } = toNextJsHandler(auth);Step 2: Data Access Layer
import "server-only";
import { cache } from "react";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
export const getSession = cache(async () => {
return auth.api.getSession({ headers: await headers() });
});
export async function verifySession() {
const session = await getSession();
if (!session) {
redirect("/login");
}
return { userId: session.user.id, name: session.user.name };
}Step 3: Page aur Server Action mein use
import { verifySession } from "@/lib/dal";
import { db } from "@/lib/db";
export default async function DashboardPage() {
const { userId, name } = await verifySession();
const orders = await db.order.findMany({ where: { userId } });
return (
<main>
<h1>Khush aamdeed, {name}</h1>
<p>Aapke orders: {orders.length}</p>
</main>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
betterAuth({...}) | Auth config: email password aur GitHub login |
prismaAdapter(db, ...) | Users aur sessions database mein store |
nextCookies() | Server Actions mein login par cookie set karne ke liye plugin |
toNextJsHandler(auth) | /api/auth/... ke saare endpoints |
auth.api.getSession({ headers }) | Cookie se session validate |
cache(...) | Ek render pass mein session sirf ek dafa fetch |
verifySession() | Session nahi to redirect, warna sirf zaroori fields |
where: { userId } | User sirf apna data dekhe |
Auth.js (NextAuth v5) bhi isi pattern par chalta hai: auth.ts se auth() function milta hai jo DAL mein session deta hai. Library jo bhi ho, DAL wala pattern same rehta hai.
Interview tip
"Kya proxy.ts mein auth check kaafi hai?" Jawab: nahi. Proxy sirf cookie dekhta hai, validate nahi karta. Server Actions aur Route Handlers public endpoints hain, is liye unke andar bhi verifySession() zaroori hai.
6. Common mistakes
Galati: Sirf proxy.ts par auth ka bharosa karna
Why? Cookie ki maujoodgi validation nahi hai, aur actions ya API ko seedha call kar ke bypass ho sakta hai.
Behtar: Proxy ko sirf redirect ke liye rakho, har data access par DAL se session verify karo.
Galati: Layout mein auth check kar ke pages ko safe samajhna
Why? Layouts navigation par dobara render nahi hote aur nested segments alag se bhi chal sakte hain.
Behtar: Check page, Server Action aur data function ke qareeb karo.
Galati: Poora user object Client Component ko bhej dena
Why? Password hash ya private fields browser tak pohanch sakte hain.
Behtar: DAL se sirf woh fields return karo jo UI ko chahiye.
Galati: Server Action mein ownership check bhool jana
Why? Logged in user kisi aur ka record id bhej kar delete ya edit kar sakta hai.
Behtar: Query mein userId shamil karo taake user sirf apna data badal sake.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Cookies & Headers, Server Actions, Environment Variables |
| Saath mein | Proxy (formerly Middleware), Forms & Validation |
| Baad mein | Database Integration, Testing Next.js Apps |
Final quick revision
| Point | Detail |
|---|---|
| Library | Better Auth ya Auth.js |
| Session | httpOnly cookie, server par validate |
| Proxy | Sirf optimistic redirect |
| DAL | verifySession() har page, action aur handler mein |
| Data | Sirf zaroori fields return, userId se filter |