zerobyte/app/server/lib/auth-middlewares/convert-legacy-user.ts
dependabot[bot] d53b24bfb4
chore(deps): bump drizzle-orm from 1.0.0-beta.9-e89174b to 1.0.0-beta.15-859cf75 (#501)
* chore(deps): bump drizzle-orm

Bumps [drizzle-orm](https://github.com/drizzle-team/drizzle-orm) from 1.0.0-beta.9-e89174b to 1.0.0-beta.15-859cf75.
- [Release notes](https://github.com/drizzle-team/drizzle-orm/releases)
- [Commits](https://github.com/drizzle-team/drizzle-orm/commits)

---
updated-dependencies:
- dependency-name: drizzle-orm
  dependency-version: 1.0.0-beta.15-859cf75
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* refactor: convert async transactions to sync + .run()

* chore: formatting

* chore: add lefthook dep

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Nicolas Meienberger <github@thisprops.com>
2026-02-14 14:18:20 +01:00

121 lines
3.2 KiB
TypeScript

import { hashPassword } from "better-auth/crypto";
import { eq } from "drizzle-orm";
import { db } from "~/server/db/db";
import { account, usersTable, member, organization } from "~/server/db/schema";
import type { AuthMiddlewareContext } from "../auth";
import { UnauthorizedError } from "http-errors-enhanced";
import { cryptoUtils } from "~/server/utils/crypto";
export const convertLegacyUserOnFirstLogin = async (ctx: AuthMiddlewareContext) => {
const { path, body } = ctx;
if (path !== "/sign-in/username") {
return;
}
const legacyUser = await db.query.usersTable.findFirst({
where: {
AND: [
{ username: body.username.trim().toLowerCase() },
{ passwordHash: { NOT: "" } },
{ passwordHash: { isNotNull: true } },
],
},
});
if (legacyUser) {
const isValid = await Bun.password.verify(body.password, legacyUser.passwordHash ?? "");
if (isValid) {
const newUserId = crypto.randomUUID();
const accountId = crypto.randomUUID();
const oldMembership = await db.query.member.findFirst({
where: { userId: legacyUser.id },
with: {
organization: true,
},
});
const passwordHash = await hashPassword(body.password);
let newOrganizationData: {
id: string;
name: string;
slug: string;
createdAt: Date;
metadata: {
resticPassword: string;
};
} | null = null;
if (!oldMembership?.organization) {
const resticPassword = cryptoUtils.generateResticPassword();
newOrganizationData = {
id: Bun.randomUUIDv7(),
name: `${legacyUser.name}'s Workspace`,
slug: legacyUser.email.split("@")[0] + "-" + Math.random().toString(36).slice(-4),
createdAt: new Date(),
metadata: {
resticPassword: await cryptoUtils.sealSecret(resticPassword),
},
};
}
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(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();
} 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();
}
});
} else {
throw new UnauthorizedError("Invalid credentials");
}
}
};