I am using next 14. using server action. this is the function:
export async function login(prevState: any, formData: FormData) {
const result = loginSchema.safeParse(Object.fromEntries(formData));
if (!result.success) {
return {
errors: result.error.flatten().fieldErrors,
};
}
const { username, password } = result.data;
if (username !== testUser.username || password !== testUser.password) {
return {
errors: {
username: ["Invalid username or password."],
},
};
}
await createSession(testUser.id);
redirect("/dashboard");
}
The function works perfectly in terms of validating the form, but when it comes to redirecting, it simply does not.
Trying to debug this for a day, but no clue what’s the problem,
this is the error from the server console:
тип Internal error: TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed
at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1175:13)