feat(impersonation): migrate to betterauth admin plugin for admin status, add impersonation

This commit is contained in:
waleed
2026-01-15 15:10:58 -08:00
parent e499cc4f82
commit 197ada5df2
19 changed files with 10547 additions and 112 deletions

View File

@@ -0,0 +1,3 @@
ALTER TABLE "session" ADD COLUMN "impersonated_by" text;--> statement-breakpoint
ALTER TABLE "user" ADD COLUMN "role" text;--> statement-breakpoint
ALTER TABLE "session" ADD CONSTRAINT "session_impersonated_by_user_id_fk" FOREIGN KEY ("impersonated_by") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;

File diff suppressed because it is too large Load Diff

View File

@@ -988,6 +988,13 @@
"when": 1768421319400,
"tag": "0141_daffy_marten_broadcloak",
"breakpoints": true
},
{
"idx": 142,
"version": "7",
"when": 1768518361916,
"tag": "0142_solid_black_bolt",
"breakpoints": true
}
]
}

View File

@@ -39,6 +39,7 @@ export const user = pgTable('user', {
updatedAt: timestamp('updated_at').notNull(),
stripeCustomerId: text('stripe_customer_id'),
isSuperUser: boolean('is_super_user').notNull().default(false),
role: text('role'), // Used by Better Auth admin plugin for impersonation
})
export const session = pgTable(
@@ -57,6 +58,7 @@ export const session = pgTable(
activeOrganizationId: text('active_organization_id').references(() => organization.id, {
onDelete: 'set null',
}),
impersonatedBy: text('impersonated_by').references(() => user.id, { onDelete: 'cascade' }), // Admin user ID when impersonating
},
(table) => ({
userIdIdx: index('session_user_id_idx').on(table.userId),