Careeroza
Auth.js Setup
What Auth.js Gives You
Auth.js (the new name of NextAuth, version 5) is the most used auth library for Next.js. It handles sign-in pages, OAuth providers (Google, GitHub), sessions, CSRF protection and secure cookies. Alternatives: Clerk, Better Auth, Supabase Auth, or your own JWT backend (see the NestJS material).
Example
npm install next-auth@beta
npx auth secretExample output
added 4 packages in 3s 📝 Created /home/ravi/shopease/.env.local with `AUTH_SECRET`.
The Config File
auth.ts at the project root exports everything you need:
import NextAuth from 'next-auth';
import Google from 'next-auth/providers/google';
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [Google], // reads AUTH_GOOGLE_ID and AUTH_GOOGLE_SECRET from env
pages: { signIn: '/login' },
});app/api/auth/[...nextauth]/route.ts — one catch-all route handles all auth endpoints:
import { handlers } from '@/auth';
export const { GET, POST } = handlers;Auth Endpoints
| URL | Purpose |
|---|---|
/api/auth/signin | Default sign-in page |
/api/auth/callback/google | Google redirects here after login |
/api/auth/session | Current session as JSON |
/api/auth/signout | Sign out |
Example
curl -s localhost:3000/api/auth/session -b "authjs.session-token=eyJhbGciOi..."Example output
{"user":{"name":"Priya Sharma","email":"[email protected]","image":"https://lh3.googleusercontent.com/a/xyz"},"expires":"2026-11-18T10:00:00.000Z"}