refactor: update validation

This commit is contained in:
Mauricio Siu 2025-02-15 13:14:48 -06:00
parent 55abac3f2f
commit 6ec60b6bab
2 changed files with 213 additions and 211 deletions

View File

@ -8,78 +8,80 @@ import { db } from "../db";
import * as schema from "../db/schema"; import * as schema from "../db/schema";
export const auth = betterAuth({ export const auth = betterAuth({
database: drizzleAdapter(db, { database: drizzleAdapter(db, {
provider: "pg", provider: "pg",
schema: schema, schema: schema,
}), }),
emailAndPassword: { emailAndPassword: {
enabled: true, enabled: true,
password: { password: {
async hash(password) { async hash(password) {
return bcrypt.hashSync(password, 10); return bcrypt.hashSync(password, 10);
}, },
async verify({ hash, password }) { async verify({ hash, password }) {
return bcrypt.compareSync(password, hash); return bcrypt.compareSync(password, hash);
}, },
}, },
}, },
hooks: { hooks: {
after: createAuthMiddleware(async (ctx) => { after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith("/sign-up")) { if (ctx.path.startsWith("/sign-up")) {
const newSession = ctx.context.newSession; const newSession = ctx.context.newSession;
await db await db
.update(schema.users_temp) .update(schema.users_temp)
.set({ .set({
role: "admin", role: "admin",
}) })
.where(eq(schema.users_temp.id, newSession?.user?.id || "")); .where(eq(schema.users_temp.id, newSession?.user?.id || ""));
} }
}), }),
}, },
user: { user: {
modelName: "users_temp", modelName: "users_temp",
additionalFields: { additionalFields: {
role: { role: {
type: "string", type: "string",
}, },
ownerId: { ownerId: {
type: "string", type: "string",
}, },
}, },
}, },
plugins: [organization()], plugins: [organization()],
}); });
export const validateRequest = async (request: IncomingMessage) => { export const validateRequest = async (request: IncomingMessage) => {
const session = await auth.api.getSession({ const session = await auth.api.getSession({
headers: new Headers({ headers: new Headers({
cookie: request.headers.cookie || "", cookie: request.headers.cookie || "",
}), }),
}); });
if (session?.user.role === "user") { if (!session?.session || !session.user) {
const owner = await db.query.member.findFirst({ return {
where: eq(schema.member.userId, session.user.id), session: null,
with: { user: null,
organization: true, };
}, }
});
if (owner) { if (session?.user) {
session.user.ownerId = owner.organization.ownerId; if (session?.user.role === "user") {
} const owner = await db.query.member.findFirst({
} else { where: eq(schema.member.userId, session.user.id),
session.user.ownerId = session?.user.id; with: {
} organization: true,
},
});
if (!session?.session || !session.user) { if (owner) {
return { session.user.ownerId = owner.organization.ownerId;
session: null, }
user: null, } else {
}; session.user.ownerId = session?.user?.id || "";
} }
}
return session; return session;
}; };

View File

