Database adapters API

Errors#

Errors defined in the specification, such as AUTH_INVALID_USER_ID must be thrown as a LuciaError.

throw new LuciaError("AUTH_INVALID_USER_ID");

InitializeAdapter#

adapter configuration takes a InitializeAdapter function, which in turn returns the actual adapter instance. This function takes a LuciaError, and all errors thrown by the adapter must use this class instead of the one imported from lucia.

const customAdapter = (config: any) => {
	return (luciaError: typeof LuciaError) => ({
		// adapter
	});
};

lucia({
	adapter: customAdapter(options)
});

Adapter#

type Adapter = {
	getSessionAndUser?: (
		sessionId: string
	) => Promise<[SessionSchema, UserSchema] | [null, null]>;
} & UserAdapter &
	SessionAdapter;

getSessionAndUser()#

const getSessionAndUser: (
	sessionId: string
) => Promise<
	[session: SessionSchema, user: UserSchema] | [session: null, user: null]
>;
Parameters#
nametypedescription
sessionIdstringUnique target session(id)
Returns#
nametypedescription
sessionSessionSchema | nullTarget session
userUserSchema | nullUser of target session

UserAdapter#

type UserAdapter = Readonly<{
	getUser: (userId: string) => Promise<UserSchema | null>;
	setUser: (user: UserSchema, key: KeySchema | null) => Promise<void>;
	updateUser: (
		userId: string,
		partialUser: Partial<UserSchema>
	) => Promise<void>;
	deleteUser: (userId: string) => Promise<void>;

	getKey: (keyId: string) => Promise<KeySchema | null>;
	getKeysByUserId: (userId: string) => Promise<KeySchema[]>;
	setKey: (key: KeySchema) => Promise<void>;
	updateKey: (keyId: string, partialKey: Partial<KeySchema>) => Promise<void>;
	deleteKey: (keyId: string) => Promise<void>;
	deleteKeysByUserId: (userId: string) => Promise<void>;
}>;

deleteKey()#

const deleteKey: (keyId: string) => Promise<void>;
Parameters#
nametypedescription
keyIdstringUnique target key(id)

deleteKeysByUserId()#

const deleteKeysByUserId: (userId: string) => Promise<void>;
Parameters#
nametypedescription
userIdstringTarget key(user_id)

deleteUser()#

const deleteUser: (userId: string) => Promise<void>;
Parameters#
nametypedescription
userIdstringUnique target user(id)

getKey()#

const getKey: (keyId: string) => Promise<KeySchema | null>;
Parameters#
nametypedescription
keyIdstringUnique arget key(id)
Returns#
typedescription
KeySchemaTarget key

getKeysByUserId()#

const getKeysByUserId: (userId: string) => Promise<KeySchema[]>;
Parameters#
nametypedescription
userIdstringTarget key(user_id)
Returns#
typedescription
KeySchema[]Array of matched key

getUser()#

const getUser: (userId: string) => Promise<UserSchema | null>;
Parameters#
nametypedescription
userIdstringUnique target user(id)
Returns#
typedescription
UserSchemaTarget user

setKey()#

const setKey: (key: KeySchema) => Promise<void>;
Parameters#
nametypedescription
keyKeySchemakey to create

setUser()#

const setUser: (user: UserSchema, key: KeySchema | null) => Promise<void>;
Parameters#
nametypedescription
userUserSchemauser to create
keyKeySchema| nullkey to create, if defined

updateKey()#

const updateKey: (
	keyId: string,
	partialKey: Partial<KeySchema>
) => Promise<void>;
Parameters#
nametypedescription
keyIdstringUnique target key(id)
partialKeyPartial<KeySchema>key fields to update

updateUser()#

const updateUser: (
	userId: string,
	partialUser: Partial<UserSchema>
) => Promise<void>;
Parameters#
nametypedescription
userIdstringUnique target user(id)
partialUserPartial<UserSchema>user fields to update

SessionAdapter#

type SessionAdapter = Readonly<{
	getSession: (sessionId: string) => Promise<SessionSchema | null>;
	getSessionsByUserId: (userId: string) => Promise<SessionSchema[]>;
	setSession: (session: SessionSchema) => Promise<void>;
	updateSession: (
		sessionId: string,
		partialSession: Partial<SessionSchema>
	) => Promise<void>;
	deleteSession: (sessionId: string) => Promise<void>;
	deleteSessionsByUserId: (userId: string) => Promise<void>;
}>;

deleteSession()#

const deleteSession: (sessionId: string) => Promise<void>;
Parameters#
nametypedescription
sessionIdstringUnique target session(id)

deleteSessionsByUserId()#

const deleteSessionsByUserId: (userId: string) => Promise<void>;
Parameters#
nametypedescription
userIdstringTarget session(user_id)

getSession()#

const getSession: (sessionId: string) => Promise<SessionSchema | null>;
Parameters#
nametypedescription
sessionIdstringUnique target session(id)
Returns#
typedescription
SessionSchemaTarget session

getSessionsByUserId()#

const getSessionsByUserId: (userId: string) => Promise<SessionSchema[]>;
Parameters#
nametypedescription
userIdstringTarget session(user_id)
Returns#
typedescription
SessionSchema[]Array of matched session

setSession()#

const setSession: (session: SessionSchema) => Promise<void>;
Parameters#
nametypedescription
sessionSessionSchemasession to create

updateSession()#

const updateSession: (
	keyId: string,
	partialKey: Partial<SessionSchema>
) => Promise<void>;
Parameters#
nametypedescription
sessionIdstringUnique target session(id)
partialSessionPartial<SessionSchema>session fields to update