Authentication with NextAuth.js
⏱ 20 min read read
Setup NextAuth.js v5 (Auth.js):
npm install next-auth@beta
# .env.local
AUTH_SECRET=your-random-secret-here # openssl rand -base64 32
AUTH_GITHUB_ID=your-github-oauth-id
AUTH_GITHUB_SECRET=your-github-secret
// auth.ts --- config file
import NextAuth from 'next-auth';
import GitHub from 'next-auth/providers/github';
import Credentials from 'next-auth/providers/credentials';
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
GitHub,
Credentials({
credentials: { email: {}, password: {} },
async authorize({ email, password }) {
const user = await verifyUser(email as string, password as string);
return user ?? null;
},
}),
],
});
// app/api/auth/[...nextauth]/route.ts
export { handlers as GET, handlers as POST } from '@/auth';
Protecting Routes with Middleware:
// middleware.ts (root of project)
export { auth as middleware } from '@/auth';
export const config = {
matcher: ['/dashboard/:path*', '/profile/:path*'],
};
Reading Session Data:
// Server Component
import { auth } from '@/auth';
const session = await auth();
if (!session) redirect('/login');
console.log(session.user?.name);
// Client Component
import { useSession } from 'next-auth/react';
const { data: session, status } = useSession();
if (status === 'loading') return <p>Loading...</p>;
if (status === 'unauthenticated') redirect('/login');
NextAuth.js handles OAuth (GitHub, Google, Discord), credentials,
email magic links.
The session is stored in a signed, encrypted JWT cookie by default.
Adapter packages connect NextAuth to Prisma, Drizzle, etc. for DB
sessions.
Install Prisma adapter: npm install @auth/prisma-adapter
npm install next-auth@beta
# .env.local
AUTH_SECRET=your-random-secret-here # openssl rand -base64 32
AUTH_GITHUB_ID=your-github-oauth-id
AUTH_GITHUB_SECRET=your-github-secret
// auth.ts --- config file
import NextAuth from 'next-auth';
import GitHub from 'next-auth/providers/github';
import Credentials from 'next-auth/providers/credentials';
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
GitHub,
Credentials({
credentials: { email: {}, password: {} },
async authorize({ email, password }) {
const user = await verifyUser(email as string, password as string);
return user ?? null;
},
}),
],
});
// app/api/auth/[...nextauth]/route.ts
export { handlers as GET, handlers as POST } from '@/auth';
Protecting Routes with Middleware:
// middleware.ts (root of project)
export { auth as middleware } from '@/auth';
export const config = {
matcher: ['/dashboard/:path*', '/profile/:path*'],
};
Reading Session Data:
// Server Component
import { auth } from '@/auth';
const session = await auth();
if (!session) redirect('/login');
console.log(session.user?.name);
// Client Component
import { useSession } from 'next-auth/react';
const { data: session, status } = useSession();
if (status === 'loading') return <p>Loading...</p>;
if (status === 'unauthenticated') redirect('/login');
NextAuth.js handles OAuth (GitHub, Google, Discord), credentials,
email magic links.
The session is stored in a signed, encrypted JWT cookie by default.
Adapter packages connect NextAuth to Prisma, Drizzle, etc. for DB
sessions.
Install Prisma adapter: npm install @auth/prisma-adapter
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress