Templates/SaaS & accounts

Authentication & sessions database schema

Users with linked identities, sessions, password resets and MFA factors.

5 tables, 4 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • emailtext not null
  • email_verified_attimestamptz
  • password_hashtext
  • created_attimestamptz not null

identities

  • iduuid not null
  • user_id> usersuuid not null
  • providertext not null
  • provider_user_idtext not null
  • created_attimestamptz not null

sessions

  • iduuid not null
  • user_id> usersuuid not null
  • user_agenttext
  • ip_addressinet
  • expires_attimestamptz not null
  • created_attimestamptz not null

password_resets

  • iduuid not null
  • user_id> usersuuid not null
  • token_hashtext not null
  • expires_attimestamptz not null
  • used_attimestamptz
  • created_attimestamptz not null

mfa_factors

  • iduuid not null
  • user_id> usersuuid not null
  • kindtext not null
  • secrettext not null
  • verified_attimestamptz
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "identities" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "provider" TEXT NOT NULL,
  "provider_user_id" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "mfa_factors" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "kind" TEXT NOT NULL,
  "secret" TEXT NOT NULL,
  "verified_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "password_resets" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "token_hash" TEXT NOT NULL UNIQUE,
  "expires_at" TIMESTAMPTZ NOT NULL,
  "used_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "sessions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "user_agent" TEXT,
  "ip_address" INET,
  "expires_at" TIMESTAMPTZ NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL UNIQUE,
  "email_verified_at" TIMESTAMPTZ,
  "password_hash" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "identities" ADD CONSTRAINT "fk_identities_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "sessions" ADD CONSTRAINT "fk_sessions_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "password_resets" ADD CONSTRAINT "fk_password_resets_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "mfa_factors" ADD CONSTRAINT "fk_mfa_factors_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

More SaaS & accounts templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.