40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
"use server";
|
|
|
|
import { createClient } from "@/utils/supabase/server";
|
|
import { encodedRedirect } from "@/utils/utils";
|
|
import { headers } from "next/headers";
|
|
import { redirect } from "next/navigation";
|
|
|
|
export const forgotPasswordAction = async (formData: FormData) => {
|
|
const email = formData.get("email")?.toString();
|
|
const supabase = await createClient();
|
|
const origin = (await headers()).get("origin");
|
|
const callbackUrl = formData.get("callbackUrl")?.toString();
|
|
|
|
if (!email) {
|
|
return encodedRedirect("error", "/forgot-password", "Email is required");
|
|
}
|
|
|
|
const { error } = await supabase.auth.resetPasswordForEmail(email, {
|
|
redirectTo: `${origin}/auth/callback?redirect_to=/protected/reset-password`,
|
|
});
|
|
|
|
if (error) {
|
|
console.error(error.message);
|
|
return encodedRedirect(
|
|
"error",
|
|
"/forgot-password",
|
|
"Could not reset password",
|
|
);
|
|
}
|
|
|
|
if (callbackUrl) {
|
|
return redirect(callbackUrl);
|
|
}
|
|
|
|
return encodedRedirect(
|
|
"success",
|
|
"/forgot-password",
|
|
"Check your email for a link to reset your password.",
|
|
);
|
|
}; |