lucia/utils

generateLuciaPasswordHash()#

Creates a new Lucia hash of a password. If you’re looking for a general hashing function, DO NOT use this API. The output hash is designed in a way to be compatible with older versions of Lucia. The sole purpose of this API is for interacting with Lucia keys stored in your database, and nothing else.

const generateLuciaPasswordHash: (password: string) => Promise<string>;
Parameters#
nametypedescription
passwordnumberPassword to hash
Returns#
typedescription
stringHash

generateRandomString()#

Generates a cryptographically random string. If argument for parameter alphabet is not provided, the result with consist of a-z0-9 (lowercase letters, numbers).

const generateRandomString: (length: number, alphabet?: string) => string;
Parameters#
nametypeoptionaldescription
lengthnumberLength string to generate
alphabetstringString of characters to generate the string from `
Returns#
typedescription
stringRandomly generated string

isWithinExpiration()#

Checks with the current time is within the expiration time (in milliseconds UNIX time) provided.

const isWithinExpiration: (expiration: number) => boolean;
Parameters#
nametypedescription
expirationnumberExpiration time in milliseconds (UNIX time)
Returns#
valuedescription
trueIs within expiration
falseIs expired

joinAdapters()#

This is an experimental API and can change or be removed.

Joins multiple adapters into a single adapter, allowing you to override specific methods.

import { __experimental_joinAdapters } from "lucia/utils";
const joinAdapters: (
	baseAdapter: InitializeAdapter<Adapter | SessionAdapter | UserAdapter>,
	...adapters: Array<
		Partial<Adapter> | InitializeAdapter<Adapter | SessionAdapter | UserAdapter>
	>
) => Adapter;
Parameters#
nametype
baseAdapterInitializeAdapter
adaptersarray
Returns#
type
Adapter
Usage#
import { lucia } from "lucia";
import { __experimental_joinAdapters as joinAdapters } from "lucia/utils";
import { betterSqlite3 } from "@lucia-auth/adapter-sqlite";

export const auth = lucia({
	adapter: joinAdapters(betterSqlite3(), {
		getUser: async (userId) => {
			// ...
		}
	})
});

parseCookie()#

ESM and TypeScript friendly cookie.parse() from cookie.

serializeCookie()#

ESM and TypeScript friendly cookie.serialize() from cookie.

validateLuciaPasswordHash()#

Validates a password hash generated by Lucia.

const validateLuciaPasswordHash: (
	password: string,
	passwordHash: string
) => Promise<boolean>;
Parameters#
nametypedescription
passwordstringPassword to compare against
passwordHashstringHashed password to validate
Returns#
valuedescription
trueIs valid
falseIs invalid