mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign). Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files) and align docker/Dockerfile turbo --scope/--filter to the actual remix package name @hanzo/esign (was a pre-existing mismatch with -remix that broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { compare, hash } from '@node-rs/bcrypt';
|
|
import { UserSecurityAuditLogType } from '@prisma/client';
|
|
|
|
import { SALT_ROUNDS } from '@hanzo/esign-lib/constants/auth';
|
|
import type { RequestMetadata } from '@hanzo/esign-lib/universal/extract-request-metadata';
|
|
import { prisma } from '@hanzo/esign-prisma';
|
|
|
|
import { AppError } from '../../errors/app-error';
|
|
|
|
export type UpdatePasswordOptions = {
|
|
userId: number;
|
|
password: string;
|
|
currentPassword: string;
|
|
requestMetadata?: RequestMetadata;
|
|
};
|
|
|
|
export const updatePassword = async ({
|
|
userId,
|
|
password,
|
|
currentPassword,
|
|
requestMetadata,
|
|
}: UpdatePasswordOptions) => {
|
|
// Existence check
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
});
|
|
|
|
if (!user.password) {
|
|
throw new AppError('NO_PASSWORD');
|
|
}
|
|
|
|
const isCurrentPasswordValid = await compare(currentPassword, user.password);
|
|
if (!isCurrentPasswordValid) {
|
|
throw new AppError('INCORRECT_PASSWORD');
|
|
}
|
|
|
|
// Compare the new password with the old password
|
|
const isSamePassword = await compare(password, user.password);
|
|
if (isSamePassword) {
|
|
throw new AppError('SAME_PASSWORD');
|
|
}
|
|
|
|
const hashedNewPassword = await hash(password, SALT_ROUNDS);
|
|
|
|
return await prisma.$transaction(async (tx) => {
|
|
await tx.userSecurityAuditLog.create({
|
|
data: {
|
|
userId,
|
|
type: UserSecurityAuditLogType.PASSWORD_UPDATE,
|
|
userAgent: requestMetadata?.userAgent,
|
|
ipAddress: requestMetadata?.ipAddress,
|
|
},
|
|
});
|
|
|
|
return await tx.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
password: hashedNewPassword,
|
|
},
|
|
});
|
|
});
|
|
};
|