diff --git a/apps/backend/src/middlewares/authInfo.ts b/apps/backend/src/middlewares/authInfo.ts index a2037fe..10514c2 100644 --- a/apps/backend/src/middlewares/authInfo.ts +++ b/apps/backend/src/middlewares/authInfo.ts @@ -11,9 +11,20 @@ import { permissionsSchema } from "../drizzle/schema/permissions"; import { RoleCode } from "../data/roles"; import { SpecificPermissionCode } from "../data/permissions"; +/** + * Middleware to load and set authentication information for a user. + * + * It queries the database to fetch user details, roles, and permissions based on the user ID provided in the context. + * It joins several tables to efficiently gather all relevant data, and then processes this data to attach the user's roles and permissions to the context. + * If the user is found and active, their details are added to the context for use in subsequent middleware or routes. + * + * @param c - The context provided by Hono, which includes environment and user-specific data. + * @param next - The next middleware function in the chain. + */ const authInfo = createMiddleware(async (c, next) => { const { uid, currentUser } = c.var; + // Proceed only if uid is present and currentUser is not already set if (uid && !currentUser) { const user = await db .select() @@ -58,8 +69,9 @@ const authInfo = createMiddleware(async (c, next) => { } }); + // Setting the currentUser with fetched data c.set("currentUser", { - name: user[0].users.name, + name: user[0].users.name, // Assuming the first result is the user permissions: Array.from(permissions), roles: Array.from(roles), });