refactor: update

This commit is contained in:
Mauricio Siu
2025-02-16 13:28:29 -06:00
parent 27736c7c97
commit a8d1471b16
14 changed files with 133 additions and 294 deletions

View File

@@ -1,41 +0,0 @@
import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";
import { TimeSpan } from "lucia";
import { Lucia } from "lucia/dist/core.js";
import type { Session, User } from "lucia/dist/core.js";
import { db } from "../db";
import { type DatabaseUser, auth, session } from "../db/schema";
export const adapter = new DrizzlePostgreSQLAdapter(db, session, auth);
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: false,
},
},
sessionExpiresIn: new TimeSpan(1, "d"),
getUserAttributes: (attributes) => {
return {
email: attributes.email,
rol: attributes.rol,
secret: attributes.secret !== null,
adminId: attributes.adminId,
};
},
});
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: Omit<DatabaseUser, "id"> & {
authId: string;
adminId: string;
};
}
}
export type ReturnValidateToken = Promise<{
user: (User & { authId: string; adminId: string }) | null;
session: Session | null;
}>;

View File

@@ -1,99 +0,0 @@
import type { IncomingMessage } from "node:http";
import { TimeSpan } from "lucia";
import { Lucia } from "lucia/dist/core.js";
import { findAdminByAuthId } from "../services/admin";
import { findUserByAuthId } from "../services/user";
import { type ReturnValidateToken, adapter } from "./auth";
export const luciaToken = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: false,
},
},
sessionExpiresIn: new TimeSpan(365, "d"),
getUserAttributes: (attributes) => {
return {
email: attributes.email,
rol: attributes.rol,
secret: attributes.secret !== null,
};
},
});
// export const validateBearerToken = async (
// req: IncomingMessage,
// ): ReturnValidateToken => {
// const authorizationHeader = req.headers.authorization;
// const sessionId = luciaToken.readBearerToken(authorizationHeader ?? "");
// if (!sessionId) {
// return {
// user: null,
// session: null,
// };
// }
// const result = await luciaToken.validateSession(sessionId);
// if (result.user) {
// if (result.user?.rol === "owner") {
// const admin = await findAdminByAuthId(result.user.id);
// result.user.adminId = admin.adminId;
// } else if (result.user?.rol === "member") {
// const userResult = await findUserByAuthId(result.user.id);
// result.user.adminId = userResult.adminId;
// }
// }
// return {
// session: result.session,
// ...((result.user && {
// user: {
// adminId: result.user.adminId,
// authId: result.user.id,
// email: result.user.email,
// rol: result.user.rol,
// id: result.user.id,
// secret: result.user.secret,
// },
// }) || {
// user: null,
// }),
// };
// };
// export const validateBearerTokenAPI = async (
// authorizationHeader: string,
// ): ReturnValidateToken => {
// const sessionId = luciaToken.readBearerToken(authorizationHeader ?? "");
// if (!sessionId) {
// return {
// user: null,
// session: null,
// };
// }
// const result = await luciaToken.validateSession(sessionId);
// if (result.user) {
// if (result.user?.rol === "owner") {
// const admin = await findAdminByAuthId(result.user.id);
// result.user.adminId = admin.adminId;
// } else if (result.user?.rol === "member") {
// const userResult = await findUserByAuthId(result.user.id);
// result.user.adminId = userResult.adminId;
// }
// }
// return {
// session: result.session,
// ...((result.user && {
// user: {
// adminId: result.user.adminId,
// authId: result.user.id,
// email: result.user.email,
// rol: result.user.rol,
// id: result.user.id,
// secret: result.user.secret,
// },
// }) || {
// user: null,
// }),
// };
// };

View File

@@ -1,5 +1,3 @@
export * from "./auth/auth";
export * from "./auth/token";
export * from "./auth/random-password";
// export * from "./db";
export * from "./services/admin";

View File

