Add password on confirmation when changing email (#4503)

* Add email confirmation

* Confirm password only if email was changed

Co-authored-by: Omar López <zomars@me.com>
This commit is contained in:
Joe Au-YeungandOmar López authored and GitHub committed 2022-09-17 23:27:25 +01:00
1 parent 2ef8e230c5
commit cf32287109
4 files changed
+120 -46

No files matched your search

+69 -44
View File
@@ -8,56 +8,81 @@ import { TRPCError } from "@trpc/server";
import { createProtectedRouter } from "../../createRouter";
export const authRouter = createProtectedRouter().mutation("changePassword", {
input: z.object({
oldPassword: z.string(),
newPassword: z.string(),
}),
async resolve({ input, ctx }) {
const { oldPassword, newPassword } = input;
export const authRouter = createProtectedRouter()
.mutation("changePassword", {
input: z.object({
oldPassword: z.string(),
newPassword: z.string(),
}),
async resolve({ input, ctx }) {
const { oldPassword, newPassword } = input;
const { user } = ctx;
const { user } = ctx;
if (user.identityProvider !== IdentityProvider.CAL) {
throw new TRPCError({ code: "FORBIDDEN", message: "THIRD_PARTY_IDENTITY_PROVIDER_ENABLED" });
}
if (user.identityProvider !== IdentityProvider.CAL) {
throw new TRPCError({ code: "FORBIDDEN", message: "THIRD_PARTY_IDENTITY_PROVIDER_ENABLED" });
}
const currentPasswordQuery = await prisma.user.findFirst({
where: {
id: user.id,
},
select: {
password: true,
},
});
const currentPasswordQuery = await prisma.user.findFirst({
where: {
id: user.id,
},
select: {
password: true,
},
});
const currentPassword = currentPasswordQuery?.password;
const currentPassword = currentPasswordQuery?.password;
if (!currentPassword) {
throw new TRPCError({ code: "NOT_FOUND", message: "MISSING_PASSWORD" });
}
if (!currentPassword) {
throw new TRPCError({ code: "NOT_FOUND", message: "MISSING_PASSWORD" });
}
const passwordsMatch = await verifyPassword(oldPassword, currentPassword);
if (!passwordsMatch) {
throw new TRPCError({ code: "BAD_REQUEST", message: "INCORRECT_PASSWORD" });
}
const passwordsMatch = await verifyPassword(oldPassword, currentPassword);
if (!passwordsMatch) {
throw new TRPCError({ code: "BAD_REQUEST", message: "INCORRECT_PASSWORD" });
}
if (oldPassword === newPassword) {
throw new TRPCError({ code: "BAD_REQUEST", message: "PASSWORD_MATCHES_OLD" });
}
if (oldPassword === newPassword) {
throw new TRPCError({ code: "BAD_REQUEST", message: "PASSWORD_MATCHES_OLD" });
}
if (!validPassword(newPassword)) {
throw new TRPCError({ code: "BAD_REQUEST", message: "INVALID_PASSWORD" });
}
if (!validPassword(newPassword)) {
throw new TRPCError({ code: "BAD_REQUEST", message: "INVALID_PASSWORD" });
}
const hashedPassword = await hashPassword(newPassword);
await prisma.user.update({
where: {
id: user.id,
},
data: {
password: hashedPassword,
},
});
},
});
const hashedPassword = await hashPassword(newPassword);
await prisma.user.update({
where: {
id: user.id,
},
data: {
password: hashedPassword,
},
});
},
})
.mutation("verifyPassword", {
input: z.object({
passwordInput: z.string(),
}),
async resolve({ input, ctx }) {
const user = await prisma.user.findUnique({
where: {
id: ctx.user.id,
},
});
if (!user?.password) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
const passwordsMatch = await verifyPassword(input.passwordInput, user.password);
if (!passwordsMatch) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return;
},
});
+1 -1
View File
@@ -269,7 +269,7 @@ export const PasswordField = forwardRef<HTMLInputElement, InputFieldProps>(funct
<Tooltip content={textLabel}>
<button
className="absolute bottom-0 right-3 h-9 text-gray-900"
className="absolute bottom-2 right-3 h-9 text-gray-900"
type="button"
onClick={() => toggleIsPasswordVisible()}>
{isPasswordVisible ? (