Added docs

This commit is contained in:
sianida26 2024-05-12 21:27:22 +07:00
parent ffcf2d0992
commit e4c5ddd04c

View File

@ -11,9 +11,20 @@ import { permissionsSchema } from "../drizzle/schema/permissions";
import { RoleCode } from "../data/roles"; import { RoleCode } from "../data/roles";
import { SpecificPermissionCode } from "../data/permissions"; 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<HonoEnv>(async (c, next) => { const authInfo = createMiddleware<HonoEnv>(async (c, next) => {
const { uid, currentUser } = c.var; const { uid, currentUser } = c.var;
// Proceed only if uid is present and currentUser is not already set
if (uid && !currentUser) { if (uid && !currentUser) {
const user = await db const user = await db
.select() .select()
@ -58,8 +69,9 @@ const authInfo = createMiddleware<HonoEnv>(async (c, next) => {
} }
}); });
// Setting the currentUser with fetched data
c.set("currentUser", { c.set("currentUser", {
name: user[0].users.name, name: user[0].users.name, // Assuming the first result is the user
permissions: Array.from(permissions), permissions: Array.from(permissions),
roles: Array.from(roles), roles: Array.from(roles),
}); });