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,
// }),
// };
// };