Integrating Google Authentication with Next.js, Auth.js, and Prisma: What I Learned Along the Way
If you've ever tried integrating authentication into a Next.js project, you probably know that the actual Google login button is not the difficult part.
The interesting part starts when you need to understand where the user data goes, how sessions are maintained, how your database fits into the authentication flow, and whether all the libraries you're using actually work together.
I recently started integrating authentication into my Next.js project using Auth.js, Google OAuth, Prisma, and MongoDB Atlas.
I thought it would be a straightforward integration. Read the documentation, install the packages, configure Google OAuth, connect Prisma, and that's it.
Well... not quite. π
I ran into a few questions that made me stop and understand the architecture instead of blindly copying code from documentation.
Here's what I learned.
1. Understanding the authentication flow first
Before writing code, I wanted to understand what actually happens when a user clicks the "Sign in with Google" button.
The basic flow looks something like this:
- The user clicks the Google sign-in button.
- Auth.js redirects the user to Google's authentication page.
- The user grants permission.
- Google redirects the user back to our application.
- Auth.js processes the OAuth response.
- The application establishes a session.
- The user can now access authenticated parts of the application.
One thing I initially overlooked was that authentication and session management are related, but they're not the same thing.
Authentication answers:
Who is this user?
Session management answers:
How does the application remember that this user is authenticated across subsequent requests?
Understanding this distinction made the rest of the integration much easier to reason about.
2. Why I chose Auth.js
For this project, I decided to use Auth.js (previously known as NextAuth.js).
It supports multiple authentication methods, including:
- Google OAuth
- GitHub OAuth
- Facebook OAuth
- Email-based authentication
- Credentials
- WebAuthn / Passkeys
I started with Google OAuth because I wanted to get one authentication provider working properly before adding more.
I also explored the newer Auth.js v5 configuration style.
A basic configuration looks like this:
// auth.ts
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [Google],
session: {
strategy: "jwt",
},
});
There are a few important things here:
-
handlershandles authentication-related API requests. -
authlets us access the current session on the server. -
signInandsignOutprovide server-side authentication helpers. -
session.strategydetermines how sessions are managed.
In the App Router, the authentication route can be configured like this:
// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
This was one of the first things I wanted to understand properly: how Auth.js fits into the Next.js App Router instead of treating it like a separate backend service.
3. The interesting part: Prisma 8 and MongoDB
This is where things became confusing.
I was using MongoDB Atlas and wanted Prisma as my ORM.
While exploring the documentation, I came across the newer Prisma ORM 8 workflow.
Prisma 8 introduces some significant changes compared with the traditional Prisma Client workflow.
Some of the important differences include:
- Contract-based data models.
- A different runtime and query API.
- A revised migration workflow.
- Database-specific packages.
For MongoDB, Prisma 8 uses the @prisma/orm-mongo package.
A simplified example of the new runtime setup looks like this:
import mongo from "@prisma/orm-mongo/runtime";
import type { Contract } from "./contract.d";
import contractJson from "./contract.json" with {
type: "json",
};
export const db = mongo<Contract>({
contractJson,
url: process.env.MONGODB_URL!,
dbName: "your_database",
});
This is quite different from the familiar Prisma Client setup:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
If you've worked with earlier Prisma versions, you'll immediately notice that the approach is different.
And that difference matters when integrating other libraries.
4. The documentation mismatch that confused me
While exploring Auth.js, I found its official Prisma Adapter documentation.
The integration example follows the traditional Prisma Client approach:
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/lib/prisma";
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [],
});
Now compare that with the newer Prisma 8 MongoDB workflow.
The Auth.js adapter example expects a Prisma Client-compatible instance, while the Prisma 8 MongoDB setup uses a different runtime and contract-based API.
That raised several questions for me:
- Can the official Auth.js Prisma Adapter work with the new Prisma 8 MongoDB runtime?
- Does it support the new query API?
- Should I use JWT sessions instead?
- If I use JWT sessions, how should I persist user information in MongoDB?
- Am I losing any functionality by not using the official adapter?
This was an important lesson for me:
Just because two libraries support the same database doesn't automatically mean their integration APIs are compatible.
I didn't want to force an integration without understanding what was happening underneath.
I also learned not to assume that a traditional Prisma Adapter example can simply be copied into a Prisma 8 project.
5. JWT sessions vs database sessions
Another important concept I explored was the difference between JWT sessions and database sessions.
Here's the basic distinction:
| JWT Session | Database Session |
|---|---|
| Session information is stored in an encrypted token cookie by Auth.js | Session record is stored in the database |
| Doesn't require a database session record | Requires database session persistence |
| Useful when implementing a custom persistence flow | Useful when using a compatible database adapter |
| Requires careful handling of token claims and expiry | Requires session lookup and lifecycle management |
For a simple Google OAuth implementation, JWT sessions can be a reasonable approach.
However, there's an important distinction:
JWT sessions do not automatically save users or OAuth accounts to MongoDB.
If your application needs persistent user records, you still need to implement that database logic.
Also, JWT sessions are not a complete replacement for an adapter. Features such as account linking, provider account persistence, and database-backed sessions need separate consideration.
This was something I wanted to understand before choosing an implementation.
6. Client-side and server-side signIn are different
This was another small but important detail.
In a Next.js project using Auth.js v5, you may encounter two different ways to import signIn.
For a Client Component:
"use client";
import { signIn, useSession } from "next-auth/react";
For server-side authentication:
import { signIn } from "@/auth";
They serve different purposes.
Client-side signIn
Useful when triggering authentication from a client-side button:
<button onClick={() => signIn("google")}>
Sign in with Google
</button>
Server-side signIn
Useful in Server Actions or server-side flows.
I initially wondered whether I should import signIn from my auth.ts file everywhere.
But the answer is no.
If you're using useSession() in a Client Component, you should use the client-side exports from next-auth/react.
One more thing: useSession() requires a SessionProvider somewhere above the component in your React component tree.
For example:
"use client";
import { SessionProvider } from "next-auth/react";
export default function Providers({
children,
}: {
children: React.ReactNode;
}) {
return (
<SessionProvider>
{children}
</SessionProvider>
);
}
You can then wrap your application with this provider in the root layout.
7. A few things I learned beyond writing the code
While working through this integration, I realized that authentication is not just about making a login button work.
There are several decisions worth understanding before implementing it.
1. Don't confuse OAuth with session management.
Google verifies the user's identity through OAuth. Your application still needs a strategy for maintaining the authenticated session.
2. Don't assume JWT means no database.
You may not need database-backed sessions, but your application can still need database records for users, roles, profiles, and other application data.
3. Understand the library versions you're using.
Auth.js v4 and v5 have different configuration patterns. Prisma's newer major versions also introduce changes. Mixing examples from different versions can create unnecessary problems.
4. Protect routes on the server.
A client-side redirect is useful for user experience, but it is not a security mechanism.
For protected server-rendered pages, use server-side session checks. Authorization checks must also happen on the server.
5. Don't blindly copy database integration examples.
Check what API a library expects and compare it with the API exposed by your ORM version.
A working example for one version is not automatically a working example for another.
8. My current takeaway
I'm still working through the integration, but the biggest improvement so far has been in my understanding of the architecture.
Instead of asking:
"Which code should I copy to make Google login work?"
I'm now asking:
- Where is the user identity established?
- Where is the session stored?
- How does the application retrieve the session?
- When and how should a user record be created?
- How should OAuth accounts be associated with users?
- Which parts are handled by Auth.js, and which parts belong to my application?
These questions help me make better implementation decisions instead of just making the code compile.
And honestly, that's one of the things I enjoy about working on personal projects: they expose gaps in your understanding that you might not notice when you're only working on familiar features.
Conclusion
Integrating authentication with Next.js, Auth.js, Prisma, and MongoDB has been a good learning experience so far.
I haven't treated this as a copy-paste tutorial because the goal was to understand the integration, the trade-offs, and the decisions behind it.
I'll continue documenting the implementation as I work through user persistence, session handling, protected routes, and authorization.
If you've integrated Auth.js with the newer Prisma MongoDB runtime, I'd genuinely like to hear how you approached the adapter and user-persistence part.
Have you used the traditional Prisma Adapter, or did you implement a custom persistence flow?
Let's learn from each other's implementation experiences.
References
- Auth.js β Getting Started
- Auth.js β Prisma Adapter
- Prisma ORM β MongoDB Quickstart
- Prisma ORM β Supported Databases
Note: Prisma ORM 8 and its MongoDB support are still evolving. Check the current documentation and package compatibility before using these examples in a production application.
Top comments (0)