Guides
Migrating from Auth.js to Entro Auth
A step-by-step guide to transitioning from Auth.js to Entro Auth.
Introduction
In this guide, we'll walk through the steps to migrate a project from Auth.js (formerly NextAuth.js) to Entro Auth. Since these projects have different design philosophies, the migration requires careful planning and work. If your current setup is working well, there's no urgent need to migrate. We continue to handle security patches and critical issues for Auth.js.
However, if you're starting a new project or facing challenges with your current setup, we strongly recommend using Entro Auth. Our roadmap includes features previously exclusive to Auth.js, and we hope this will unite the ecosystem more strongly without causing fragmentation.
Create Entro Auth Instance
Before starting the migration process, set up Entro Auth in your project. Follow the installation guide to get started.
For example, if you use the GitHub OAuth provider, here is a comparison of the auth.ts file.
import NextAuth from "next-auth"
import GitHub from "next-auth/providers/github"
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [GitHub],
})Now Entro Auth supports stateless session management without any database. If you were using a Database adapter in Auth.js, you can refer to the Database models below to check the differences with Entro Auth's core schema.
Create Client Instance
This client instance includes a set of functions for interacting with the Entro Auth server instance. For more information, see the client documentation.
import { createAuthClient } from "@entro314labs/entro-auth/react"
export const authClient = createAuthClient()Update the Route Handler
Rename the /app/api/auth/[...nextauth] folder to /app/api/auth/[...all] to avoid confusion. Then, update the route.ts file as follows:
import { handlers } from "@/lib/auth"
export const { GET, POST } = handlersSession Management
In this section, we'll look at how to manage sessions in Entro Auth compared to Auth.js. For more information, see the session management documentation.
Client-side
Sign In
Here are the differences between Auth.js and Entro Auth for signing in users. For example, with the GitHub OAuth provider:
"use client"
import { signIn } from "next-auth/react"
signIn("github")Sign Out
Here are the differences between Auth.js and Entro Auth when signing out users.
"use client"
import { signOut } from "next-auth/react"
signOut()Get Session
Here are the differences between Auth.js and Entro Auth for getting the current active session.
"use client"
import { useSession } from "next-auth/react"
const { data, status, update } = useSession()Server-side
Sign In
Here are the differences between Auth.js and Entro Auth for signing in users. For example, with the GitHub OAuth provider:
import { signIn } from "@/lib/auth"
await signIn("github")Sign Out
Here are the differences between Auth.js and Entro Auth when signing out users.
import { signOut } from "@/lib/auth"
await signOut()Get Session
Here are the differences between Auth.js and Entro Auth for getting the current active session.
import { auth } from "@/lib/auth";
const session = await auth()Protecting Resources
Proxy(Middleware) is not intended for slow data fetching. While Proxy can be helpful for optimistic checks such as permission-based redirects, it should not be used as a full session management or authorization solution. - Next.js docs
Auth.js offers approaches using Proxy (Middleware), but we recommend handling auth checks on each page or route rather than in a Proxy or Layout. Here is a basic example of protecting resources with Entro Auth.
"use client";
import { authClient } from "@/lib/auth-client";
import { redirect } from "next/navigation";
const DashboardPage = () => {
const { data, error, isPending } = authClient.useSession();
if (isPending) {
return <div>Pending</div>;
}
if (!data || error) {
redirect("/sign-in");
}
return (
<div>
<h1>Welcome {data.user.name}</h1>
</div>
);
};
export default DashboardPage;Database models
Both Auth.js and Entro Auth provide stateless (JWT) and database session strategies. If you were using the database session strategy in Auth.js and plan to continue using it in Entro Auth, you will also need to migrate your database.
Just like Auth.js has database models, Entro Auth also has a core schema. In this section, we'll compare the two and explore the differences between them.
User -> User
TableUser5 fields
idPKstringUnique identifier for each user
nameOptionalstringUser's chosen display name
emailOptionalstringUser's email address for communication and login
emailVerifiedOptionalDateWhen the user's email was verified
imageOptionalstringUser's image url
Session -> Session
TableSession4 fields
idPKstringUnique identifier for each session
userIdFK → user.idstringThe ID of the user
sessionTokenstringThe unique session token
expiresDateThe time when the session expires
Account -> Account
TableAccount12 fields
idPKstringUnique identifier for each account
userIdFK → user.idstringThe ID of the user
typestringType of the account (e.g. 'oauth', 'email', 'credentials')
providerstringProvider identifier
providerAccountIdstringAccount ID from the provider
refresh_tokenOptionalstringThe refresh token of the account. Returned by the provider
access_tokenOptionalstringThe access token of the account. Returned by the provider
expires_atOptionalnumberThe time when the access token expires
token_typeOptionalstringType of the token
scopeOptionalstringThe scope of the account. Returned by the provider
id_tokenOptionalstringThe ID token returned from the provider
session_stateOptionalstringOAuth session state
VerificationToken -> Verification
TableVerificationToken3 fields
identifierPKstringIdentifier for the verification request
tokenPKstringThe verification token
expiresDateThe time when the verification token expires
Comparison
Table: User
name,email, andemailVerifiedare required in Entro Auth, while optional in Auth.jsemailVerifieduses a boolean in Entro Auth, while Auth.js uses a timestamp- Entro Auth includes
createdAtandupdatedAttimestamps
Table: Session
- Entro Auth uses
tokeninstead ofsessionToken - Entro Auth uses
expiresAtinstead ofexpires - Entro Auth includes
ipAddressanduserAgentfields - Entro Auth includes
createdAtandupdatedAttimestamps
Table: Account
- Entro Auth uses camelCase naming (e.g.
refreshTokenvsrefresh_token) - Entro Auth uses
idfor the local account record and theproviderIdplusaccountIdpair for the external identity - Entro Auth uses
providerIdinstead ofprovider - Entro Auth includes
accessTokenExpiresAtandrefreshTokenExpiresAtfor token management - Entro Auth includes
passwordfield to support built-in credential authentication - Entro Auth does not have a
typefield as it's determined by theproviderId - Entro Auth removes
token_typeandsession_statefields - Entro Auth includes
createdAtandupdatedAttimestamps
Critical for Credentials Migration:
In Auth.js, passwords are often stored directly on the User model. In Entro Auth, passwords must be stored in the Account table.
If you are migrating users with passwords (including for the Phone Number plugin), you must create a record in the account table for each user with providerId: "credential" and accountId set to the Entro Auth user id. Without this record, password-based sign-ins will fail.
Table: VerificationToken -> Verification
- Entro Auth uses
Verificationtable instead ofVerificationToken - Entro Auth uses a single
idprimary key instead of composite primary key - Entro Auth uses
valueinstead oftokento support various verification types - Entro Auth uses
expiresAtinstead ofexpires - Entro Auth includes
createdAtandupdatedAttimestamps
If you were using Auth.js v4, note that v5 does not introduce any breaking changes to the database schema. Optional fields like oauth_token_secret and oauth_token can be removed if you are not using them. Rarely used fields like refresh_token_expires_in can also be removed.
Customization
You may have extended the database models or implemented additional logic in Auth.js. Entro Auth allows you to customize the core schema in a type-safe way. You can also define custom logic during the lifecycle of database operations. For more details, see Concepts - Database.
Wrapping Up
Now you're ready to migrate from Auth.js to Entro Auth. For a complete implementation with multiple authentication methods, check out Better Auth's Next.js demo app, which this fork's API follows under its own package name. Entro Auth offers greater flexibility and more features, so be sure to explore the documentation to unlock its full potential.
If you need help with migration, open an issue or write to labs@entro314.com.