Ploy
Ploy
Authentication

Users and data

How Ploy Auth stores users, hashes passwords, and how to reference users from your own data.

Users and data

Ploy Auth keeps your application's users, not Ploy dashboard accounts, in a regular SQLite database in your organization. You can store your own tables in the same database and link them to users with foreign keys; a separate application database is optional.

Where users live

auth.binding in ploy.yaml names your application's auth database binding. New setups create a project-scoped database on the first production deploy. The same database is bound to your worker as env.AUTH_DB when the binding is AUTH_DB. Existing setups keep their database IDs.

TableContents
ploy_usersOne row per user: id, email (unique, lowercase), email_verified, password_hash, metadata (JSON), created_at, updated_at
ploy_sessionsOne row per session: user_id, SHA-256 token_hash, expires_at, revoked
ploy_identitiesLinked Google, Apple, and GitHub accounts: user_id, provider, provider_user_id, email, email_verified, profile (name and avatar), encrypted provider tokens and granted scopes
ploy_passkeysRegistered passkeys: user_id, credential id, public_key, signature counter, transports, name, last_used_at
ploy_tokensSingle-use sign-in links and short-lived provider-refresh leases: purpose, SHA-256 token_hash, email, user_id, expires_at, used_at

password_hash is an empty string for users who only use social sign-in, passkeys, or sign-in links. Expired tokens are deleted automatically a day after they expire. Sessions, identities, and passkeys reference ploy_users(id) with ON DELETE CASCADE, so deleting a user removes those auth records. Your own tables follow the deletion policy you define.

Ploy creates and upgrades these tables itself. Treat them as read-only from your code and use the endpoints to change accounts.

Older setups may share an organization database between projects. Ploy does not move or split existing users automatically. Check each project's auth database before importing users; a shared database means shared users.

Password hashing

New passwords are hashed with scrypt (N = 214, r = 8, p = 5, 16-byte random salt, 64-byte key), one of the OWASP-recommended configurations. The stored value records its parameters:

scrypt$16384$8$5$<salt, base64url>$<hash, base64url>

Accounts created before scrypt use PBKDF2-HMAC-SHA512 with 100,000 iterations (<salt>:<hash>). They keep working and are rehashed with scrypt on the user's next successful sign-in. The same upgrade path applies if the parameters are raised later.

Passwords must be 8 to 128 characters. Unknown emails get the same response and the same default hashing cost as a wrong password for a current Ploy hash.

Owners and admins can import existing users, including Better Auth scrypt password hashes and social identities, without changing their IDs. Better Auth's N=16384, r=16, p=1 hashes use more memory but less total hashing work than Ploy's default; they are verified with their original parameters and upgraded after a successful password sign-in. Raw Better Auth hashes must be labeled explicitly during import because their salt:hash syntax is also used by Ploy's legacy PBKDF2 hashes.

Referencing users from your data

Use ploy_users.id as the user key in your own tables. Treat it as an opaque string: older accounts have short IDs and newer ones UUIDs.

Same database: foreign keys and joins

Use the auth database for your application tables when you want SQLite to enforce relationships to users. env.AUTH_DB is a normal database binding: it can query your own tables as well as read the auth tables.

To apply your migrations to that database, give it a second, non-auth binding:

  1. Deploy production with auth.binding: AUTH_DB so Ploy initializes auth.
  2. Open Auth in the dashboard, select your project, and copy its Database name.
  3. Add a db binding using that exact database name, then deploy production again:
ploy.yaml
auth:
  binding: AUTH_DB
db:
  DB: <your-auth-database-name>

Replace <your-auth-database-name> with the name from the dashboard. AUTH_DB and DB then point to the same database, not copies. New auth databases are project-scoped; do not assume the name is auth_db. An older setup may still use that name.

Ploy creates or upgrades its auth tables before applying application migrations. The migration runner excludes the auth binding name, not every binding pointing at the same database, so it can apply your migrations through DB. With only one non-auth binding, use migrations/; with multiple non-auth bindings, use migrations/DB/.

migrations/001_create_posts.sql
CREATE TABLE posts (
  id INTEGER PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES ploy_users(id) ON DELETE CASCADE,
  title TEXT NOT NULL,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX posts_user_id ON posts(user_id);

The database service enables foreign-key enforcement. Inserts with an unknown user_id fail, and deleting a user deletes their posts in this example. Choose your own policy: ON DELETE RESTRICT blocks deletion while posts exist; ON DELETE SET NULL retains posts but requires a nullable user_id. Tables in the same database can also use SQL joins with ploy_users.

The bindings are aliases, not permission boundaries: both can write every table in that database. Keep application writes and migrations limited to your own tables. Do not insert, update, delete, rename, or drop Ploy-managed auth tables; use the auth endpoints and dashboard for account changes. Exclude those tables from ORM-generated schema changes and leave ploy_internal_db_migrations to Ploy. A foreign key enforces existence, not authorization: resolve the signed-in user on the server and scope application queries to their ID.

Separate databases: application-managed references

Keep a separate database if you prefer that separation:

ploy.yaml
kind: dynamic
build: pnpm build
out: dist
auth:
  binding: AUTH_DB
db:
  DB: app
migrations/001_create_posts.sql
CREATE TABLE posts (
  id INTEGER PRIMARY KEY,
  user_id TEXT NOT NULL,
  title TEXT NOT NULL,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX posts_user_id ON posts(user_id);

The tables live in different databases, so there is no SQL foreign key between them. Resolve the user from the session, then query with the ID:

const user = await env.PLOY_AUTH.getUser(token);
if (!user) {
	return new Response("Unauthorized", { status: 401 });
}

const { results } = await env.DB.prepare(
	"SELECT * FROM posts WHERE user_id = ? ORDER BY created_at DESC",
)
	.bind(user.id)
	.all();

For admin views that need user details next to your data, read ploy_users through env.AUTH_DB and merge in code. Store app-specific profile data in your own tables keyed by user_id, or in metadata for a few small fields set at sign-up.

When you delete a user, also delete or anonymize their rows in the separate application database. SQLite cannot enforce foreign keys or perform ordinary joins across these bindings, and auth deletion does not automatically clean up that database.

Lifecycle hooks and user metadata

Lifecycle hooks can seed public user metadata at sign-up and notify your application of new users and sessions. Metadata can originate from client input; keep permissions, suspensions, and entitlement decisions in server-owned tables, and never put credentials or secrets in metadata. Notifications are best-effort, not a replacement for querying the auth database.

Deployments and environments

  • Previews share production auth by default, including its database and users. Owners and admins can disable Enable on previews under Auth → Settings; see preview access. Preview deployments never provision auth or apply migrations.
  • Settings apply instantly to production auth. auth.allow_signups and auth.require_email_verification in ploy.yaml seed a new setup only; dashboard changes are preserved on redeploy. New setups default to sign-ups closed and email verification required. Existing settings, including verification opt-outs, stay unchanged.
  • Your migrations run on production deploys of the default branch, after Ploy has created the auth tables.
  • Local development with ploy dev keeps local users separate from production. Before applying migrations, it creates the production auth tables in each of the project's db databases and keeps their ploy_users rows in sync with local sign-ups, under the same IDs, so migrations and inserts referencing ploy_users(id) behave as in production. Like production, migrations run with foreign keys off and fail if they leave a violation. Read sessions through PLOY_AUTH or /_ploy/auth/me.

How is this guide?

Last updated on