Getting started in Fastify
Install Lucia using your package manager of your choice.
npm i lucia
pnpm add lucia
yarn add lucia
ESM#
Lucia can only be used in ESM projects. Configure your package.json
and tsconfig.json
accordingly.
// package.json
{
"type": "module"
// ...
}
// tsconfig.json
{
"compilerOptions": {
"module": "ESNext", // "ES2022" etc
"moduleResolution": "NodeNext" // "Node", "Node16"
// ...
}
// ...
}
Initialize Lucia#
Import lucia()
from lucia
and initialize it. Export auth
and its type as Auth
. Make sure to pass the fastify()
middleware We also need to provide an adapter
but since it’ll be specific to the database you’re using, we’ll cover that later.
// lucia.ts
import { lucia } from "lucia";
import { fastify } from "lucia/middleware";
// expect error (see next section)
export const auth = lucia({
env: "DEV", // "PROD" if deployed to HTTPS
middleware: fastify()
});
export type Auth = typeof auth;
Setup your database#
Lucia uses adapters to connect to your database. We provide official adapters for a wide range of database options, but you can always create your own. The schema and usage are described in each adapter’s documentation. The example below is for the Prisma adapter.
import { lucia } from "lucia";
import { fastify } from "lucia/middleware";
import { prisma } from "@lucia-auth/adapter-prisma";
import { PrismaClient } from "@prisma/client";
const client = new PrismaClient();
const auth = lucia({
env: "DEV", // "PROD" if deployed to HTTPS
middleware: fastify(),
adapter: prisma(client)
});
Adapters for database drivers and ORMs#
better-sqlite3
: SQLite- libSQL: libSQL (Turso)
- Mongoose: MongoDB
mysql2
: MySQLpg
: PostgreSQL (including@neondatabase/serverless
,@vercel/postgres
)postgres
: PostgreSQL- Prisma: MongoDB, MySQL, PostgreSQL, SQLite
- Redis: Redis
- Unstorage: Azure, Cloudflare KV, Memory, MongoDB, Planetscale, Redis, Vercel KV
Provider specific adapters#
Using query builders#
Set up types#
Create a .d.ts
file in your project root and declare a Lucia
namespace. The import path for Auth
is where you initialized lucia()
.
// app.d.ts
/// <reference types="lucia" />
declare namespace Lucia {
type Auth = import("./lucia.js").Auth;
type DatabaseUserAttributes = {};
type DatabaseSessionAttributes = {};
}
Polyfill#
If you’re using Node.js version 18 or below, you need to polyfill the Web Crypto API.
import { lucia } from "lucia";
import "lucia/polyfill/node";
export const auth = lucia({
// ...
});
Optionally, instead of doing a side-effect import, add the --experimental-global-webcrypto
flag when running fastify
.
{
"scripts": {
"start": "NODE_OPTIONS=--experimental-global-webcrypto fastify start -l info app.js",
"dev": "NODE_OPTIONS=--experimental-global-webcrypto npm run build && fastify start -w -l info -P app.js"
// ...
}
// ...
}
Next steps#
You can learn all the concepts and general APIs of Lucia by reading the Basics section in the docs. If you prefer writing code immediately, check out the Starter guides page or the examples repository.
Remember to check out the Guidebook for tutorials and guides! If you have any questions, join our Discord server!