import { IS_CLOUD, isAdminPresent, validateRequest } from "@dokploy/server"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertTriangle } from "lucide-react"; import type { GetServerSidePropsContext } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import { type ReactElement, useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { OnboardingLayout } from "@/components/layouts/onboarding-layout"; import { SignInWithGithub } from "@/components/proprietary/auth/sign-in-with-github"; import { SignInWithGoogle } from "@/components/proprietary/auth/sign-in-with-google"; import { AlertBlock } from "@/components/shared/alert-block"; import { Logo } from "@/components/shared/logo"; import { Button } from "@/components/ui/button"; import { CardContent, CardDescription, CardTitle } from "@/components/ui/card"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { authClient } from "@/lib/auth-client"; const registerSchema = z .object({ name: z.string().min(1, { message: "First name is required", }), lastName: z.string().min(1, { message: "Last name is required", }), email: z .string() .min(1, { message: "Email is required", }) .email({ message: "Email must be a valid email", }), password: z .string() .min(1, { message: "Password is required", }) .refine((password) => password === "" || password.length >= 8, { message: "Password must be at least 8 characters", }), confirmPassword: z .string() .min(1, { message: "Password is required", }) .refine( (confirmPassword) => confirmPassword === "" || confirmPassword.length >= 8, { message: "Password must be at least 8 characters", }, ), }) .refine((data) => data.password === data.confirmPassword, { message: "Passwords do not match", path: ["confirmPassword"], }); type Register = z.infer; interface Props { hasAdmin: boolean; isCloud: boolean; } const Register = ({ isCloud }: Props) => { const router = useRouter(); const [isError, setIsError] = useState(false); const [error, setError] = useState(null); const [data, setData] = useState(null); const form = useForm({ defaultValues: { name: "", lastName: "", email: "", password: "", confirmPassword: "", }, resolver: zodResolver(registerSchema), }); useEffect(() => { form.reset(); }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (values: Register) => { const { data, error } = await authClient.signUp.email({ email: values.email, password: values.password, name: values.name, lastName: values.lastName, }); if (error) { setIsError(true); setError(error.message || "An error occurred"); } else { toast.success("User registered successfully", { duration: 2000, }); if (!isCloud) { router.push("/"); } else { setData(data); } } }; return (
{isCloud ? "Sign Up" : "Setup the server"} Enter your email and password to{" "} {isCloud ? "create an account" : "setup the server"}
{isError && (
{error}
)} {isCloud && data && ( Registered successfully, please check your inbox or spam folder to confirm your account. )} {isCloud && (
)} {isCloud && (

Or register with email

)}
( First Name )} /> ( Last Name )} /> ( Email )} /> ( Password )} /> ( Confirm Password )} />
{isCloud && (
Already have account? Sign in
)}
Need help? Contact us
); }; export default Register; Register.getLayout = (page: ReactElement) => { return {page}; }; export async function getServerSideProps(context: GetServerSidePropsContext) { if (IS_CLOUD) { const { user } = await validateRequest(context.req); if (user) { return { redirect: { permanent: true, destination: "/dashboard/projects", }, }; } return { props: { isCloud: true, }, }; } const hasAdmin = await isAdminPresent(); if (hasAdmin) { return { redirect: { permanent: false, destination: "/", }, }; } return { props: { isCloud: false, }, }; }