On this page

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],
})
Note

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.

auth-client.ts
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 } = handlers

Session 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.

app/dashboard/page.tsx
"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

FieldTypeDescription
  • id
    PK
    string

    Unique identifier for each user

  • name
    Optional
    string

    User's chosen display name

  • email
    Optional
    string

    User's email address for communication and login

  • emailVerified
    Optional
    Date

    When the user's email was verified

  • image
    Optional
    string

    User's image url

Session -> Session

TableSession4 fields

FieldTypeDescription
  • id
    PK
    string

    Unique identifier for each session

  • userId
    FK → user.id
    string

    The ID of the user

  • sessionToken
    string

    The unique session token

  • expires
    Date

    The time when the session expires

Account -> Account

TableAccount12 fields

FieldTypeDescription
  • id
    PK
    string

    Unique identifier for each account

  • userId
    FK → user.id
    string

    The ID of the user

  • type
    string

    Type of the account (e.g. 'oauth', 'email', 'credentials')

  • provider
    string

    Provider identifier

  • providerAccountId
    string

    Account ID from the provider

  • refresh_token
    Optional
    string

    The refresh token of the account. Returned by the provider

  • access_token
    Optional
    string

    The access token of the account. Returned by the provider

  • expires_at
    Optional
    number

    The time when the access token expires

  • token_type
    Optional
    string

    Type of the token

  • scope
    Optional
    string

    The scope of the account. Returned by the provider

  • id_token
    Optional
    string

    The ID token returned from the provider

  • session_state
    Optional
    string

    OAuth session state

VerificationToken -> Verification

TableVerificationToken3 fields

FieldTypeDescription
  • identifier
    PK
    string

    Identifier for the verification request

  • token
    PK
    string

    The verification token

  • expires
    Date

    The time when the verification token expires

Comparison

Table: User

  • name, email, and emailVerified are required in Entro Auth, while optional in Auth.js
  • emailVerified uses a boolean in Entro Auth, while Auth.js uses a timestamp
  • Entro Auth includes createdAt and updatedAt timestamps

Table: Session

  • Entro Auth uses token instead of sessionToken
  • Entro Auth uses expiresAt instead of expires
  • Entro Auth includes ipAddress and userAgent fields
  • Entro Auth includes createdAt and updatedAt timestamps

Table: Account

  • Entro Auth uses camelCase naming (e.g. refreshToken vs refresh_token)
  • Entro Auth uses id for the local account record and the providerId plus accountId pair for the external identity
  • Entro Auth uses providerId instead of provider
  • Entro Auth includes accessTokenExpiresAt and refreshTokenExpiresAt for token management
  • Entro Auth includes password field to support built-in credential authentication
  • Entro Auth does not have a type field as it's determined by the providerId
  • Entro Auth removes token_type and session_state fields
  • Entro Auth includes createdAt and updatedAt timestamps
Warning

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 Verification table instead of VerificationToken
  • Entro Auth uses a single id primary key instead of composite primary key
  • Entro Auth uses value instead of token to support various verification types
  • Entro Auth uses expiresAt instead of expires
  • Entro Auth includes createdAt and updatedAt timestamps
Note

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.