Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 6x 2x 2x 2x 2x 1x 1x 1x | import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { passwordSchema } from "~app/utils/validation";
import { generatePasswordData, getHash } from "~utils/server/crypto";
import { authProcedure } from "~web/handlers/trpc";
export const procedure = authProcedure
.meta({
title: "Change user password",
description:
"Changes the current user's password after verifying the previous password matches.",
})
.input(
z.strictObject({
prevPassword: passwordSchema,
password: passwordSchema,
}),
)
.mutation(async ({ input, ctx }) => {
const { database } = ctx;
const user = await database
.selectFrom("users")
.select(["passwordHash", "passwordSalt"])
.where("id", "=", ctx.auth.userId)
.executeTakeFirstOrThrow();
const isPrevPasswordValid =
(await getHash(input.prevPassword, user.passwordSalt)) ===
user.passwordHash;
if (!isPrevPasswordValid) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: `Change password of user "${ctx.auth.email}" failed: password doesn't match.`,
});
}
const passwordData = await generatePasswordData(ctx, input.password);
await database
.updateTable("users")
.set({
passwordHash: passwordData.hash,
passwordSalt: passwordData.salt,
})
.where("users.id", "=", ctx.auth.userId)
.executeTakeFirst();
});
|