27 lines
624 B
TypeScript
27 lines
624 B
TypeScript
"use server";
|
|
|
|
import { AuthError } from "next-auth";
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { signIn } from "@/auth";
|
|
|
|
export async function loginAction(formData: FormData) {
|
|
const callbackUrl = String(formData.get("callbackUrl") ?? "/");
|
|
|
|
try {
|
|
await signIn("credentials", {
|
|
adminName: formData.get("adminName"),
|
|
password: formData.get("password"),
|
|
redirectTo: callbackUrl,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
redirect(
|
|
`/login?error=CredentialsSignin&callbackUrl=${encodeURIComponent(callbackUrl)}`
|
|
);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|