ubiquitous-invention/packages/database/src/schema/users.ts

147 lines
5.6 KiB
TypeScript
Raw Normal View History

import { sql } from "drizzle-orm";
import {
pgTable,
uuid,
varchar,
text,
integer,
timestamp,
primaryKey,
uniqueIndex,
index,
} from "drizzle-orm/pg-core";
export const users = pgTable(
"users",
{
id: uuid("id").primaryKey().defaultRandom(),
email: varchar("email", { length: 255 }).notNull().unique(),
name: varchar("name", { length: 255 }),
avatarUrl: text("avatar_url"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => ({
emailIdx: index("users_email_idx").on(table.email),
// Belt-and-braces: existing column-level UNIQUE on `email` plus a
// case-insensitive UNIQUE on `lower(email)`. The latter prevents
// accidentally storing `Alice@x.com` and `alice@x.com` as two users
// and makes the case-insensitive lookups in `apps/web/lib/auth.ts`
// safe even if upstream rows were created mixed-case.
emailLowerUnique: uniqueIndex("users_email_lower_unique").on(sql`lower(${table.email})`),
}),
);
export const accounts = pgTable(
"accounts",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
type: varchar("type", { length: 255 }).notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("provider_account_id", { length: 255 }).notNull(),
refreshToken: text("refresh_token"),
accessToken: text("access_token"),
expiresAt: integer("expires_at"),
tokenType: varchar("token_type", { length: 255 }),
scope: varchar("scope", { length: 255 }),
idToken: text("id_token"),
sessionState: varchar("session_state", { length: 255 }),
},
(table) => ({
providerAccountUnique: uniqueIndex("accounts_provider_provider_account_id_unique").on(
table.provider,
table.providerAccountId,
),
userIdIdx: index("accounts_user_id_idx").on(table.userId),
}),
);
export const sessions = pgTable(
"sessions",
{
id: uuid("id").primaryKey().defaultRandom(),
sessionToken: varchar("session_token", { length: 255 }).notNull().unique(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expires: timestamp("expires", { withTimezone: true }).notNull(),
},
(table) => ({
userIdIdx: index("sessions_user_id_idx").on(table.userId),
}),
);
export const verificationTokens = pgTable(
"verification_tokens",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
expires: timestamp("expires", { withTimezone: true }).notNull(),
},
(table) => ({
pk: primaryKey({ columns: [table.identifier, table.token] }),
}),
);
feat(identity): schema + helpers + read-only profile UI (Task 1, part 1/2) First half of Task-multi-email-identity. Lays down everything except the NextAuth callback wiring, which is gated on a research subagent finishing its survey of OAuth provider behavior for the email_verified claim across GitHub, Google, and Authentik. Schema (packages/database): * New user_email_identities table colocated with `users` in users.ts. Columns: id, user_id (FK), email (lowercased), verified_at, source, created_at, last_used_at. * Indexes: user_id, email, unique(user_id, email), and a PARTIAL unique index on email WHERE verified_at IS NOT NULL — a verified email resolves to exactly one users row globally, while unverified rows (none today; placeholder for the manual-verification follow-up) do not share the constraint. * Drizzle relation: users.emailIdentities -> userEmailIdentities, and the inverse one(users) relation. * Migration 0005 generated by db:generate, augmented with a backfill INSERT that seeds one source='primary' identity per existing users row using created_at as verified_at. Migration applied to dev DB; existing admin@tasks.dev user verified as 1:1 mapped. Server (apps/web/server): * apps/web/server/lib/identity.ts exports two pure read helpers: - userOwnsEmail(userId, email): boolean used by the (upcoming) invite-accept procedure to verify the human controls the invited address under any of their linked identities. - findUserIdByVerifiedEmail(email): the replacement for the old ensureUserIdByEmail lookup. Will be called from auth.ts once the OAuth research subagent returns. * apps/web/server/routers/identity.ts exposes identity.listMine — a protected procedure returning the caller's identities ordered by verifiedAt desc. Cross-user identity surface is intentionally NOT exposed here; that lives behind the workspace-scoped autocomplete in Task 3 with its own tenancy fence. UI (apps/web/app): * New route /[workspaceSlug]/settings/profile renders a read-only "Linked emails" section with per-identity row (email, source badge, verified state, last-used relative time) plus a hint that explains how to add another email (sign in via that email's OAuth provider). * Empty / loading / error states all handled. The "no identities" branch should never fire post-backfill but renders a friendly message instead of throwing. What's NOT in this commit: * auth.ts changes (ensureUserIdByEmail -> ensureUserIdByVerifiedEmail, OAuth callback identity upsert, cross-user conflict rejection). Waiting on subagent research to land the callback wiring correctly on the first try across all three providers. * Vitest tests. The pure helpers are 10-line query shims and the behavior-relevant assertion is the auth callback path — easier to write meaningful tests once that lands. All three CI gates green: pnpm lint (14 pre-existing warnings, unchanged), pnpm type-check (6/6 packages), pnpm test (14/14 existing tests across @tasks/shared, @tasks/database, @tasks/ai). Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 11:07:50 -04:00
/**
* Multi-email identity. One `users` row can own many verified emails one
* "primary" (mirrored from `users.email` for cheap legacy lookups) plus
* any number of OAuth-claimed or manually-verified addresses.
*
* Why this exists: a user who signs in via GitHub (alice@personal) and
* later via Google (alice@gmail) would otherwise collide as two separate
* `users` rows under the old `ensureUserIdByEmail` lookup. The identity
* table is the source of truth for "which `users.id` does this email
* belong to," and the invite-accept flow uses `userOwnsEmail()` against
* it to verify that the human accepting an invite actually controls the
* invited address (under any of their linked identities, not just their
* primary one).
*
* Source values:
* - 'primary' mirror of `users.email` for the row that
* existed at user creation.
* - 'oauth:github' captured from a verified GitHub OAuth claim.
* - 'oauth:google' captured from a verified Google OAuth claim.
* - 'oauth:authentik' captured from a verified Authentik OIDC claim.
* - 'manual' added by the user via the (future) one-time-
* code verification flow.
*
* Constraints:
* - `(user_id, email)` unique: one user can't have the same email
* twice across sources. (A second provider claiming an email that's
* already linked just bumps `last_used_at`.)
* - `email` unique WHERE `verified_at IS NOT NULL`: a verified email
* can only resolve to one `users` row globally. Unverified rows
* (none exist yet, but the column is in place for the manual-verify
* flow) don't share the constraint.
*/
export const userEmailIdentities = pgTable(
"user_email_identities",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
/** Stored lowercased. Callers are responsible for `.toLowerCase()`. */
email: varchar("email", { length: 255 }).notNull(),
verifiedAt: timestamp("verified_at", { withTimezone: true }),
source: varchar("source", { length: 30 }).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
lastUsedAt: timestamp("last_used_at", { withTimezone: true }),
},
(table) => ({
userIdx: index("user_email_identities_user_id_idx").on(table.userId),
emailIdx: index("user_email_identities_email_idx").on(table.email),
userEmailUnique: uniqueIndex("user_email_identities_user_id_email_unique").on(
table.userId,
table.email,
),
verifiedEmailUnique: uniqueIndex("user_email_identities_verified_email_unique")
.on(table.email)
.where(sql`${table.verifiedAt} IS NOT NULL`),
}),
);