34 lines
737 B
TypeScript
34 lines
737 B
TypeScript
"use server";
|
|
|
|
import { AuthError } from "next-auth";
|
|
import { redirect } from "next/navigation";
|
|
import { signIn } from "@/auth";
|
|
|
|
export type LoginState = {
|
|
ok: boolean;
|
|
message?: string;
|
|
};
|
|
|
|
export async function login(_state: LoginState, formData: FormData): Promise<LoginState> {
|
|
const callbackUrl = formData.get("callbackUrl")?.toString();
|
|
|
|
try {
|
|
await signIn("credentials", {
|
|
email: formData.get("email"),
|
|
senha: formData.get("senha"),
|
|
redirectTo: callbackUrl || undefined
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
return {
|
|
ok: false,
|
|
message: "E-mail ou senha invalidos."
|
|
};
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
|
|
redirect(callbackUrl || "/briefings");
|
|
}
|