Next.js 15 continues to build on its reputation as a powerful full-stack framework, offering seamless integration with modern authentication solutions. Whether you're building a simple app or an enterprise-grade system, this guide will walk you through implementing authentication using Next.js 15 with popular tools like Auth.js (NextAuth.js), Clerk, and Supabase Auth.
Authentication ensures secure user access to your application. Next.js 15 supports multiple authentication patterns:
Auth.js (formerly NextAuth.js) is the most popular authentication library for Next.js.
npm install next-auth @auth/core
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
secret: process.env.AUTH_SECRET!,
});
Wrap your root layout with SessionProvider in app/layout.tsx:
import { SessionProvider } from "next-auth/react";
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<SessionProvider>{children}</SessionProvider>
</body>
</html>
);
}
Use middleware in middleware.ts:
export { auth as middleware } from "@/auth";
Example login button in a client component:
"use client";
import { useSession, signIn, signOut } from "next-auth/react";
export default function AuthButton() {
const { data: session } = useSession();
return (
<div>
{!session ? (
<button onClick={() => signIn("google")}>Sign in with Google</button>
) : (
<button onClick={() => signOut()}>Sign out</button>
)}
</div>
);
}
Use HTTPS in Production: Ensure all authentication flows use HTTPS.
Secure Session Storage: Use HTTP-only cookies for session tokens.
Role-Based Access Control: Implement middleware for protected routes:
// middleware.ts
import { auth } from "@/auth";
export default auth((req) => {
if (!req.auth) return Response.redirect(new URL("/login", req.url));
});
Next.js 15 simplifies authentication with flexible options:
Clerk for rapid development with pre-built components
Supabase for all-in-one backend services
Choose the method that aligns with your project’s needs and security requirements. For more details, refer to the Next.js Authentication Documentation.