Files
Fastchat/app/login/actions.ts
T

36 lines
939 B
TypeScript

"use server";
import { redirect } from "next/navigation";
import { login } from "@/utils/authentication/signin";
export type LoginState = {
error?: string;
} | null;
export async function loginAction(
_prevState: LoginState,
formData: FormData,
): Promise<LoginState> {
const email = formData.get("email");
const password = formData.get("password");
const next = formData.get("next");
if (typeof email !== "string" || typeof password !== "string") {
return { error: "Email and password are required." };
}
try {
const result = await login(email, password);
if (result?.error) {
return { error: result.error.message ?? "Login failed." };
}
} catch (error) {
const message = error instanceof Error ? error.message : "Login failed.";
return { error: message };
}
const redirectTo =
typeof next === "string" && next.startsWith("/") ? next : "/profile";
redirect(redirectTo);
}