import { Logo } from "@/components/shared/logo"; import { Button } from "@/components/ui/button"; import { Card, 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 { isAdminPresent } from "@/server/api/services/admin"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertTriangle } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/router"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const registerSchema = z .object({ 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; } const Register = ({ hasAdmin }: Props) => { const router = useRouter(); const { mutateAsync, error, isError } = api.auth.createAdmin.useMutation(); const form = useForm({ defaultValues: { email: "", password: "", confirmPassword: "", }, resolver: zodResolver(registerSchema), }); useEffect(() => { form.reset(); }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (values: Register) => { await mutateAsync({ email: values.email, password: values.password, }) .then(() => { toast.success("User registration succesfuly", { duration: 2000, }); router.push("/"); }) .catch((e) => e); }; return (
Dokploy Setup the server Enter your email and password to setup the server
{isError && (
{error?.message}
)}
( Email )} /> ( Password )} /> ( Confirm Password )} />
{hasAdmin && (
Already have account? Sign in
)}
Need help? Contact us
); }; export default Register; export async function getServerSideProps() { const hasAdmin = await isAdminPresent(); if (hasAdmin) { return { redirect: { permanent: false, destination: "/", }, }; } return { props: { hasAdmin, }, }; }