import Link from "next/link"; import { CardTitle, CardDescription, CardContent, Card, } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { type ReactElement, useEffect, useState } from "react"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { useRouter } from "next/router"; import { toast } from "sonner"; import { OnboardingLayout } from "@/components/layouts/onboarding-layout"; import type { GetServerSidePropsContext } from "next"; import { validateRequest } from "@/server/auth/auth"; import { api } from "@/utils/api"; import { isAdminPresent } from "@/server/api/services/admin"; import { Logo } from "@/components/shared/logo"; import { Login2FA } from "@/components/auth/login-2fa"; const loginSchema = 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", }) .min(8, { message: "Password must be at least 8 characters", }), }); type Login = z.infer; interface Props { hasAdmin: boolean; } type AuthResponse = { is2FAEnabled: boolean; authId: string; }; export default function Home({ hasAdmin }: Props) { const [temp, setTemp] = useState({ is2FAEnabled: false, authId: "", }); const { mutateAsync, isLoading } = api.auth.login.useMutation(); const router = useRouter(); const form = useForm({ defaultValues: { email: "", password: "", }, resolver: zodResolver(loginSchema), }); useEffect(() => { form.reset(); }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (values: Login) => { await mutateAsync({ email: values.email, password: values.password, }) .then((data) => { if (data.is2FAEnabled) { setTemp(data); } else { toast.success("Signin succesfully", { duration: 2000, }); router.push("/dashboard/projects"); } }) .catch(() => { toast.error("Signin failed", { duration: 2000, }); }); }; return (
Dokploy Sign in Enter your credentials to access your account
{!temp.is2FAEnabled ? (
( Email )} /> ( Password )} />
) : ( )} {!hasAdmin && (
Dont have an account? Sign up
)}
Need help? Contact us
Lost your password?
); } Home.getLayout = (page: ReactElement) => { return {page}; }; export async function getServerSideProps(context: GetServerSidePropsContext) { const hasAdmin = await isAdminPresent(); if (!hasAdmin) { return { redirect: { permanent: true, destination: "/register", }, }; } const { user } = await validateRequest(context.req, context.res); if (user) { return { redirect: { permanent: true, destination: "/dashboard/projects", }, }; } return { props: { hasAdmin, }, }; }