Installation
Learn how to configure Entro Auth in your project.
Install the Package
Let's start by adding Entro Auth to your project:
npm install @entro314labs/entro-authIf you're using a separate client and server setup, make sure to install Entro Auth in both parts of your project.
On Node.js, Entro Auth requires version 24.7.0 or later. That is the first release whose Web Crypto API has ML-DSA, the post-quantum signature the JWT and passkey plugins can use.
Set Environment Variables
Create a .env file in the root of your project and add the following environment variables:
- Secret Key
A secret value used for encryption and hashing. It must be at least 32 characters and generated with high entropy. Click the button below to generate one. You can also use openssl rand -base64 32 to generate one.
ENTRO_AUTH_SECRET=Need to rotate your secret later? You can use ENTRO_AUTH_SECRETS (plural) to roll over to a new secret without invalidating existing data. See the secrets option for details.
- Set Base URL
ENTRO_AUTH_URL=http://localhost:3000 # Base URL of your appCreate A Entro Auth Instance
Create a file named auth.ts in one of these locations:
- Project root
lib/folderutils/folder
You can also nest any of these folders under src/, app/ or server/ folder. (e.g. src/lib/auth.ts, app/lib/auth.ts).
And in this file, import Entro Auth and create your auth instance. Make sure to export the auth instance with the variable name auth or as a default export.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
//...
});Configure Database
Entro Auth requires a database to store user data. You can easily configure Entro Auth to use SQLite, PostgreSQL, or MySQL, and more!
You can also configure Entro Auth to work in a stateless mode if you don't configure a database. See Stateless Session Management for more information. Note that most plugins will require a database.
import { entroAuth } from "@entro314labs/entro-auth";
import Database from "better-sqlite3";
export const auth = entroAuth({
database: new Database("./sqlite.db"),
})Alternatively, if you prefer to use an ORM, you can use one of the built-in adapters.
import { entroAuth } from "@entro314labs/entro-auth";
import { drizzleAdapter } from "@entro314labs/entro-auth/adapters/drizzle";
import { db } from "@/db"; // your drizzle instance
import * as schema from "@/db/auth-schema";
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
schema,
}),
});If your database is not listed above, check out our other supported databases for more information, or use one of the supported ORMs.
If you’re using a database adapter (Drizzle, CapyDB, or a community adapter),
consider importing entroAuth from @entro314labs/entro-auth/minimal instead of @entro314labs/entro-auth to reduce bundle size.
See Bundle Size Optimization for more information.
Create Database Tables
Entro Auth includes a CLI tool to help manage the schema required by the library.
- Generate: This command generates an ORM schema or SQL migration file.
If you're using Kysely, you can apply the migration directly with migrate command below. Use generate only if you plan to apply the migration manually.
npx @entro314labs/entro-auth-cli@latest generate-
Migrate: This command creates the required tables directly in the database. (Available only for the built-in Kysely adapter)
npx @entro314labs/entro-auth-cli@latest migrate
see the CLI documentation for more information.
If you instead want to create the schema manually, you can find the core schema required in the database section.
Authentication Methods
Configure the authentication methods you want to use. Entro Auth comes with built-in support for email/password, and social sign-on providers.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
//...other options
emailAndPassword: {
enabled: true,
},
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
});You can use even more authentication methods like passkey, username, magic link and more through plugins.
Mount Handler
To handle API requests, you need to set up a route handler on your server.
Create a new file or route in your framework's designated catch-all route handler. This route should handle requests for the path /api/auth/* (unless you've configured a different base path).
Entro Auth supports any backend framework with standard Request and Response objects and offers helper functions for popular frameworks.
import { auth } from "@/lib/auth"; // path to your auth file
import { toNextJsHandler } from "@entro314labs/entro-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);Create Client Instance
The client-side library helps you interact with the auth server. Entro Auth comes with a client for all the popular web frameworks, including vanilla JavaScript.
- Import
createAuthClientfrom the package for your framework (e.g., "@entro314labs/entro-auth/react" for React). - Call the function to create your client.
- Pass the base URL of your auth server. (If the auth server is running on the same domain as your client, you can skip this step.)
If you're using a different base path other than /api/auth make sure to pass
the whole URL including the path. (e.g.
http://localhost:3000/custom-path/auth)
import { createAuthClient } from "@entro314labs/entro-auth/react"
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: "http://localhost:3000"
})Tip: You can also export specific methods if you prefer:
export const { signIn, signUp, useSession } = createAuthClient()🎉 That's it!
That's it! You're now ready to use entro-auth in your application. Continue to basic usage to learn how to use the auth instance to sign in users.