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) {
|
if (existingMembership) {
|
||||||
tx.update(member).set({ organizationId }).where(eq(member.id, existingMembership.id)).run();
|
tx.update(member).set({ organizationId }).where(eq(member.id, existingMembership.id)).run();
|
||||||
} else {
|
} else {
|
||||||
tx.insert(member).values({
|
tx.insert(member)
|
||||||
id: Bun.randomUUIDv7(),
|
.values({
|
||||||
organizationId,
|
id: Bun.randomUUIDv7(),
|
||||||
userId,
|
organizationId,
|
||||||
role: "member",
|
userId,
|
||||||
createdAt: new Date(),
|
role: "member",
|
||||||
}).run();
|
createdAt: new Date(),
|
||||||
|
})
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.delete(sessionsTable).where(eq(sessionsTable.userId, userId)).run();
|
tx.delete(sessionsTable).where(eq(sessionsTable.userId, userId)).run();
|
||||||
|
|
|
||||||
|
|
@ -75,8 +75,7 @@ const rekeyTwoFactor = async (legacySecret: string) => {
|
||||||
db.transaction((tx) => {
|
db.transaction((tx) => {
|
||||||
for (const record of updates) {
|
for (const record of updates) {
|
||||||
try {
|
try {
|
||||||
tx
|
tx.update(twoFactor)
|
||||||
.update(twoFactor)
|
|
||||||
.set({
|
.set({
|
||||||
secret: record.secret,
|
secret: record.secret,
|
||||||
backupCodes: record.backupCodes,
|
backupCodes: record.backupCodes,
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ const resetPassword = async (username: string, newPassword: string) => {
|
||||||
const legacyHash = user.passwordHash ? await Bun.password.hash(newPassword) : null;
|
const legacyHash = user.passwordHash ? await Bun.password.hash(newPassword) : null;
|
||||||
|
|
||||||
db.transaction((tx) => {
|
db.transaction((tx) => {
|
||||||
tx
|
tx.update(account)
|
||||||
.update(account)
|
|
||||||
.set({ password: newPasswordHash })
|
.set({ password: newPasswordHash })
|
||||||
.where(and(eq(account.userId, user.id), eq(account.providerId, "credential")))
|
.where(and(eq(account.userId, user.id), eq(account.providerId, "credential")))
|
||||||
.run();
|
.run();
|
||||||
|
|
|
||||||
|
|
@ -65,45 +65,53 @@ export const convertLegacyUserOnFirstLogin = async (ctx: AuthMiddlewareContext)
|
||||||
db.transaction((tx) => {
|
db.transaction((tx) => {
|
||||||
tx.delete(usersTable).where(eq(usersTable.id, legacyUser.id)).run();
|
tx.delete(usersTable).where(eq(usersTable.id, legacyUser.id)).run();
|
||||||
|
|
||||||
tx.insert(usersTable).values({
|
tx.insert(usersTable)
|
||||||
id: newUserId,
|
.values({
|
||||||
username: legacyUser.username,
|
id: newUserId,
|
||||||
email: legacyUser.email,
|
username: legacyUser.username,
|
||||||
name: legacyUser.name,
|
email: legacyUser.email,
|
||||||
hasDownloadedResticPassword: legacyUser.hasDownloadedResticPassword,
|
name: legacyUser.name,
|
||||||
emailVerified: false,
|
hasDownloadedResticPassword: legacyUser.hasDownloadedResticPassword,
|
||||||
role: "admin", // In legacy system, the only user is an admin
|
emailVerified: false,
|
||||||
}).run();
|
role: "admin", // In legacy system, the only user is an admin
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
|
||||||
tx.insert(account).values({
|
tx.insert(account)
|
||||||
id: accountId,
|
.values({
|
||||||
providerId: "credential",
|
id: accountId,
|
||||||
accountId: legacyUser.username,
|
providerId: "credential",
|
||||||
userId: newUserId,
|
accountId: legacyUser.username,
|
||||||
password: passwordHash,
|
userId: newUserId,
|
||||||
createdAt: new Date(),
|
password: passwordHash,
|
||||||
}).run();
|
createdAt: new Date(),
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
|
||||||
// Migrate organization membership to the new user
|
// Migrate organization membership to the new user
|
||||||
// The old membership was cascade-deleted when the old user was deleted
|
// The old membership was cascade-deleted when the old user was deleted
|
||||||
if (oldMembership?.organization) {
|
if (oldMembership?.organization) {
|
||||||
tx.insert(member).values({
|
tx.insert(member)
|
||||||
id: Bun.randomUUIDv7(),
|
.values({
|
||||||
userId: newUserId,
|
id: Bun.randomUUIDv7(),
|
||||||
organizationId: oldMembership.organization.id,
|
userId: newUserId,
|
||||||
role: oldMembership.role,
|
organizationId: oldMembership.organization.id,
|
||||||
createdAt: new Date(),
|
role: oldMembership.role,
|
||||||
}).run();
|
createdAt: new Date(),
|
||||||
|
})
|
||||||
|
.run();
|
||||||
} else if (newOrganizationData) {
|
} else if (newOrganizationData) {
|
||||||
tx.insert(organization).values(newOrganizationData).run();
|
tx.insert(organization).values(newOrganizationData).run();
|
||||||
|
|
||||||
tx.insert(member).values({
|
tx.insert(member)
|
||||||
id: Bun.randomUUIDv7(),
|
.values({
|
||||||
userId: newUserId,
|
id: Bun.randomUUIDv7(),
|
||||||
organizationId: newOrganizationData.id,
|
userId: newUserId,
|
||||||
role: "owner",
|
organizationId: newOrganizationData.id,
|
||||||
createdAt: new Date(),
|
role: "owner",
|
||||||
}).run();
|
createdAt: new Date(),
|
||||||
|
})
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -6,32 +6,19 @@ import {
|
||||||
type MiddlewareOptions,
|
type MiddlewareOptions,
|
||||||
} from "better-auth";
|
} from "better-auth";
|
||||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||||
import {
|
import { admin, createAuthMiddleware, twoFactor, username, organization } from "better-auth/plugins";
|
||||||
admin,
|
|
||||||
createAuthMiddleware,
|
|
||||||
twoFactor,
|
|
||||||
username,
|
|
||||||
organization,
|
|
||||||
} from "better-auth/plugins";
|
|
||||||
import { UnauthorizedError } from "http-errors-enhanced";
|
import { UnauthorizedError } from "http-errors-enhanced";
|
||||||
import { convertLegacyUserOnFirstLogin } from "./auth-middlewares/convert-legacy-user";
|
import { convertLegacyUserOnFirstLogin } from "./auth-middlewares/convert-legacy-user";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { config } from "../core/config";
|
import { config } from "../core/config";
|
||||||
import { db } from "../db/db";
|
import { db } from "../db/db";
|
||||||
import { cryptoUtils } from "../utils/crypto";
|
import { cryptoUtils } from "../utils/crypto";
|
||||||
import {
|
import { organization as organizationTable, member, usersTable } from "../db/schema";
|
||||||
organization as organizationTable,
|
|
||||||
member,
|
|
||||||
usersTable,
|
|
||||||
} from "../db/schema";
|
|
||||||
import { ensureOnlyOneUser } from "./auth-middlewares/only-one-user";
|
import { ensureOnlyOneUser } from "./auth-middlewares/only-one-user";
|
||||||
import { authService } from "../modules/auth/auth.service";
|
import { authService } from "../modules/auth/auth.service";
|
||||||
import { tanstackStartCookies } from "better-auth/tanstack-start";
|
import { tanstackStartCookies } from "better-auth/tanstack-start";
|
||||||
|
|
||||||
export type AuthMiddlewareContext = MiddlewareContext<
|
export type AuthMiddlewareContext = MiddlewareContext<MiddlewareOptions, AuthContext<BetterAuthOptions>>;
|
||||||
MiddlewareOptions,
|
|
||||||
AuthContext<BetterAuthOptions>
|
|
||||||
>;
|
|
||||||
|
|
||||||
export const auth = betterAuth({
|
export const auth = betterAuth({
|
||||||
secret: await cryptoUtils.deriveSecret("better-auth"),
|
secret: await cryptoUtils.deriveSecret("better-auth"),
|
||||||
|
|
@ -72,45 +59,41 @@ export const auth = betterAuth({
|
||||||
return { data: user };
|
return { data: user };
|
||||||
},
|
},
|
||||||
after: async (user) => {
|
after: async (user) => {
|
||||||
const slug =
|
const slug = user.email.split("@")[0] + "-" + Math.random().toString(36).slice(-4);
|
||||||
user.email.split("@")[0] +
|
|
||||||
"-" +
|
|
||||||
Math.random().toString(36).slice(-4);
|
|
||||||
|
|
||||||
const resticPassword = cryptoUtils.generateResticPassword();
|
const resticPassword = cryptoUtils.generateResticPassword();
|
||||||
const metadata = {
|
const metadata = {
|
||||||
resticPassword:
|
resticPassword: await cryptoUtils.sealSecret(resticPassword),
|
||||||
await cryptoUtils.sealSecret(resticPassword),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db.transaction((tx) => {
|
db.transaction((tx) => {
|
||||||
const orgId = Bun.randomUUIDv7();
|
const orgId = Bun.randomUUIDv7();
|
||||||
|
|
||||||
tx.insert(organizationTable).values({
|
tx.insert(organizationTable)
|
||||||
name: `${user.name}'s Workspace`,
|
.values({
|
||||||
slug: slug,
|
name: `${user.name}'s Workspace`,
|
||||||
id: orgId,
|
slug: slug,
|
||||||
createdAt: new Date(),
|
id: orgId,
|
||||||
metadata,
|
createdAt: new Date(),
|
||||||
}).run();
|
metadata,
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
|
||||||
tx.insert(member).values({
|
tx.insert(member)
|
||||||
id: Bun.randomUUIDv7(),
|
.values({
|
||||||
userId: user.id,
|
id: Bun.randomUUIDv7(),
|
||||||
role: "owner",
|
userId: user.id,
|
||||||
organizationId: orgId,
|
role: "owner",
|
||||||
createdAt: new Date(),
|
organizationId: orgId,
|
||||||
}).run();
|
createdAt: new Date(),
|
||||||
|
})
|
||||||
|
.run();
|
||||||
});
|
});
|
||||||
} catch {
|
} catch {
|
||||||
await db
|
await db.delete(usersTable).where(eq(usersTable.id, user.id));
|
||||||
.delete(usersTable)
|
|
||||||
.where(eq(usersTable.id, user.id));
|
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(`Failed to create organization for user ${user.id}`);
|
||||||
`Failed to create organization for user ${user.id}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -123,9 +106,7 @@ export const auth = betterAuth({
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!orgMembership) {
|
if (!orgMembership) {
|
||||||
throw new UnauthorizedError(
|
throw new UnauthorizedError("User does not belong to any organization");
|
||||||
"User does not belong to any organization",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -323,8 +323,7 @@ const reorderSchedules = async (scheduleIds: number[]) => {
|
||||||
db.transaction((tx) => {
|
db.transaction((tx) => {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
for (const [index, scheduleId] of scheduleIds.entries()) {
|
for (const [index, scheduleId] of scheduleIds.entries()) {
|
||||||
tx
|
tx.update(backupSchedulesTable)
|
||||||
.update(backupSchedulesTable)
|
|
||||||
.set({ sortOrder: index, updatedAt: now })
|
.set({ sortOrder: index, updatedAt: now })
|
||||||
.where(and(eq(backupSchedulesTable.id, scheduleId), eq(backupSchedulesTable.organizationId, organizationId)))
|
.where(and(eq(backupSchedulesTable.id, scheduleId), eq(backupSchedulesTable.organizationId, organizationId)))
|
||||||
.run();
|
.run();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue