* Signs in the user with the provided credentials.
* Sets the JWT cookie and redirects to the dashboard on success.
* @param _prevState - Previous state, unused.
* @param formData - Form data containing 'identifier' and 'password'.
* @returns A message indicating the failure of the operation, or redirects to the dashboard on success.
*/
export async function sign_in(
_prevState: unknown,
formData: FormData
): Promise<FormMessage> {
try {
const identifier = formData.get("identifier") as string;
const password = formData.get("password") as string;
const response = await fetch(AUTH_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ identifier, password }),
});
const { jwt, error } = await response.json();
await new Promise((resolve) => setTimeout(resolve, 1500));
if (error) {
if (error.status === 400) {
return { message: "Invalid username or password" };
}
return { message: error.message || "Server Error", type: "error" };
}
if (jwt) {
setJwtCookie(jwt);
}
} catch (error) {
return { message: "Server Error" };
}
redirect("/dashboard");
}