
π Complete Guide to NextAuth in Next.js (Simple Explanation)
When we build a full stack app, we need a way for users to login and stay logged in . This is called authentication . In this blog we will understand how to use NextAuth in Next.js in a very simple way π π What is NextAuth NextAuth is a library that helps us: β Login users β Manage sessions β Protect pages π Example User enters email and password App checks database If correct β user is logged in β π¦ Step 1 Install NextAuth Run this command: npm install next-auth@4 π Step 2 Add Secret Key Create or open .env file NEXTAUTH_SECRET=mySuperSecretKey123 π This is used to secure login sessions π§ Step 3 Add TypeScript Support Create file: next-auth.d.ts Add: declare module " next-auth " { interface Session { user : { id : string ; } & DefaultSession [ " user " ]; } } π Now we can use: session.user.id βοΈ Step 4 Create Auth Configuration Create file: lib/auth.ts import CredentialsProvider from " next-auth/providers/credentials " ; import { connectToDatabase } from " ./db " ; import User from "
Continue reading on Dev.to Tutorial
Opens in a new tab



