Dynamic Role Based Access

Dynamic Role-Based Access and Permission Procedures System

  • User Authentication:

    • Users log in via a POST request to /login.

    • The server verifies credentials against the database.

    • If valid, a JWT token is generated and sent to the client as a cookie.

  • Token Verification Middleware: Custom Middleware

    • verifyUser middleware checks for the presence of a JWT token in cookies.

    • If the token is valid, the user ID is extracted and added to the request object.

    • This middleware protects routes, ensuring only authenticated users can access them.

  • Fetching User Profile:

    • On component mount, a GET request to /api/profile is made.

    • The verifyUser middleware verifies the token.

    • If successful, it returns the user ID.

  • Fetching Detailed User Info:

    • The fetchProfileInfo function is called with the user ID.

    • A GET request to /api/profileInfo/:id retrieves detailed user information from the database.

  • Role-Based Access Control:

    • The user’s role is determined by authority_adminType from the profile info.

    • If the user is a manager (authority_adminType === "ম্যানেজার"), the status state is set to false.

  • Conditional Rendering in Frontend:

    • Menu items are conditionally rendered based on the status state.

    • Authorized menu items are always visible.

    • Unauthorized menu items are only visible if status is true.

Key Components and Functions

  • Backend Routes and Logic:

    • /login: Handles user authentication and token generation.

    • verifyUser: Middleware to verify JWT tokens.

    • /api/profile: Returns user ID if token is valid.

    • /api/profileInfo/:id: Fetches detailed user information based on user ID.

  • Frontend Logic:

    • useEffect: Fetches user ID and profile information on component mount.

    • fetchProfileInfo: Fetches and processes detailed profile info to determine user role.

    • Conditional rendering: Uses status to control the visibility of menu items.

Last updated