mysql2 adapter

Adapter for mysql2 provided by the MySQL adapter package.

import { mysql2 } from "@lucia-auth/adapter-mysql";
const mysql2: (
	pool: Pool,
	tableNames: {
		user: string;
		key: string;
		session: string | null;
	}
) => InitializeAdapter<Adapter>;
Parameters#

Table names are automatically escaped.

nametypedescription
poolPoolmysql2 connection pool
tableNames.userstringUser table name
tableNames.keystringKey table name
tableNames.sessionstring | nullSession table name - can be null when using alongside a session adapter

Installation#

npm i @lucia-auth/adapter-mysql
pnpm add @lucia-auth/adapter-mysql
yarn add @lucia-auth/adapter-mysql

Usage#

import { lucia } from "lucia";
import { mysql2 } from "@lucia-auth/adapter-mysql";
import mysql from "mysql2/promise";

const connectionPool = mysql.createPool({
	// ...
});

const auth = lucia({
	adapter: mysql2(connectionPool, {
		user: "auth_user",
		key: "user_key",
		session: "user_session"
	})
	// ...
});

MySQL schema#

You can choose any table names, just make sure to define them in the adapter argument. The id columns are not UUID types with the default configuration.

User table#

You can add additional columns to store user attributes.

CREATE TABLE auth_user (
    id VARCHAR(15) NOT NULL PRIMARY KEY
);

Key table#

Make sure to update the foreign key statement if you change the user table name.

CREATE TABLE user_key (
    id VARCHAR(255) NOT NULL PRIMARY KEY,
    user_id VARCHAR(15) NOT NULL,
    hashed_password VARCHAR(255),
    FOREIGN KEY (user_id) REFERENCES auth_user(id)
);

Session table#

You can add additional columns to store session attributes. Make sure to update the foreign key statement if you change the user table name.

CREATE TABLE user_session (
    id VARCHAR(127) NOT NULL PRIMARY KEY,
    user_id VARCHAR(15) NOT NULL,
    active_expires BIGINT UNSIGNED NOT NULL,
    idle_expires BIGINT UNSIGNED NOT NULL,
    FOREIGN KEY (user_id) REFERENCES auth_user(id)
);