35 lines
1000 B
TypeScript
35 lines
1000 B
TypeScript
"use server";
|
|
import { encodedRedirect } from "@/utils/utils";
|
|
import { createClient } from "@/utils/supabase/server";
|
|
import { redirect } from "next/navigation";
|
|
|
|
export const signInAction = async (formData: FormData) => {
|
|
const email = formData.get("email") as string;
|
|
const supabase = await createClient();
|
|
|
|
if (!email || email === "") {
|
|
return encodedRedirect("error", "/sign-in", "Email is required");
|
|
}
|
|
|
|
const { data, error } = await supabase.auth.signInWithOtp({
|
|
email,
|
|
options: {
|
|
shouldCreateUser: true,
|
|
},
|
|
});
|
|
|
|
if (error && error.message.includes("User not found")) {
|
|
// Encode email parameter untuk keamanan
|
|
const encodedEmail = encodeURIComponent(email);
|
|
return redirect(`/sign-in?email=${encodedEmail}`);
|
|
}
|
|
|
|
if (error) {
|
|
return encodedRedirect("error", "/sign-in", error.message);
|
|
}
|
|
|
|
// Redirect dengan email parameter
|
|
const encodedEmail = encodeURIComponent(email);
|
|
return redirect(`/verify-otp?email=${encodedEmail}`);
|
|
};
|