mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor(auth): set null when the findAdmin is null
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
|||||||
FormMessage,
|
FormMessage,
|
||||||
} from "@/components/ui/form";
|
} from "@/components/ui/form";
|
||||||
import { Input } from "@/components/ui/input";
|
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 { api } from "@/utils/api";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import type { GetServerSidePropsContext } from "next";
|
import type { GetServerSidePropsContext } from "next";
|
||||||
@@ -50,16 +50,12 @@ const loginSchema = z.object({
|
|||||||
|
|
||||||
type Login = z.infer<typeof loginSchema>;
|
type Login = z.infer<typeof loginSchema>;
|
||||||
|
|
||||||
interface Props {
|
|
||||||
hasAdmin: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
type AuthResponse = {
|
type AuthResponse = {
|
||||||
is2FAEnabled: boolean;
|
is2FAEnabled: boolean;
|
||||||
authId: string;
|
authId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function Home({ hasAdmin }: Props) {
|
export default function Home() {
|
||||||
const [temp, setTemp] = useState<AuthResponse>({
|
const [temp, setTemp] = useState<AuthResponse>({
|
||||||
is2FAEnabled: false,
|
is2FAEnabled: false,
|
||||||
authId: "",
|
authId: "",
|
||||||
@@ -169,14 +165,6 @@ export default function Home({ hasAdmin }: Props) {
|
|||||||
<Login2FA authId={temp.authId} />
|
<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="flex flex-row justify-between flex-wrap">
|
||||||
<div className="mt-4 text-center text-sm flex flex-row justify-center gap-2">
|
<div className="mt-4 text-center text-sm flex flex-row justify-center gap-2">
|
||||||
<Link
|
<Link
|
||||||
@@ -209,6 +197,21 @@ Home.getLayout = (page: ReactElement) => {
|
|||||||
return <OnboardingLayout>{page}</OnboardingLayout>;
|
return <OnboardingLayout>{page}</OnboardingLayout>;
|
||||||
};
|
};
|
||||||
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
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();
|
const hasAdmin = await isAdminPresent();
|
||||||
|
|
||||||
if (!hasAdmin) {
|
if (!hasAdmin) {
|
||||||
@@ -230,6 +233,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
hasAdmin,
|
hasAdmin,
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ import {
|
|||||||
FormMessage,
|
FormMessage,
|
||||||
} from "@/components/ui/form";
|
} from "@/components/ui/form";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { IS_CLOUD, isAdminPresent } from "@dokploy/builders";
|
import { IS_CLOUD, isAdminPresent, validateRequest } from "@dokploy/builders";
|
||||||
// import { IS_CLOUD } from "@/server/constants";
|
|
||||||
import { api } from "@/utils/api";
|
import { api } from "@/utils/api";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { AlertTriangle } from "lucide-react";
|
import { AlertTriangle } from "lucide-react";
|
||||||
@@ -26,6 +25,7 @@ import { useEffect } from "react";
|
|||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import type { GetServerSidePropsContext } from "next";
|
||||||
|
|
||||||
const registerSchema = z
|
const registerSchema = z
|
||||||
.object({
|
.object({
|
||||||
@@ -70,7 +70,7 @@ interface Props {
|
|||||||
isCloud: boolean;
|
isCloud: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Register = ({ hasAdmin, isCloud }: Props) => {
|
const Register = ({ isCloud }: Props) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { mutateAsync, error, isError } = api.auth.createAdmin.useMutation();
|
const { mutateAsync, error, isError } = api.auth.createAdmin.useMutation();
|
||||||
|
|
||||||
@@ -226,8 +226,18 @@ const Register = ({ hasAdmin, isCloud }: Props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default Register;
|
export default Register;
|
||||||
export async function getServerSideProps() {
|
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||||
if (IS_CLOUD) {
|
if (IS_CLOUD) {
|
||||||
|
const { user } = await validateRequest(context.req, context.res);
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
permanent: true,
|
||||||
|
destination: "/dashboard/projects",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
isCloud: true,
|
isCloud: true,
|
||||||
@@ -246,7 +256,6 @@ export async function getServerSideProps() {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
hasAdmin,
|
|
||||||
isCloud: false,
|
isCloud: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ export const authRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const newAdmin = await createAdmin(input);
|
const newAdmin = await createAdmin(input);
|
||||||
const session = await lucia.createSession(newAdmin.id || "", {});
|
const session = await lucia.createSession(newAdmin.id || "", {});
|
||||||
ctx.res.appendHeader(
|
ctx.res.appendHeader(
|
||||||
@@ -55,12 +54,7 @@ export const authRouter = createTRPCRouter({
|
|||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
throw error;
|
||||||
throw new TRPCError({
|
|
||||||
code: "BAD_REQUEST",
|
|
||||||
message: "Error to create the main admin",
|
|
||||||
cause: error,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
createUser: publicProcedure
|
createUser: publicProcedure
|
||||||
|
|||||||
@@ -69,14 +69,20 @@ export async function validateRequest(
|
|||||||
lucia.createBlankSessionCookie().serialize(),
|
lucia.createBlankSessionCookie().serialize(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.user) {
|
if (result.user) {
|
||||||
if (result.user?.rol === "admin") {
|
try {
|
||||||
const admin = await findAdminByAuthId(result.user.id);
|
if (result.user?.rol === "admin") {
|
||||||
result.user.adminId = admin.adminId;
|
const admin = await findAdminByAuthId(result.user.id);
|
||||||
} else if (result.user?.rol === "user") {
|
result.user.adminId = admin.adminId;
|
||||||
const userResult = await findUserByAuthId(result.user.id);
|
} else if (result.user?.rol === "user") {
|
||||||
result.user.adminId = userResult.adminId;
|
const userResult = await findUserByAuthId(result.user.id);
|
||||||
|
result.user.adminId = userResult.adminId;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
user: null,
|
||||||
|
session: null,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,15 +38,16 @@ export const createAdmin = async (input: typeof apiCreateAdmin._type) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IS_CLOUD) {
|
await tx
|
||||||
await tx
|
.insert(admins)
|
||||||
.insert(admins)
|
.values({
|
||||||
.values({
|
authId: newAuth.id,
|
||||||
authId: newAuth.id,
|
...(!IS_CLOUD && {
|
||||||
serverIp: await getPublicIpWithFallback(),
|
serverIp: await getPublicIpWithFallback(),
|
||||||
})
|
}),
|
||||||
.returning();
|
serverIp: await getPublicIpWithFallback(),
|
||||||
}
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
return newAuth;
|
return newAuth;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user