fosrl.pangolin/server/routers/auth/changePassword.ts

127 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-10-05 15:11:51 -04:00
import { Request, Response, NextFunction } from "express";
import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode";
import { fromError } from "zod-validation-error";
import { z } from "zod";
import { db } from "@server/db";
import { User, users } from "@server/db/schema";
import { eq } from "drizzle-orm";
2025-01-01 21:41:31 -05:00
import { response } from "@server/lib";
import {
hashPassword,
verifyPassword
} from "@server/auth/password";
import { verifyTotpCode } from "@server/auth/totp";
2024-12-21 21:01:12 -05:00
import logger from "@server/logger";
2025-01-01 21:41:31 -05:00
import { unauthorized } from "@server/auth/unauthorizedResponse";
import { invalidateAllSessions } from "@server/auth/sessions/app";
import { passwordSchema } from "@server/auth/passwordSchema";
2024-10-05 15:11:51 -04:00
2025-01-01 21:41:31 -05:00
export const changePasswordBody = z
.object({
oldPassword: z.string(),
newPassword: passwordSchema,
code: z.string().optional()
})
.strict();
2024-10-05 15:11:51 -04:00
export type ChangePasswordBody = z.infer<typeof changePasswordBody>;
export type ChangePasswordResponse = {
codeRequested?: boolean;
};
export async function changePassword(
req: Request,
res: Response,
2025-01-01 21:41:31 -05:00
next: NextFunction
2024-10-05 15:11:51 -04:00
): Promise<any> {
const parsedBody = changePasswordBody.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2025-01-01 21:41:31 -05:00
fromError(parsedBody.error).toString()
)
2024-10-05 15:11:51 -04:00
);
}
const { newPassword, oldPassword, code } = parsedBody.data;
const user = req.user as User;
try {
if (newPassword === oldPassword) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2025-01-01 21:41:31 -05:00
"New password cannot be the same as the old password"
)
2024-10-05 15:11:51 -04:00
);
}
const validPassword = await verifyPassword(
oldPassword,
2025-01-01 21:41:31 -05:00
user.passwordHash
2024-10-05 15:11:51 -04:00
);
if (!validPassword) {
return next(unauthorized());
}
if (user.twoFactorEnabled) {
if (!code) {
return response<{ codeRequested: boolean }>(res, {
data: { codeRequested: true },
success: true,
error: false,
message: "Two-factor authentication required",
2025-01-01 21:41:31 -05:00
status: HttpCode.ACCEPTED
2024-10-05 15:11:51 -04:00
});
}
2024-10-05 15:45:01 -04:00
const validOTP = await verifyTotpCode(
code!,
user.twoFactorSecret!,
2025-01-01 21:41:31 -05:00
user.userId
2024-10-05 15:45:01 -04:00
);
2024-10-05 15:11:51 -04:00
if (!validOTP) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2025-01-01 21:41:31 -05:00
"The two-factor code you entered is incorrect"
)
2024-10-05 15:11:51 -04:00
);
}
}
const hash = await hashPassword(newPassword);
await db
.update(users)
.set({
2025-01-01 21:41:31 -05:00
passwordHash: hash
2024-10-05 15:11:51 -04:00
})
2024-10-13 17:13:47 -04:00
.where(eq(users.userId, user.userId));
2024-10-05 15:11:51 -04:00
2024-10-13 17:13:47 -04:00
await invalidateAllSessions(user.userId);
2024-10-05 15:11:51 -04:00
2024-10-05 17:01:49 -04:00
// TODO: send email to user confirming password change
2024-10-05 15:11:51 -04:00
return response(res, {
data: null,
success: true,
error: false,
message: "Password changed successfully",
2025-01-01 21:41:31 -05:00
status: HttpCode.OK
2024-10-05 15:11:51 -04:00
});
} catch (error) {
2024-12-21 21:01:12 -05:00
logger.error(error);
2024-10-05 15:11:51 -04:00
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
2025-01-01 21:41:31 -05:00
"Failed to authenticate user"
)
2024-10-05 15:11:51 -04:00
);
}
}