@@ -143,27 +143,26 @@ export const findAdmin = async () => {
};
export const getUserByToken = async (token: string) => {
const user = await db.query.users.findFirst({
where: eq(users.token, token),
with: {
auth: {
columns: {
password: false,
},
},
},
});
if (!user) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Invitation not found",
});
}
return {
...user,
isExpired: user.isRegistered,
};
// const user = await db.query.users.findFirst({
// where: eq(users.token, token),
// with: {
// auth: {
// columns: {
// password: false,
// },
// },
// },
// });
// if (!user) {
// throw new TRPCError({
// code: "NOT_FOUND",
// message: "Invitation not found",
// });
// }
// return {
// ...user,
// isExpired: user.isRegistered,
// };
};
export const removeUserById = async (userId: string) => {
@@ -181,9 +180,9 @@ export const removeAdminByAuthId = async (authId: string) => {
// First delete all associated users
const users = admin.users;
for (const user of users) {
await removeUserById(user.id);
}
// for (const user of users) {
// await removeUserById(user.id);
// }
// Then delete the auth record which will cascade delete the admin
return await db
.delete(auth)

View File

@@ -33,20 +33,6 @@ export const findUserByAuthId = async (authId: string) => {
// return userR;
};
export const findUsers = async (adminId: string) => {
const currentUsers = await db.query.user.findMany({
where: eq(user.adminId, adminId),
with: {
auth: {
columns: {
secret: false,
},
},
},
});
return currentUsers;
};
export const addNewProject = async (userId: string, projectId: string) => {
const userR = await findUserById(userId);

View File

@@ -1,5 +1,4 @@
import { IS_CLOUD, paths } from "@dokploy/server/constants";
import { updateAdmin } from "@dokploy/server/services/admin";
import { type RotatingFileStream, createStream } from "rotating-file-stream";
import { db } from "../../db";
import { execAsync } from "../process/execAsync";
@@ -23,27 +22,27 @@ class LogRotationManager {
}
private async initialize(): Promise<void> {
// const isActive = await this.getStateFromDB();
// if (isActive) {
// await this.activateStream();
// }
const isActive = await this.getStateFromDB();
if (isActive) {
await this.activateStream();
}
}
// private async getStateFromDB(): Promise<boolean> {
// const setting = await db.query.admins.findFirst({});
// return setting?.enableLogRotation ?? false;
// }
private async getStateFromDB(): Promise<boolean> {
const setting = await db.query.admins.findFirst({});
return setting?.enableLogRotation ?? false;
}
// private async setStateInDB(active: boolean): Promise<void> {
// const admin = await db.query.admins.findFirst({});
private async setStateInDB(active: boolean): Promise<void> {
const admin = await db.query.admins.findFirst({});
// if (!admin) {
// return;
// }
// await updateAdmin(admin?.authId, {
// enableLogRotation: active,
// });
// }
if (!admin) {
return;
}
// await updateAdmin(admin?.authId, {
// enableLogRotation: active,
// });
}
private async activateStream(): Promise<void> {
const { DYNAMIC_TRAEFIK_PATH } = paths();

View File

@@ -49,7 +49,7 @@ export const runMariadbBackup = async (
projectName: project.name,
databaseType: "mariadb",
type: "success",
userId: project.userId,
organizationId: project.organizationId,
});
} catch (error) {
console.log(error);
@@ -60,7 +60,7 @@ export const runMariadbBackup = async (
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
userId: project.userId,
organizationId: project.organizationId,
});
throw error;
}

View File

@@ -46,7 +46,7 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {
projectName: project.name,
databaseType: "mongodb",
type: "success",
userId: project.userId,
organizationId: project.organizationId,
});
} catch (error) {
console.log(error);
@@ -57,7 +57,7 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
userId: project.userId,
organizationId: project.organizationId,
});
throw error;
}

View File

@@ -46,7 +46,7 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
projectName: project.name,
databaseType: "mysql",
type: "success",
userId: project.userId,
organizationId: project.organizationId,
});
} catch (error) {
console.log(error);
@@ -57,7 +57,7 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
userId: project.userId,
organizationId: project.organizationId,
});
throw error;
}

View File

@@ -49,7 +49,7 @@ export const runPostgresBackup = async (
projectName: project.name,
databaseType: "postgres",
type: "success",
userId: project.userId,
organizationId: project.organizationId,
});
} catch (error) {
await sendDatabaseBackupNotifications({
@@ -59,7 +59,7 @@ export const runPostgresBackup = async (
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
userId: project.userId,
organizationId: project.organizationId,
});
throw error;

View File

@@ -19,13 +19,13 @@ export const sendDatabaseBackupNotifications = async ({
databaseType,
type,
errorMessage,
userId,
organizationId,
}: {
projectName: string;
applicationName: string;
databaseType: "postgres" | "mysql" | "mongodb" | "mariadb";
type: "error" | "success";
userId: string;
organizationId: string;
errorMessage?: string;
}) => {
const date = new Date();
@@ -33,7 +33,7 @@ export const sendDatabaseBackupNotifications = async ({
const notificationList = await db.query.notifications.findMany({
where: and(
eq(notifications.databaseBackup, true),
eq(notifications.userId, userId),
eq(notifications.organizationId, organizationId),
),
with: {
email: true,