From a2ab88231527f5f794cd15fa0d7eb1011734a358 Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Sat, 14 Feb 2026 14:12:43 +0100 Subject: [PATCH] chore: formatting --- .../cli/commands/assign-organization.ts | 16 +++-- app/server/cli/commands/rekey-2fa.ts | 3 +- app/server/cli/commands/reset-password.ts | 3 +- .../auth-middlewares/convert-legacy-user.ts | 70 ++++++++++-------- app/server/lib/auth.ts | 71 +++++++------------ app/server/modules/backups/backups.service.ts | 3 +- 6 files changed, 77 insertions(+), 89 deletions(-) diff --git a/app/server/cli/commands/assign-organization.ts b/app/server/cli/commands/assign-organization.ts index 57deab32..59fdab1b 100644 --- a/app/server/cli/commands/assign-organization.ts +++ b/app/server/cli/commands/assign-organization.ts @@ -42,13 +42,15 @@ const assignUserToOrganization = async (userId: string, organizationId: string) if (existingMembership) { tx.update(member).set({ organizationId }).where(eq(member.id, existingMembership.id)).run(); } else { - tx.insert(member).values({ - id: Bun.randomUUIDv7(), - organizationId, - userId, - role: "member", - createdAt: new Date(), - }).run(); + tx.insert(member) + .values({ + id: Bun.randomUUIDv7(), + organizationId, + userId, + role: "member", + createdAt: new Date(), + }) + .run(); } tx.delete(sessionsTable).where(eq(sessionsTable.userId, userId)).run(); diff --git a/app/server/cli/commands/rekey-2fa.ts b/app/server/cli/commands/rekey-2fa.ts index 618a06aa..c52b8513 100644 --- a/app/server/cli/commands/rekey-2fa.ts +++ b/app/server/cli/commands/rekey-2fa.ts @@ -75,8 +75,7 @@ const rekeyTwoFactor = async (legacySecret: string) => { db.transaction((tx) => { for (const record of updates) { try { - tx - .update(twoFactor) + tx.update(twoFactor) .set({ secret: record.secret, backupCodes: record.backupCodes, diff --git a/app/server/cli/commands/reset-password.ts b/app/server/cli/commands/reset-password.ts index b86fbe00..fb9225fa 100644 --- a/app/server/cli/commands/reset-password.ts +++ b/app/server/cli/commands/reset-password.ts @@ -21,8 +21,7 @@ const resetPassword = async (username: string, newPassword: string) => { const legacyHash = user.passwordHash ? await Bun.password.hash(newPassword) : null; db.transaction((tx) => { - tx - .update(account) + tx.update(account) .set({ password: newPasswordHash }) .where(and(eq(account.userId, user.id), eq(account.providerId, "credential"))) .run(); diff --git a/app/server/lib/auth-middlewares/convert-legacy-user.ts b/app/server/lib/auth-middlewares/convert-legacy-user.ts index d21fcf6c..5be364d4 100644 --- a/app/server/lib/auth-middlewares/convert-legacy-user.ts +++ b/app/server/lib/auth-middlewares/convert-legacy-user.ts @@ -65,45 +65,53 @@ export const convertLegacyUserOnFirstLogin = async (ctx: AuthMiddlewareContext) db.transaction((tx) => { tx.delete(usersTable).where(eq(usersTable.id, legacyUser.id)).run(); - tx.insert(usersTable).values({ - id: newUserId, - username: legacyUser.username, - email: legacyUser.email, - name: legacyUser.name, - hasDownloadedResticPassword: legacyUser.hasDownloadedResticPassword, - emailVerified: false, - role: "admin", // In legacy system, the only user is an admin - }).run(); + tx.insert(usersTable) + .values({ + id: newUserId, + username: legacyUser.username, + email: legacyUser.email, + name: legacyUser.name, + hasDownloadedResticPassword: legacyUser.hasDownloadedResticPassword, + emailVerified: false, + role: "admin", // In legacy system, the only user is an admin + }) + .run(); - tx.insert(account).values({ - id: accountId, - providerId: "credential", - accountId: legacyUser.username, - userId: newUserId, - password: passwordHash, - createdAt: new Date(), - }).run(); + tx.insert(account) + .values({ + id: accountId, + providerId: "credential", + accountId: legacyUser.username, + userId: newUserId, + password: passwordHash, + createdAt: new Date(), + }) + .run(); // Migrate organization membership to the new user // The old membership was cascade-deleted when the old user was deleted if (oldMembership?.organization) { - tx.insert(member).values({ - id: Bun.randomUUIDv7(), - userId: newUserId, - organizationId: oldMembership.organization.id, - role: oldMembership.role, - createdAt: new Date(), - }).run(); + tx.insert(member) + .values({ + id: Bun.randomUUIDv7(), + userId: newUserId, + organizationId: oldMembership.organization.id, + role: oldMembership.role, + createdAt: new Date(), + }) + .run(); } else if (newOrganizationData) { tx.insert(organization).values(newOrganizationData).run(); - tx.insert(member).values({ - id: Bun.randomUUIDv7(), - userId: newUserId, - organizationId: newOrganizationData.id, - role: "owner", - createdAt: new Date(), - }).run(); + tx.insert(member) + .values({ + id: Bun.randomUUIDv7(), + userId: newUserId, + organizationId: newOrganizationData.id, + role: "owner", + createdAt: new Date(), + }) + .run(); } }); } else { diff --git a/app/server/lib/auth.ts b/app/server/lib/auth.ts index 414e7576..58f0bd6e 100644 --- a/app/server/lib/auth.ts +++ b/app/server/lib/auth.ts @@ -6,32 +6,19 @@ import { type MiddlewareOptions, } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; -import { - admin, - createAuthMiddleware, - twoFactor, - username, - organization, -} from "better-auth/plugins"; +import { admin, createAuthMiddleware, twoFactor, username, organization } from "better-auth/plugins"; import { UnauthorizedError } from "http-errors-enhanced"; import { convertLegacyUserOnFirstLogin } from "./auth-middlewares/convert-legacy-user"; import { eq } from "drizzle-orm"; import { config } from "../core/config"; import { db } from "../db/db"; import { cryptoUtils } from "../utils/crypto"; -import { - organization as organizationTable, - member, - usersTable, -} from "../db/schema"; +import { organization as organizationTable, member, usersTable } from "../db/schema"; import { ensureOnlyOneUser } from "./auth-middlewares/only-one-user"; import { authService } from "../modules/auth/auth.service"; import { tanstackStartCookies } from "better-auth/tanstack-start"; -export type AuthMiddlewareContext = MiddlewareContext< - MiddlewareOptions, - AuthContext ->; +export type AuthMiddlewareContext = MiddlewareContext>; export const auth = betterAuth({ secret: await cryptoUtils.deriveSecret("better-auth"), @@ -72,45 +59,41 @@ export const auth = betterAuth({ return { data: user }; }, after: async (user) => { - const slug = - user.email.split("@")[0] + - "-" + - Math.random().toString(36).slice(-4); + const slug = user.email.split("@")[0] + "-" + Math.random().toString(36).slice(-4); const resticPassword = cryptoUtils.generateResticPassword(); const metadata = { - resticPassword: - await cryptoUtils.sealSecret(resticPassword), + resticPassword: await cryptoUtils.sealSecret(resticPassword), }; try { db.transaction((tx) => { const orgId = Bun.randomUUIDv7(); - tx.insert(organizationTable).values({ - name: `${user.name}'s Workspace`, - slug: slug, - id: orgId, - createdAt: new Date(), - metadata, - }).run(); + tx.insert(organizationTable) + .values({ + name: `${user.name}'s Workspace`, + slug: slug, + id: orgId, + createdAt: new Date(), + metadata, + }) + .run(); - tx.insert(member).values({ - id: Bun.randomUUIDv7(), - userId: user.id, - role: "owner", - organizationId: orgId, - createdAt: new Date(), - }).run(); + tx.insert(member) + .values({ + id: Bun.randomUUIDv7(), + userId: user.id, + role: "owner", + organizationId: orgId, + createdAt: new Date(), + }) + .run(); }); } catch { - await db - .delete(usersTable) - .where(eq(usersTable.id, user.id)); + await db.delete(usersTable).where(eq(usersTable.id, user.id)); - throw new Error( - `Failed to create organization for user ${user.id}`, - ); + throw new Error(`Failed to create organization for user ${user.id}`); } }, }, @@ -123,9 +106,7 @@ export const auth = betterAuth({ }); if (!orgMembership) { - throw new UnauthorizedError( - "User does not belong to any organization", - ); + throw new UnauthorizedError("User does not belong to any organization"); } return { diff --git a/app/server/modules/backups/backups.service.ts b/app/server/modules/backups/backups.service.ts index 901424bb..99cf88a7 100644 --- a/app/server/modules/backups/backups.service.ts +++ b/app/server/modules/backups/backups.service.ts @@ -323,8 +323,7 @@ const reorderSchedules = async (scheduleIds: number[]) => { db.transaction((tx) => { const now = Date.now(); for (const [index, scheduleId] of scheduleIds.entries()) { - tx - .update(backupSchedulesTable) + tx.update(backupSchedulesTable) .set({ sortOrder: index, updatedAt: now }) .where(and(eq(backupSchedulesTable.id, scheduleId), eq(backupSchedulesTable.organizationId, organizationId))) .run();