DatabaseAdapter interface defines the contract that all database adapters must implement to work with Arraf Auth. It provides a consistent API for managing users, sessions, accounts, and verifications across different database systems.
Overview
The adapter pattern allows Arraf Auth to work with any database system by implementing a standardized interface. This decouples the authentication logic from the specific database implementation, making it easy to switch databases or use custom storage solutions.Interface Definition
User Methods
createUser
Creates a new user in the database.User data to create. The adapter will generate
id, createdAt, and updatedAt automatically.The created user object with generated
id, createdAt, and updatedAt fields.findUserById
Finds a user by their unique ID.The user’s unique identifier.
The user object if found, or
null if no user exists with that ID.findUserByEmail
Finds a user by their email address.The email address to search for.
The user object if found, or
null if no user exists with that email.findUserByPhone
Finds a user by their phone number.The phone number to search for.
The user object if found, or
null if no user exists with that phone number.updateUser
Updates an existing user’s information.The ID of the user to update.
Partial user data to update. Only provided fields will be updated.
The updated user object.
deleteUser
Deletes a user from the database.The ID of the user to delete.
This method returns nothing on success.
Deleting a user should cascade to delete their sessions and accounts. Implement this with database constraints or manual cleanup.
Session Methods
createSession
Creates a new session for a user.Session data to create. The adapter will generate
id and createdAt automatically.The created session object.
findSession
Finds a session by its token.The session token to search for.
The session object if found, or
null if no session exists with that token.updateSession
Updates an existing session.The token of the session to update.
Partial session data to update.
The updated session object.
deleteSession
Deletes a session (used for logout).The token of the session to delete.
This method returns nothing on success.
deleteUserSessions
Deletes all sessions for a specific user.The ID of the user whose sessions should be deleted.
This method returns nothing on success.
Account Methods
createAccount
Creates a new account linking a user to an authentication provider.Account data to create.
The created account object.
findAccount
Finds an account by provider and account ID.The provider identifier.
The account ID from the provider.
The account object if found, or
null.findAccountsByUserId
Finds all accounts belonging to a user.The user’s ID.
Array of account objects.
Verification Methods
createVerification
Creates a new verification record for OTP or email verification.Verification data to create.
The created verification object.
findVerification
Finds a verification record by identifier and type.The email or phone number.
The verification type.
The verification object if found, or
null.updateVerification
Updates a verification record (typically to increment attempts).The verification ID.
Partial verification data to update.
The updated verification object.
deleteVerification
Deletes a verification record after successful verification.The verification ID to delete.
This method returns nothing on success.
deleteExpiredVerifications
Cleans up expired verification records.This method returns nothing on success.
This should be called periodically (e.g., via a cron job) to prevent database bloat.
Creating a Custom Adapter
To create a custom adapter for your database:Official Adapters
Arraf Auth provides official adapters for popular databases:- Prisma Adapter - For Prisma ORM
- Drizzle Adapter - For Drizzle ORM