27 lines
835 B
SQL
27 lines
835 B
SQL
-- 0002_sessions.sql
|
|
-- Create sessions table for auth tokens
|
|
|
|
CREATE TABLE sessions (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
token_hash TEXT UNIQUE NOT NULL,
|
|
user_id UUID NOT NULL REFERENCES users(id),
|
|
user_email_id UUID REFERENCES user_emails(id),
|
|
token_type TEXT NOT NULL,
|
|
auth_method TEXT NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
expires_at TIMESTAMPTZ NOT NULL,
|
|
revoked_at TIMESTAMPTZ,
|
|
ip_address INET,
|
|
user_agent TEXT,
|
|
is_used BOOLEAN DEFAULT FALSE
|
|
);
|
|
|
|
-- Index for user session lookups (logout all, etc.)
|
|
CREATE INDEX sessions_user_id_idx ON sessions (user_id);
|
|
|
|
-- Index for expiration cleanup
|
|
CREATE INDEX sessions_expires_at_idx ON sessions (expires_at);
|
|
|
|
-- Index for token type filtering
|
|
CREATE INDEX sessions_token_type_idx ON sessions (token_type);
|