Skip to Content

TypeScript

Learn about TypeScript configuration for Entro Auth, including strict mode, inferring types for sessions and users, defining additional fields, and inferring additional fields on the client.

Entro Auth is designed to be type-safe. Both the client and server are built with TypeScript, allowing you to easily infer types.

TypeScript Config

Strict Mode

Entro Auth is designed to work with TypeScript's strict mode. We recommend enabling strict mode in your TypeScript config file:

tsconfig.json
{
  "compilerOptions": {
    "strict": true
  }
}

if you can't set strict to true, you can enable strictNullChecks:

tsconfig.json
{
  "compilerOptions": {
    "strictNullChecks": true,
  }
}

When strict is true, strictNullChecks is enabled as well. If you explicitly set strictNullChecks to false, type inference issues could occur.

If you're running into issues with TypeScript inference exceeding maximum length the compiler will serialize, then please make sure you're following the instructions above, as well as ensuring that both declaration and composite are not enabled.

Exporting the instance from a package

A package that exports its auth instance and is built with declaration: true, as a shared package of a monorepo often is, needs only the Entro Auth packages it already depends on. The compiler writes the type of the instance into the package's declaration file, and every name in that type is one those packages export. If the build stops with TS2883 (The inferred type of 'auth' cannot be named without a reference to …) or TS4023, a type of one of our own dependencies has reached the instance under a name we do not export. That is a defect on our side. Until it is fixed, adding the package the message names (zod, for example) to your own dependencies gets the build through.

Inferring Types

Both the client SDK and the server offer types that can be inferred using the $Infer property. Plugins can extend base types like User and Session, and you can use $Infer to infer these types. Additionally, plugins can provide extra types that can also be inferred through $Infer.

auth-client.ts
import { createAuthClient } from "@entro314labs/entro-auth/client"

const authClient = createAuthClient()

export type Session = typeof authClient.$Infer.Session

The Session type includes both session and user properties. The user property represents the user object type, and the session property represents the session object type.

You can also infer types on the server side.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"
import Database from "better-sqlite3"

export const auth = entroAuth({
    database: new Database("database.db")
})

type Session = typeof auth.$Infer.Session

Additional Fields

Entro Auth allows you to add additional fields to the user and session objects. All additional fields are properly inferred and available on the server and client side.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"
import Database from "better-sqlite3"

export const auth = entroAuth({
    database: new Database("database.db"),
    user: {
       additionalFields: {
          role: {
              type: "string",
              input: false
            } 
        }
    }
   
})

type Session = typeof auth.$Infer.Session

In the example above, we added a role field to the user object. This field is now available on the Session type.

The input property

The input property in an additional field configuration determines whether the field should be included in the user input. This property defaults to true, meaning the field will be part of the user input during operations like registration.

To prevent a field from being part of the user input, you must explicitly set input: false:

additionalFields: {
    role: {
        type: "string",
        input: false
    }
}

When input is set to false, the field will be excluded from user input, preventing users from passing a value for it.

By default, additional fields are included in the user input, which can lead to security vulnerabilities if not handled carefully. For fields that should not be set by the user, like a role, it is crucial to set input: false in the configuration.

Inferring Additional Fields on Client

To make sure proper type inference for additional fields on the client side, you need to inform the client about these fields. There are two approaches to achieve this, depending on your project structure:

  1. For Monorepo or Single-Project Setups

If your server and client code reside in the same project, you can use the inferAdditionalFields plugin to automatically infer the additional fields from your server configuration.

auth-client.ts
import { inferAdditionalFields } from "@entro314labs/entro-auth/client/plugins";
import { createAuthClient } from "@entro314labs/entro-auth/client";
import type { auth } from "@/lib/auth"; // Import the auth instance as a type

export const authClient = createAuthClient({
  plugins: [inferAdditionalFields<typeof auth>()],
});
  1. For Separate Client-Server Projects

If your client and server are in separate projects, you'll need to manually specify the additional fields when creating the auth client.

auth-client.ts
import { createAuthClient } from "@entro314labs/entro-auth/client";
import { inferAdditionalFields } from "@entro314labs/entro-auth/client/plugins";

export const authClient = createAuthClient({
  plugins: [inferAdditionalFields({
      user: {
        role: {
          type: "string"
        }
      }
  })],
});

On this page