154 lines
3.9 KiB
TypeScript
154 lines
3.9 KiB
TypeScript
"use server";
|
|
|
|
import { encodedRedirect } from "@/utils/utils";
|
|
import { createClient } from "@/utils/supabase/server";
|
|
import { headers } from "next/headers";
|
|
import { redirect } from "next/navigation";
|
|
import { Provider } from "@supabase/supabase-js";
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
export const signUpAction = async (formData: FormData) => {
|
|
const email = formData.get("email")?.toString();
|
|
const password = formData.get("password")?.toString();
|
|
const supabase = await createClient();
|
|
const origin = (await headers()).get("origin");
|
|
|
|
if (!email || !password) {
|
|
return encodedRedirect("error", "/login", "Email is required");
|
|
}
|
|
|
|
const { error } = await supabase.auth.signUp({
|
|
email,
|
|
password,
|
|
options: {
|
|
emailRedirectTo: `${origin}/auth/callback`,
|
|
},
|
|
});
|
|
|
|
if (error) {
|
|
console.error(error.code + " " + error.message);
|
|
return encodedRedirect("error", "/sign-up", error.message);
|
|
} else {
|
|
return encodedRedirect(
|
|
"success",
|
|
"/sign-up",
|
|
"Thanks for signing up! Please check your email for a verification link."
|
|
);
|
|
}
|
|
};
|
|
|
|
export const signInAction = async (formData: FormData) => {
|
|
const email = formData.get("email") as string;
|
|
const provider = formData.get("provider") as Provider;
|
|
const supabase = await createClient();
|
|
if (email) {
|
|
const { error } = await supabase.auth.signInWithOtp({
|
|
email,
|
|
options: {
|
|
emailRedirectTo: `${process.env.NEXT_PUBLIC_SITE_URL}/auth/callback`,
|
|
},
|
|
});
|
|
|
|
if (error) {
|
|
return encodedRedirect("error", "/login", error.message);
|
|
}
|
|
} else if (provider) {
|
|
const { error, data } = await supabase.auth.signInWithOAuth({
|
|
provider,
|
|
options: {
|
|
redirectTo: `${process.env.NEXT_PUBLIC_SITE_URL}/auth/callback`,
|
|
},
|
|
});
|
|
|
|
if (error) {
|
|
return encodedRedirect("error", "/login", error.message);
|
|
}
|
|
|
|
if (data?.url) {
|
|
return redirect(data.url);
|
|
} else {
|
|
return encodedRedirect("error", "/login", "Could not sign in");
|
|
}
|
|
}
|
|
|
|
revalidatePath("/", "layout");
|
|
redirect("/dashboard");
|
|
};
|
|
|
|
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."
|
|
);
|
|
};
|
|
|
|
export const resetPasswordAction = async (formData: FormData) => {
|
|
const supabase = await createClient();
|
|
|
|
const password = formData.get("password") as string;
|
|
const confirmPassword = formData.get("confirmPassword") as string;
|
|
|
|
if (!password || !confirmPassword) {
|
|
encodedRedirect(
|
|
"error",
|
|
"/protected/reset-password",
|
|
"Password and confirm password are required"
|
|
);
|
|
}
|
|
|
|
if (password !== confirmPassword) {
|
|
encodedRedirect(
|
|
"error",
|
|
"/protected/reset-password",
|
|
"Passwords do not match"
|
|
);
|
|
}
|
|
|
|
const { error } = await supabase.auth.updateUser({
|
|
password: password,
|
|
});
|
|
|
|
if (error) {
|
|
encodedRedirect(
|
|
"error",
|
|
"/protected/reset-password",
|
|
"Password update failed"
|
|
);
|
|
}
|
|
|
|
encodedRedirect("success", "/protected/reset-password", "Password updated");
|
|
};
|
|
|
|
export const signOutAction = async () => {
|
|
const supabase = await createClient();
|
|
await supabase.auth.signOut();
|
|
return redirect("/login");
|
|
};
|