Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import UserValidationUtils from "~/utils/app/UserValidationUtils";
|
||||
import type { TFunction } from "i18next";
|
||||
import { useActionData, useLoaderData } from "react-router";
|
||||
import { ActionFunction, LoaderFunctionArgs, MetaFunction } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getUserByEmail, updateUserPassword } from "~/utils/db/users.db.server";
|
||||
import { hashPassword } from "~/utils/auth/passwordHasher.server";
|
||||
import EventsService from "~/modules/events/services/.server/EventsService";
|
||||
import { UserPasswordUpdatedDto } from "~/modules/events/dtos/UserPasswordUpdatedDto";
|
||||
import IpAddressServiceServer from "~/modules/ipAddress/services/IpAddressService.server";
|
||||
import ResetPasswordScreen from "~/modules/users/components/ResetPasswordScreen";
|
||||
import { createUserSession, getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { loadRequiredWebConfig } from "@toolctrl/config";
|
||||
import { absoluteAppRootPath } from "~/utils/app/CrossHostUrls";
|
||||
import { isPasswordResetTokenValid } from "~/services/auth/password-reset-token.server";
|
||||
import { checkAuthRateLimit } from "~/services/auth/auth-rate-limit.server";
|
||||
|
||||
type LoaderData = {
|
||||
email: string;
|
||||
};
|
||||
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const url = new URL(request.url);
|
||||
let email = url.searchParams.get("e") ?? "";
|
||||
if (email) {
|
||||
email = decodeURIComponent(email);
|
||||
}
|
||||
return {
|
||||
title: `${t("account.reset.title")} | ${process.env.APP_NAME}`,
|
||||
email,
|
||||
};
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
success?: string;
|
||||
error?: string;
|
||||
fields?: {
|
||||
email: string;
|
||||
verifyToken: string;
|
||||
};
|
||||
};
|
||||
const translateActionText = (t: TFunction, key: string, fallback: string) => {
|
||||
const translated = t(key, { defaultValue: fallback });
|
||||
return translated === key ? fallback : translated;
|
||||
};
|
||||
const translateIpAddressError = (t: TFunction, error: string) => {
|
||||
if (error === "blacklisted") {
|
||||
return t("admin.accounts.ipAddresses.errors.blacklisted");
|
||||
}
|
||||
if (error === "unauthorized") {
|
||||
return t("admin.accounts.ipAddresses.errors.unauthorized");
|
||||
}
|
||||
return error;
|
||||
};
|
||||
export const action: ActionFunction = async ({ request }) => {
|
||||
const { t } = await getTranslations(request);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const form = await request.formData();
|
||||
const email = form.get("email")?.toString().toLowerCase().trim() ?? "";
|
||||
const verifyToken = form.get("verify-token")?.toString() ?? "";
|
||||
const password = form.get("password")?.toString() ?? "";
|
||||
const passwordConfirm = form.get("password-confirm")?.toString() ?? "";
|
||||
|
||||
const ipError = await IpAddressServiceServer.log(request, { action: "reset", description: email }).catch((e) => e.message);
|
||||
if (ipError) {
|
||||
return Response.json({ error: translateIpAddressError(t, ipError) }, { status: 400 });
|
||||
}
|
||||
|
||||
const fields = {
|
||||
email,
|
||||
verifyToken,
|
||||
};
|
||||
if (!email) {
|
||||
return Response.json({ error: translateActionText(t, "account.reset.errors.emailRequired", "Email required"), fields }, { status: 400 });
|
||||
}
|
||||
const invalidResetTokenError = translateActionText(t, "account.reset.errors.invalidOrExpiredToken", "Invalid or expired reset token.");
|
||||
const rateLimit = checkAuthRateLimit({
|
||||
request,
|
||||
action: "reset-password",
|
||||
identifier: email,
|
||||
limit: 5,
|
||||
windowMs: 15 * 60 * 1000,
|
||||
});
|
||||
if (!rateLimit.allowed) {
|
||||
return Response.json({ error: invalidResetTokenError, fields }, { status: 429, headers: { "Retry-After": String(rateLimit.retryAfterSeconds) } });
|
||||
}
|
||||
const passwordError = await UserValidationUtils.validatePasswords({ t, password, passwordConfirm, email });
|
||||
if (passwordError) {
|
||||
return Response.json({ error: passwordError, fields }, { status: 400 });
|
||||
}
|
||||
|
||||
const user = await getUserByEmail(email);
|
||||
if (!user) {
|
||||
return Response.json({ error: invalidResetTokenError, fields }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!isPasswordResetTokenValid(user, verifyToken)) {
|
||||
return Response.json({ error: invalidResetTokenError, fields }, { status: 400 });
|
||||
}
|
||||
|
||||
const passwordHash = await hashPassword(password);
|
||||
await updateUserPassword({ passwordHash }, user.id);
|
||||
await EventsService.create({
|
||||
request,
|
||||
event: "user.password.updated",
|
||||
tenantId: null,
|
||||
userId: user.id,
|
||||
data: {
|
||||
user: { id: user.id, email: user.email },
|
||||
} satisfies UserPasswordUpdatedDto,
|
||||
});
|
||||
|
||||
const userInfo = await getUserInfo(request);
|
||||
return createUserSession(
|
||||
{
|
||||
...userInfo,
|
||||
userId: user.id,
|
||||
lng: user.locale ?? userInfo.lng,
|
||||
},
|
||||
absoluteAppRootPath(loadRequiredWebConfig().routing)
|
||||
);
|
||||
};
|
||||
|
||||
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
||||
|
||||
export default function ResetRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
// D-D0a — Davids "Reset Password" (+ Success/Invalid-States). Token aus URL, action validiert serverseitig.
|
||||
return <ResetPasswordScreen data={data} actionData={actionData} />;
|
||||
}
|
||||
Reference in new issue
Block a user