A minimal, production-friendly setup to use NextAuth.js (Auth.js) for Google sign‑in on a Next.js client and save/check the user on a separate Node.js (Express) server using MongoDB. This guide is intentionally step‑by‑step and only covers Continue with Google.
-
Client (Next.js 13/14, App Router)
Continue with Googlebutton using NextAuth- NextAuth route that, on successful sign‑in, POSTs user info to the server via Axios
-
Server (Node.js + Express + MongoDB)
GoogleUsermodel- Route + Controller to create user if not exists else login success (no update)
- Node.js 18+
- MongoDB URI (Atlas or local)
- Google Cloud Project with OAuth credentials
-
Go to Google Cloud Console → APIs & Services → Credentials
-
Create OAuth 2.0 Client ID (type: Web application)
-
Add Authorized redirect URI:
http://localhost:3000/api/auth/callback/google -
Copy Client ID and Client Secret — you’ll need them on the client.
cd client
npm i next-auth axiosGOOGLE_CLIENT_ID=your-google-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your-google-client-secret
NEXTAUTH_SECRET=any-long-random-string
NEXTAUTH_URL=http://localhost:3000
# Your server’s base URL (Express API)
NEXT_PUBLIC_API_URL=http://localhost:5000cd server
npm i express mongoose cors dotenv
npm i -D nodemonPORT=5000
MONGO_URI=mongodb+srv://<user>:<pass>@cluster0.xxxxx.mongodb.net/yourdb
CLIENT_ORIGIN=http://localhost:3000-
User clicks Login → Continue with Google on client
-
NextAuth completes Google OAuth →
callbacks.signInruns -
Client POSTs
{ name, email, image }toPOST /api/users/google -
Server checks MongoDB:
- No user → create user → respond
201 New user created - User exists → respond
200 Login success
- No user → create user → respond
-
signIncallback returnstrue→ NextAuth finalizes the session
# 1) Clone
git clone <this-repo-url>
cd repo-root
# 2) Install deps
cd server && npm i && cd ..
cd client && npm i && cd ..
# 3) Add environment files
# - client/.env.local (Google keys, NEXTAUTH, API URL)
# - server/.env (PORT, MONGO_URI, CLIENT_ORIGIN)
# 4) Run servers (two terminals)
# Terminal A (server):
cd server
npm run dev # e.g. nodemon index.js
# Terminal B (client):
cd client
npm run dev # Next.js on http://localhost:3000-
error=AccessDeniedafter Google- Your
signIncallback returnedfalse. Check server URL / CORS / status code.
- Your
-
OAUTH_CALLBACK_ERROR outgoing request timed out after 3500ms- Confirm
.env.local:NEXTAUTH_URL=http://localhost:3000 - Confirm Google Console Authorized redirect URI matches exactly
- Disable VPN / retry dev servers
- Confirm
-
CORS errors
- Ensure
CLIENT_ORIGIN=http://localhost:3000and Expresscors()uses it
- Ensure
-
Mongo duplicate key (email unique)
- Clear duplicates in DB or drop index if created wrongly: ensure unique
email
- Clear duplicates in DB or drop index if created wrongly: ensure unique
- Add role fields or lastLogin timestamps
- Add secure session tokens or JWT callbacks to enrich
session.user - Protect server routes by verifying NextAuth session/JWT (e.g., pass
Authorizationheader with a signed token)
- This setup uses NextAuth on the client only for OAuth; persistence is handled by your Node server for full control.
- Collection name is pinned to
GoogleUserto avoid Mongoose pluralization.
Happy building! 🚀