refactor(auth): set null when the findAdmin is null

This commit is contained in:
Mauricio Siu
2024-10-04 01:01:30 -06:00
parent 172d55311e
commit ab4677ac0e
5 changed files with 55 additions and 41 deletions

View File

@@ -17,7 +17,7 @@ import {
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { validateRequest, isAdminPresent } from "@dokploy/builders";
import { validateRequest, isAdminPresent, IS_CLOUD } from "@dokploy/builders";
import { api } from "@/utils/api";
import { zodResolver } from "@hookform/resolvers/zod";
import type { GetServerSidePropsContext } from "next";
@@ -50,16 +50,12 @@ const loginSchema = z.object({
type Login = z.infer<typeof loginSchema>;
interface Props {
hasAdmin: boolean;
}
type AuthResponse = {
is2FAEnabled: boolean;
authId: string;
};
export default function Home({ hasAdmin }: Props) {
export default function Home() {
const [temp, setTemp] = useState<AuthResponse>({
is2FAEnabled: false,
authId: "",
@@ -169,14 +165,6 @@ export default function Home({ hasAdmin }: Props) {
<Login2FA authId={temp.authId} />
)}
{!hasAdmin && (
<div className="mt-4 text-center text-sm">
Dont have an account?
<Link className="underline" href="/register">
Sign up
</Link>
</div>
)}
<div className="flex flex-row justify-between flex-wrap">
<div className="mt-4 text-center text-sm flex flex-row justify-center gap-2">
<Link
@@ -209,6 +197,21 @@ Home.getLayout = (page: ReactElement) => {
return <OnboardingLayout>{page}</OnboardingLayout>;
};
export async function getServerSideProps(context: GetServerSidePropsContext) {
if (IS_CLOUD) {
const { user } = await validateRequest(context.req, context.res);
if (user) {
return {
redirect: {
permanent: true,
destination: "/dashboard/projects",
},
};
}
return {
props: {},
};
}
const hasAdmin = await isAdminPresent();
if (!hasAdmin) {
@@ -230,6 +233,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
},
};
}
return {
props: {
hasAdmin,

View File

@@ -15,8 +15,7 @@ import {
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { IS_CLOUD, isAdminPresent } from "@dokploy/builders";
// import { IS_CLOUD } from "@/server/constants";
import { IS_CLOUD, isAdminPresent, validateRequest } from "@dokploy/builders";
import { api } from "@/utils/api";
import { zodResolver } from "@hookform/resolvers/zod";
import { AlertTriangle } from "lucide-react";
@@ -26,6 +25,7 @@ import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import type { GetServerSidePropsContext } from "next";
const registerSchema = z
.object({
@@ -70,7 +70,7 @@ interface Props {
isCloud: boolean;
}
const Register = ({ hasAdmin, isCloud }: Props) => {
const Register = ({ isCloud }: Props) => {
const router = useRouter();
const { mutateAsync, error, isError } = api.auth.createAdmin.useMutation();
@@ -226,8 +226,18 @@ const Register = ({ hasAdmin, isCloud }: Props) => {
};
export default Register;
export async function getServerSideProps() {
export async function getServerSideProps(context: GetServerSidePropsContext) {
if (IS_CLOUD) {
const { user } = await validateRequest(context.req, context.res);
if (user) {
return {
redirect: {
permanent: true,
destination: "/dashboard/projects",
},
};
}
return {
props: {
isCloud: true,
@@ -246,7 +256,6 @@ export async function getServerSideProps() {
}
return {
props: {
hasAdmin,
isCloud: false,
},
};

View File

@@ -46,7 +46,6 @@ export const authRouter = createTRPCRouter({
});
}
}
const newAdmin = await createAdmin(input);
const session = await lucia.createSession(newAdmin.id || "", {});
ctx.res.appendHeader(
@@ -55,12 +54,7 @@ export const authRouter = createTRPCRouter({
);
return true;
} catch (error) {
console.log(error);
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the main admin",
cause: error,
});
throw error;
}
}),
createUser: publicProcedure

View File

@@ -69,14 +69,20 @@ export async function validateRequest(
lucia.createBlankSessionCookie().serialize(),
);
}
if (result.user) {
if (result.user?.rol === "admin") {
const admin = await findAdminByAuthId(result.user.id);
result.user.adminId = admin.adminId;
} else if (result.user?.rol === "user") {
const userResult = await findUserByAuthId(result.user.id);
result.user.adminId = userResult.adminId;
try {
if (result.user?.rol === "admin") {
const admin = await findAdminByAuthId(result.user.id);
result.user.adminId = admin.adminId;
} else if (result.user?.rol === "user") {
const userResult = await findUserByAuthId(result.user.id);
result.user.adminId = userResult.adminId;
}
} catch (error) {
return {
user: null,
session: null,
};
}
}

View File

@@ -38,15 +38,16 @@ export const createAdmin = async (input: typeof apiCreateAdmin._type) => {
});
}
if (!IS_CLOUD) {
await tx
.insert(admins)
.values({
authId: newAuth.id,
await tx
.insert(admins)
.values({
authId: newAuth.id,
...(!IS_CLOUD && {
serverIp: await getPublicIpWithFallback(),
})
.returning();
}
}),
serverIp: await getPublicIpWithFallback(),
})
.returning();
return newAuth;
});