chore: formatting
This commit is contained in:
parent
bff9cfdb9c
commit
a2ab882315
6 changed files with 77 additions and 89 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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<BetterAuthOptions>
|
||||
>;
|
||||
export type AuthMiddlewareContext = MiddlewareContext<MiddlewareOptions, AuthContext<BetterAuthOptions>>;
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue