-
Hi there, first, I'm just learning remix and remix-auth and I bumped into something I don't understand. I have the following set: export const authenticator = new Authenticator<User>(sessionStorage);
authenticator.use(
new FormStrategy(async ({ form }) => {
console.debug(form); // NodeFormData { _fields: {} }
const email = form.get("email");
const password = form.get("password");
invariant(email, "Email is required");
invariant(password, "Password is required");
const user = await verifyLogin(email, password);
invariant(user, "User/pass not found");
return user;
}),
"user-pass"
);
And from the login route I have the following: export const action: ActionFunction = async ({ request }) => {
const formData = await request.formData();
const email = formData.get("email");
const password = formData.get("password");
let redirectTo = formData.get("redirectTo");
if (typeof redirectTo !== "string" || redirectTo === "") {
redirectTo = "/";
}
if (!validateEmail(email)) {
return json<ActionData>(
{ errors: { email: "Email is invalid" } },
{ status: 400 }
);
}
if (typeof password !== "string") {
return json<ActionData>(
{ errors: { password: "Password is required" } },
{ status: 400 }
);
}
if (password.length < 8) {
return json<ActionData>(
{ errors: { password: "Password is too short" } },
{ status: 400 }
);
}
console.debug(request);
// it is reaching this point with data for both email and password
return await authenticator.authenticate("user-pass", request, {
successRedirect: redirectTo,
failureRedirect: "/login",
throwOnError: true,
});
}; Data is being sent from a form using post and it is available within the action. Any help please? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The problem is you read the request body before passing the request to Remix Auth, the body of a request can only be read once, if you want to read it before you must clone it first. Also, all of your code in the action could go inside the FormStrategy. |
Beta Was this translation helpful? Give feedback.
The problem is you read the request body before passing the request to Remix Auth, the body of a request can only be read once, if you want to read it before you must clone it first.
Also, all of your code in the action could go inside the FormStrategy.