Tiesen Logo

Database Concepts

Understanding the database structure and table relationships for the authentication system

The authentication system uses a simplified three-table structure to manage users, authentication accounts, and sessions.

Tables Overview

User Table

Stores core user information and profile data.

FieldTypeKeyDescription
iduuidPKUnique identifier for each user (auto-generated)
namevarchar(255)User's display name
emailvarchar(255)UNIQUEUser's email address (required for authentication)
imagevarchar(255)URL to user's profile image
createdAttimestampTimestamp when user account was created (auto-set)
updatedAttimestampTimestamp when user data was last updated (auto-updated)

Account Table

Links users to their authentication providers (OAuth or credentials).

FieldTypeKeyDescription
providervarchar(255)PK (composite)Authentication provider name (google, github, discord, credentials)
accountIdvarchar(255)PK (composite)Unique account ID from the provider or email for credentials
userIduuidFKReferences User.id - the user this account belongs to
passwordvarchar(255)Hashed password (only for credentials provider, null for OAuth)

Primary Key: Composite key of (provider, accountId) - ensures unique accounts per provider

Foreign Key: userId references users.id with cascade delete

Session Table

Manages user sessions for authentication state.

FieldTypeKeyDescription
tokenvarchar(255)PKUnique session token identifying the session
expirestimestampTimestamp when session expires (with timezone)
userIduuidFKReferences User.id - the user this session belongs to

Foreign Key: userId references users.id with cascade delete

Relationships

  • User → Account: One-to-Many (a user can have multiple auth methods: OAuth + credentials)
  • User → Session: One-to-Many (a user can have multiple active sessions across devices)
  • Account → User: Many-to-One (each account belongs to one user)
  • Session → User: Many-to-One (each session belongs to one user)

Key Features

  • Cascade Deletes: When a user is deleted, all their accounts and sessions are automatically removed
  • Composite Primary Key: Accounts use (provider, accountId) to prevent duplicate provider accounts
  • Timezone Support: Sessions and user timestamps include timezone information
  • Auto-Generated UUIDs: User IDs are automatically generated UUIDs for security
  • Auto-Update Timestamps: User updatedAt field automatically updates on changes

Database Adapters

The schema is implemented across different ORMs:

  • Drizzle: PostgreSQL schema with UUID and timestamp support
  • Prisma: Cross-database schema with auto-generated types
  • Mongoose: MongoDB schemas with document validation

Each adapter maintains the same logical structure while optimizing for their respective database features.