mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-10 05:24:20 +00:00
56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
"use server";
|
|
|
|
import { AuthError } from "next-auth";
|
|
import { z } from "zod";
|
|
|
|
import { signIn, signOut } from "@/auth.config";
|
|
import { authFormSchema } from "@/types";
|
|
|
|
const formSchemaSignIn = authFormSchema("sign-in");
|
|
|
|
const defaultValues: z.infer<typeof formSchemaSignIn> = {
|
|
email: "",
|
|
password: "",
|
|
};
|
|
|
|
export async function authenticate(
|
|
prevState: unknown,
|
|
formData: z.infer<typeof formSchemaSignIn>,
|
|
) {
|
|
try {
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
await signIn("credentials", {
|
|
...formData,
|
|
redirect: false,
|
|
});
|
|
return {
|
|
message: "Success",
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
switch (error.type) {
|
|
case "CredentialsSignin":
|
|
return {
|
|
message: "Credentials error",
|
|
errors: {
|
|
...defaultValues,
|
|
credentials: "Incorrect email or password",
|
|
},
|
|
};
|
|
default:
|
|
return {
|
|
message: "Unknown error",
|
|
errors: {
|
|
...defaultValues,
|
|
unknown: "Unknown error",
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function logOut() {
|
|
await signOut();
|
|
}
|