From 79d4943c00f01d8e3d7956cdce0ecd5e04b6d790 Mon Sep 17 00:00:00 2001 From: luciano Date: Wed, 29 Apr 2026 12:47:40 +0200 Subject: [PATCH] feat(db): add Better Auth tables (Session, Account, Verification) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - User extended with emailVerified, name, image (Better Auth fields) - Session: signed session tokens with expiry, ip and user agent - Account: credential rows for email+password (provider 'credential') and OAuth providers (e.g. Google) — password hash stored on the credential row - Verification: single-use tokens for email verification, password reset, and magic links - Re-export new types from @ketopath/db Migration 20260429104721_add_auth_tables run against local Postgres. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../migration.sql | 64 +++++++++++++++++ packages/db/prisma/schema.prisma | 68 +++++++++++++++++-- packages/db/src/index.ts | 2 +- 3 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 packages/db/prisma/migrations/20260429104721_add_auth_tables/migration.sql diff --git a/packages/db/prisma/migrations/20260429104721_add_auth_tables/migration.sql b/packages/db/prisma/migrations/20260429104721_add_auth_tables/migration.sql new file mode 100644 index 0000000..3e05b34 --- /dev/null +++ b/packages/db/prisma/migrations/20260429104721_add_auth_tables/migration.sql @@ -0,0 +1,64 @@ +-- AlterTable +ALTER TABLE "users" ADD COLUMN "email_verified" BOOLEAN NOT NULL DEFAULT false, +ADD COLUMN "image" TEXT, +ADD COLUMN "name" TEXT; + +-- CreateTable +CREATE TABLE "sessions" ( + "id" TEXT NOT NULL, + "user_id" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expires_at" TIMESTAMP(3) NOT NULL, + "ip_address" TEXT, + "user_agent" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "sessions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "accounts" ( + "id" TEXT NOT NULL, + "user_id" TEXT NOT NULL, + "account_id" TEXT NOT NULL, + "provider_id" TEXT NOT NULL, + "access_token" TEXT, + "refresh_token" TEXT, + "id_token" TEXT, + "access_token_expires_at" TIMESTAMP(3), + "refresh_token_expires_at" TIMESTAMP(3), + "scope" TEXT, + "password" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "verifications" ( + "id" TEXT NOT NULL, + "identifier" TEXT NOT NULL, + "value" TEXT NOT NULL, + "expires_at" TIMESTAMP(3) NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "verifications_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "sessions_token_key" ON "sessions"("token"); + +-- CreateIndex +CREATE UNIQUE INDEX "accounts_provider_id_account_id_key" ON "accounts"("provider_id", "account_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "verifications_identifier_value_key" ON "verifications"("identifier", "value"); + +-- AddForeignKey +ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index b0e94e9..ce968fc 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -52,18 +52,76 @@ enum FastingProtocol { } model User { - id String @id @default(cuid()) - email String @unique - role Role @default(USER) - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") + id String @id @default(cuid()) + email String @unique + emailVerified Boolean @default(false) @map("email_verified") + name String? + image String? + role Role @default(USER) + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @updatedAt @map("updated_at") profile Profile? preferences Preferences? + sessions Session[] + accounts Account[] @@map("users") } +// Better Auth — sessione utente firmata. +model Session { + id String @id @default(cuid()) + userId String @map("user_id") + token String @unique + expiresAt DateTime @map("expires_at") + ipAddress String? @map("ip_address") + userAgent String? @map("user_agent") + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @updatedAt @map("updated_at") + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@map("sessions") +} + +// Better Auth — credenziali (email+password) o account OAuth. +// Per email+password il provider è 'credential' e la password (hash scrypt) +// è salvata nel campo `password`. +model Account { + id String @id @default(cuid()) + userId String @map("user_id") + accountId String @map("account_id") + providerId String @map("provider_id") + accessToken String? @map("access_token") + refreshToken String? @map("refresh_token") + idToken String? @map("id_token") + accessTokenExpiresAt DateTime? @map("access_token_expires_at") + refreshTokenExpiresAt DateTime? @map("refresh_token_expires_at") + scope String? + password String? + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @updatedAt @map("updated_at") + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@unique([providerId, accountId]) + @@map("accounts") +} + +// Better Auth — token monouso (email verification, password reset, magic link). +model Verification { + id String @id @default(cuid()) + identifier String + value String + expiresAt DateTime @map("expires_at") + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @updatedAt @map("updated_at") + + @@unique([identifier, value]) + @@map("verifications") +} + model Profile { id String @id @default(cuid()) userId String @unique @map("user_id") diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index e733bd1..cf10914 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -8,4 +8,4 @@ export { Prisma, Role, } from '@prisma/client'; -export type { Preferences, Profile, User } from '@prisma/client'; +export type { Account, Preferences, Profile, Session, User, Verification } from '@prisma/client';