Menu

1 / 2

# Stop Breaking Your Next.js App: NextAuth Setup in App Router (with MongoDB)

DEV Community: database·Yash Matrani·about 21 hours ago
#KJ7Avgtd
#devtodatabase#devto#dev#auth#client
Reading 0:00
15s threshold

Setting up authentication in Next.js 14 (App Router) is a nightmare if you don't understand the boundary between Server Components and Client Components.

I spent hours debugging useSession returning null or throwing hydration errors. Here is the exact, fool-proof way to set up NextAuth (Auth.js) with a MongoDB database.

1. Installation

bash npm install next-auth @auth/mongodb-adapter mongodb 2. Environment Variables Create a .env.local file and add your GitHub OAuth credentials and MongoDB URI:

env GITHUB_ID=your_github_client_id GITHUB_SECRET=your_github_client_secret MONGODB_URI=your_mongodb_connection_string NEXTAUTH_SECRET=any_long_random_string NEXTAUTH_URL=http://localhost:3000 3.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.