"use server"; import { createClient } from "@/utils/supabase/server"; export const signInAction = async (formData: { email: string }) => { const supabase = await createClient(); const encodeEmail = encodeURIComponent(formData.email); try { // First, check for existing session const { data: { session }, error: sessionError, } = await supabase.auth.getSession(); // If there's an active session and the email matches if (session && session.user.email === formData.email) { return { success: true, message: "Already logged in", redirectTo: "/protected/dashboard", // or wherever you want to redirect logged in users }; } // If no active session or different email, proceed with OTP const { data, error } = await supabase.auth.signInWithOtp({ email: formData.email, options: { shouldCreateUser: false, }, }); if (error) { return { success: false, error: error.message, redirectTo: `/verify-otp?email=${encodeEmail}`, }; } return { success: true, message: "OTP has been sent to your email", redirectTo: `/verify-otp?email=${encodeEmail}`, }; } catch (error) { return { success: false, error: "An unexpected error occurred", redirectTo: "/sign-in", }; } };