@ -1,10 +1,10 @@
import { randomBytes } from "node:crypto"; import { randomBytes } from "node:crypto";
import { db } from "@dokploy/server/db"; import { db } from "@dokploy/server/db";
import { import {
admins, admins,
type apiCreateUserInvitation, type apiCreateUserInvitation,
auth, auth,
users_temp, users_temp,
} from "@dokploy/server/db/schema"; } from "@dokploy/server/db/schema";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import * as bcrypt from "bcrypt"; import * as bcrypt from "bcrypt";
@ -13,188 +13,188 @@ import { IS_CLOUD } from "../constants";
export type Admin = typeof users_temp.$inferSelect; export type Admin = typeof users_temp.$inferSelect;
export const createInvitation = async ( export const createInvitation = async (
input: typeof apiCreateUserInvitation._type, input: typeof apiCreateUserInvitation._type,
adminId: string, adminId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const result = await tx const result = await tx
.insert(auth) .insert(auth)
.values({ .values({
email: input.email.toLowerCase(), email: input.email.toLowerCase(),
rol: "user", rol: "user",
password: bcrypt.hashSync("01231203012312", 10), password: bcrypt.hashSync("01231203012312", 10),
}) })
.returning() .returning()
.then((res) => res[0]); .then((res) => res[0]);
if (!result) { if (!result) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message: "Error creating the user", message: "Error creating the user",
}); });
} }
const expiresIn24Hours = new Date(); const expiresIn24Hours = new Date();
expiresIn24Hours.setDate(expiresIn24Hours.getDate() + 1); expiresIn24Hours.setDate(expiresIn24Hours.getDate() + 1);
const token = randomBytes(32).toString("hex"); const token = randomBytes(32).toString("hex");
await tx await tx
.insert(users) .insert(users)
.values({ .values({
adminId: adminId, adminId: adminId,
authId: result.id, authId: result.id,
token, token,
expirationDate: expiresIn24Hours.toISOString(), expirationDate: expiresIn24Hours.toISOString(),
}) })
.returning(); .returning();
}); });
}; };
export const findUserById = async (userId: string) => { export const findUserById = async (userId: string) => {
const user = await db.query.users_temp.findFirst({ const user = await db.query.users_temp.findFirst({
where: eq(users_temp.id, userId), where: eq(users_temp.id, userId),
// with: { // with: {
// account: true, // account: true,
// }, // },
}); });
if (!user) { if (!user) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
message: "User not found", message: "User not found",
}); });
} }
return user; return user;
}; };
export const updateUser = async (userId: string, userData: Partial<Admin>) => { export const updateUser = async (userId: string, userData: Partial<Admin>) => {
const user = await db const user = await db
.update(users_temp) .update(users_temp)
.set({ .set({
...userData, ...userData,
}) })
.where(eq(users_temp.id, userId)) .where(eq(users_temp.id, userId))
.returning() .returning()
.then((res) => res[0]); .then((res) => res[0]);
return user; return user;
}; };
export const updateAdminById = async ( export const updateAdminById = async (
adminId: string, adminId: string,
adminData: Partial<Admin>, adminData: Partial<Admin>
) => { ) => {
const admin = await db const admin = await db
.update(admins) .update(admins)
.set({ .set({
...adminData, ...adminData,
}) })
.where(eq(admins.adminId, adminId)) .where(eq(admins.adminId, adminId))
.returning() .returning()
.then((res) => res[0]); .then((res) => res[0]);
return admin; return admin;
}; };
export const findAdminById = async (userId: string) => { export const findAdminById = async (userId: string) => {
const admin = await db.query.admins.findFirst({ const admin = await db.query.admins.findFirst({
where: eq(admins.userId, userId), where: eq(admins.userId, userId),
}); });
return admin; return admin;
}; };
export const isAdminPresent = async () => { export const isAdminPresent = async () => {
const admin = await db.query.user.findFirst({ const admin = await db.query.users_temp.findFirst({
where: eq(user.role, "admin"), where: eq(users_temp.role, "admin"),
}); });
if (!admin) { if (!admin) {
return false; return false;
} }
return true; return true;
}; };
export const findAdminByAuthId = async (authId: string) => { export const findAdminByAuthId = async (authId: string) => {
const admin = await db.query.admins.findFirst({ const admin = await db.query.admins.findFirst({
where: eq(admins.authId, authId), where: eq(admins.authId, authId),
with: { with: {
users: true, users: true,
}, },
}); });
if (!admin) { if (!admin) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
message: "Admin not found", message: "Admin not found",
}); });
} }
return admin; return admin;
}; };
export const findAdmin = async () => { export const findAdmin = async () => {
const admin = await db.query.admins.findFirst({}); const admin = await db.query.admins.findFirst({});
if (!admin) { if (!admin) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
message: "Admin not found", message: "Admin not found",
}); });
} }
return admin; return admin;
}; };
export const getUserByToken = async (token: string) => { export const getUserByToken = async (token: string) => {
const user = await db.query.users.findFirst({ const user = await db.query.users.findFirst({
where: eq(users.token, token), where: eq(users.token, token),
with: { with: {
auth: { auth: {
columns: { columns: {
password: false, password: false,
}, },
}, },
}, },
}); });
if (!user) { if (!user) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
message: "Invitation not found", message: "Invitation not found",
}); });
} }
return { return {
...user, ...user,
isExpired: user.isRegistered, isExpired: user.isRegistered,
}; };
}; };
export const removeUserById = async (userId: string) => { export const removeUserById = async (userId: string) => {
await db await db
.delete(users_temp) .delete(users_temp)
.where(eq(users_temp.id, userId)) .where(eq(users_temp.id, userId))
.returning() .returning()
.then((res) => res[0]); .then((res) => res[0]);
}; };
export const removeAdminByAuthId = async (authId: string) => { export const removeAdminByAuthId = async (authId: string) => {
const admin = await findAdminByAuthId(authId); const admin = await findAdminByAuthId(authId);
if (!admin) return null; if (!admin) return null;
// First delete all associated users // First delete all associated users
const users = admin.users; const users = admin.users;
for (const user of users) { for (const user of users) {
await removeUserById(user.id); await removeUserById(user.id);
} }
// Then delete the auth record which will cascade delete the admin // Then delete the auth record which will cascade delete the admin
return await db return await db
.delete(auth) .delete(auth)
.where(eq(auth.id, authId)) .where(eq(auth.id, authId))
.returning() .returning()
.then((res) => res[0]); .then((res) => res[0]);
}; };
export const getDokployUrl = async () => { export const getDokployUrl = async () => {
if (IS_CLOUD) { if (IS_CLOUD) {
return "https://app.dokploy.com"; return "https://app.dokploy.com";
} }
const admin = await findAdmin(); const admin = await findAdmin();
if (admin.host) { if (admin.host) {
return `https://${admin.host}`; return `https://${admin.host}`;
} }
return `http://${admin.serverIp}:${process.env.PORT}`; return `http://${admin.serverIp}:${process.env.PORT}`;
}; };