Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(auth): add missing verified column to two-factor table (#1105)

Corentin Thomassetafb5fe339298
diff --git a/.changeset/quiet-moons-attack.md b/.changeset/quiet-moons-attack.md
new file mode 100644
index 00000000..e4478a2a
--- /dev/null
+++ b/.changeset/quiet-moons-attack.md
@@ -0,0 +1,5 @@
+---
+"@papra/app": patch
+---
+
+Fixed a regression issue breaking the two factor authentication after updating `better-auth`.
diff --git a/apps/papra-server/src/migrations/list/0019-two-factor-verified.migration.ts b/apps/papra-server/src/migrations/list/0019-two-factor-verified.migration.ts
new file mode 100644
index 00000000..977bcd14
--- /dev/null
+++ b/apps/papra-server/src/migrations/list/0019-two-factor-verified.migration.ts
@@ -0,0 +1,22 @@
+import type { Migration } from '../migrations.types';
+import { sql } from 'drizzle-orm';
+
+export const twoFactorVerifiedMigration = {
+ name: 'two-factor-verified',
+
+ up: async ({ db }) => {
+ const tableInfo = await db.run(sql`PRAGMA table_info(auth_two_factor)`);
+ const existingColumns = tableInfo.rows.map(row => row.name);
+ const hasColumn = (columnName: string) => existingColumns.includes(columnName);
+
+ if (!hasColumn('verified')) {
+ // Default to true so existing two-factor setups (which were already verified
+ // before better-auth introduced this column) keep working after the upgrade.
+ await db.run(sql`ALTER TABLE "auth_two_factor" ADD COLUMN "verified" integer DEFAULT true NOT NULL;`);
+ }
+ },
+
+ down: async ({ db }) => {
+ await db.run(sql`ALTER TABLE "auth_two_factor" DROP COLUMN "verified";`);
+ },
+} satisfies Migration;
diff --git a/apps/papra-server/src/migrations/migrations.registry.test.ts b/apps/papra-server/src/migrations/migrations.registry.test.ts
index 6744ad0c..bad13fbc 100644
--- a/apps/papra-server/src/migrations/migrations.registry.test.ts
+++ b/apps/papra-server/src/migrations/migrations.registry.test.ts
@@ -108,7 +108,7 @@ describe('migrations registry', () => {
CREATE TABLE "api_keys" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "name" text NOT NULL, "key_hash" text NOT NULL, "prefix" text NOT NULL, "user_id" text NOT NULL, "last_used_at" integer, "expires_at" integer, "permissions" text DEFAULT '[]' NOT NULL, "all_organizations" integer DEFAULT false NOT NULL, FOREIGN KEY ("user_id") REFERENCES "users"("id") ON UPDATE cascade ON DELETE cascade );
CREATE TABLE "auth_accounts" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "user_id" text, "account_id" text NOT NULL, "provider_id" text NOT NULL, "access_token" text, "refresh_token" text, "access_token_expires_at" integer, "refresh_token_expires_at" integer, "scope" text, "id_token" text, "password" text, FOREIGN KEY ("user_id") REFERENCES "users"("id") ON UPDATE cascade ON DELETE cascade );
CREATE TABLE "auth_sessions" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "token" text NOT NULL, "user_id" text, "expires_at" integer NOT NULL, "ip_address" text, "user_agent" text, "active_organization_id" text, FOREIGN KEY ("user_id") REFERENCES "users"("id") ON UPDATE cascade ON DELETE cascade, FOREIGN KEY ("active_organization_id") REFERENCES "organizations"("id") ON UPDATE cascade ON DELETE set null );
- CREATE TABLE "auth_two_factor" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "user_id" text, "secret" text, "backup_codes" text, FOREIGN KEY ("user_id") REFERENCES "users"("id") ON UPDATE cascade ON DELETE cascade );
+ CREATE TABLE "auth_two_factor" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "user_id" text, "secret" text, "backup_codes" text, "verified" integer DEFAULT true NOT NULL, FOREIGN KEY ("user_id") REFERENCES "users"("id") ON UPDATE cascade ON DELETE cascade );
CREATE TABLE "auth_verifications" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "identifier" text NOT NULL, "value" text NOT NULL, "expires_at" integer NOT NULL );
CREATE TABLE "custom_property_definitions" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "organization_id" text NOT NULL REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE, "name" text NOT NULL, "key" text NOT NULL, "description" text, "type" text NOT NULL, "config" text, "display_order" integer NOT NULL DEFAULT 0 );
CREATE TABLE "custom_property_select_options" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "property_definition_id" text NOT NULL REFERENCES "custom_property_definitions"("id") ON DELETE CASCADE ON UPDATE CASCADE, "name" text NOT NULL, "key" text NOT NULL, "display_order" integer NOT NULL DEFAULT 0 );
diff --git a/apps/papra-server/src/migrations/migrations.registry.ts b/apps/papra-server/src/migrations/migrations.registry.ts
index 8368a150..4907314e 100644
--- a/apps/papra-server/src/migrations/migrations.registry.ts
+++ b/apps/papra-server/src/migrations/migrations.registry.ts
@@ -18,6 +18,7 @@ import { indexDocumentsFtsIdsMigration } from './list/0015-index-documents-fts-i
import { caseInsensitiveTagNameUniqConstraintMigration } from './list/0016-case-insensitive-tag-name-uniq-constraint.migration';
import { documentsDateMigration } from './list/0017-documents-date.migration';
import { customPropertiesMigration } from './list/0018-custom-properties.migration';
+import { twoFactorVerifiedMigration } from './list/0019-two-factor-verified.migration';
export const migrations: Migration[] = [
initialSchemaSetupMigration,
@@ -38,4 +39,5 @@ export const migrations: Migration[] = [
caseInsensitiveTagNameUniqConstraintMigration,
documentsDateMigration,
customPropertiesMigration,
+ twoFactorVerifiedMigration,
];
diff --git a/apps/papra-server/src/modules/app/auth/auth.tables.ts b/apps/papra-server/src/modules/app/auth/auth.tables.ts
index e6f2072a..6e036d60 100644
--- a/apps/papra-server/src/modules/app/auth/auth.tables.ts
+++ b/apps/papra-server/src/modules/app/auth/auth.tables.ts
@@ -66,5 +66,8 @@ export const twoFactorTable = sqliteTable(
userId: text('user_id').references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
secret: text('secret'),
backupCodes: text('backup_codes'),
+ // Default to true for backfill purposes
+ // from better auth : https://github.com/better-auth/better-auth/blob/4cbc823e694c1adb10ef26cf8294263453a156d3/packages/better-auth/src/plugins/two-factor/schema.ts#L42
+ verified: integer('verified', { mode: 'boolean' }).notNull().default(true),
},
);