Integrations
Nitro Integration
Integrate Entro Auth with Nitro.
Entro Auth can be integrated with your Nitro Application (an open source framework to build web servers).
This guide aims to help you integrate Entro Auth with your Nitro application in a few simple steps.
Create a new Nitro Application
Start by scaffolding a new Nitro application using the following command:
npx create-nitro-appThis will create the nitro-app directory and install all the dependencies. You can now open the nitro-app directory in your code editor.
Database Setup
This guide uses SQLite through better-sqlite3 with Entro Auth's built-in Kysely adapter. SQLite in a local file is not suited to production: use a production database such as PostgreSQL there. See Adapters.
Install the SQLite driver:
npm install better-sqlite3Install & Configure Entro Auth
Follow steps 1 & 2 from the installation guide to install Entro Auth in your Nitro application & set up the environment variables.
Once that is done, create your Entro Auth instance within the server/utils/auth.ts file.
import { entroAuth } from "@entro314labs/entro-auth";
import Database from "better-sqlite3";
export default entroAuth({
database: new Database("database.sqlite"),
emailAndPassword: { enabled: true },
});Create the Database Tables
Use the Entro Auth CLI to create the tables Entro Auth needs:
npx @entro314labs/entro-auth-cli@latest migrate --config server/utils/auth.tsThe --config flag is used to specify the path to the file where you have created your Entro Auth instance.
Mount The Handler
You can now mount the Entro Auth handler in your Nitro application. You can do this by adding the following code to your server/api/auth/[...all].ts file:
import auth from "~/server/utils/auth";
export default auth;This is a catch-all route that will handle all requests to /api/auth/*.
CORS
You can configure CORS for your Nitro app using a route rule in your nitro.config.ts:
import { defineConfig } from "nitro";
export default defineConfig({
routeRules: {
"/api/auth/**": {
cors: {
origin: ["http://localhost:3000"],
credentials: true,
},
},
},
});If you use cookie-based sessions, set credentials: true and list your frontend's exact origin(s) in origin. A wildcard origin (cors: true) cannot be combined with credentials. If you only use bearer tokens, credentials isn't required. Ensure that the config is in sync with your frontend application.
Learn more about CORS on the Nitro documentation.
Auth Guard/Middleware
You can add an auth guard to your Nitro application to protect routes that require authentication. You can do this by creating a new file server/utils/require-auth.ts and adding the following code:
import { defineHandler, HTTPError } from "nitro";
import auth from "~/server/utils/auth.ts";
/**
* Middleware used to require authentication for a route.
*
* Can be extended to check for specific roles or permissions.
*/
export default defineHandler(async (event) => {
const session = await auth.api.getSession({
headers: event.req.headers,
});
if (!session) {
throw HTTPError.status(401, "Unauthorized");
}
// You can save the session to the event context for later use
event.context.auth = session;
});
You can now use the Object Syntax Event Handler to apply middleware to specific routes:
import { defineHandler } from "nitro";
import requireAuth from "~/server/utils/require-auth.ts";
export default defineHandler({
middleware: [requireAuth],
handler: () =>
Response.json(
{ message: "Secret data" },
{ status: 201, statusText: "Secret data" },
),
});