Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 1 min read

NextAuth for Beginners: Add Sign-In to Your Next.js App Without Building Auth

Authentication is one of those features every app needs and nobody wants to build from scratch: passwords, sessions, OAuth, security. NextAuth (now also called Auth.js) gives you sign-in for Next.js apps with a small amo

Authentication is one of those features every app needs and nobody wants to build from scratch: passwords, sessions, OAuth, security. NextAuth (now also called Auth.js) gives you sign-in for Next.js apps with a small amount of configuration instead of a custom auth system.
You start by defining your providers. A typical setup with Google and GitHub looks like this:

import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import GitHub from "next-auth/providers/github";

export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [Google, GitHub],
});

Then you add a sign-in button:

import { signIn } from "@/auth";

export function SignInButton() {
  return <button onClick={() => signIn("google")}>Sign in with Google</button>;
}

NextAuth handles the OAuth flow, creates a session, and gives you a session object with the user's name, email, and picture. To protect a page, you check the session on the server and redirect to sign-in if it is missing.
NextAuth is for you if you are building a Next.js app and you want users to sign in with Google, GitHub, email, or another provider without storing passwords yourself. It supports many providers, works with databases through adapters, and has good TypeScript support.
But know its limits. Auth has real security consequences, so you still need to understand sessions, cookies, and environment variables β€” the library does the work, but you configure it. Custom flows like magic links or credentials need extra setup, and the App Router and Pages Router setups differ, so follow the docs for your router.
Start today: create a fresh Next.js app, add NextAuth with one provider, and build a page that shows "Hello, name" when signed in and a sign-in button when not. Once that works, protect one route.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.