{"id":22242301,"url":"https://github.com/adwxith/role-based-access-control-rbac-backend","last_synced_at":"2026-04-14T19:31:17.254Z","repository":{"id":265110768,"uuid":"895146930","full_name":"adwxith/Role-Based-Access-Control-RBAC-Backend","owner":"adwxith","description":"🌟 Role-Based Access Control (RBAC) Backend 🌟  A powerful and secure backend application designed for managing user roles, authentication, and permissions in modern web applications. Built with **Node.js**, **Express**, and **PostgreSQL**, this backend solution provides a robust foundation for implementing role-based access control in any project.","archived":false,"fork":false,"pushed_at":"2024-11-27T18:01:53.000Z","size":2170,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T09:41:50.229Z","etag":null,"topics":["authentication","authorization","docker","expressjs","jwt-auth","nodejs","postgresql","rbac-management","rest-api","token-based-authentication"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adwxith.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-27T16:36:11.000Z","updated_at":"2024-11-27T18:01:56.000Z","dependencies_parsed_at":"2024-11-27T18:40:46.109Z","dependency_job_id":null,"html_url":"https://github.com/adwxith/Role-Based-Access-Control-RBAC-Backend","commit_stats":null,"previous_names":["adwxith/role-based-access-control-rbac-backend"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adwxith%2FRole-Based-Access-Control-RBAC-Backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adwxith%2FRole-Based-Access-Control-RBAC-Backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adwxith%2FRole-Based-Access-Control-RBAC-Backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adwxith%2FRole-Based-Access-Control-RBAC-Backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adwxith","download_url":"https://codeload.github.com/adwxith/Role-Based-Access-Control-RBAC-Backend/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245442652,"owners_count":20616050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","authorization","docker","expressjs","jwt-auth","nodejs","postgresql","rbac-management","rest-api","token-based-authentication"],"created_at":"2024-12-03T04:15:35.958Z","updated_at":"2025-10-10T15:41:29.484Z","avatar_url":"https://github.com/adwxith.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n---\n\n# Role-Based-Access-Control (RBAC) Backend\n\nThis backend application implements Role-Based Access Control (RBAC) for managing user roles and authentication. Built with Node.js and PostgreSQL, it uses JWT for authentication and Docker for containerization.\n\n---\n**⚠️ Precautions ⚠️**\n\n- **Access Tokens:** Access tokens are returned in API responses for easier testing and reference during project development. **This is not a best practice for production environments.** Always ensure tokens are handled securely and never exposed unnecessarily.\n\n- **Environment Files:** Including sensitive files like `.env` or embedding environment variables in `docker-compose.yml` is only for simplicity during development. **Avoid committing such files to version control** and use secure methods for storing environment variables in production (e.g., deployment secrets or environment management tools).\n\n---\n\n## Table of Contents\n- [Prerequisites](#prerequisites)\n- [Installation](#installation)\n- [Setup](#setup)\n  - [Environment Variables](#environment-variables)\n  - [Database Setup](#database-setup)\n  - [Roles and Permissions](#roles-and-permissions)\n- [Docker Setup](#docker-setup)\n- [API Documentation](#api-documentation)\n- [Role Management](#role-management)\n- [API Testing with Postman](#api-testing-with-postman)\n- [Best Practices](#best-practices)\n- [License](#license)\n\n---\n\n## Prerequisites\nEnsure you have the following installed:\n- [Node.js](https://nodejs.org/) (v14 or higher)\n- [Docker](https://www.docker.com/)\n- [Docker Compose](https://docs.docker.com/compose/)\n\n---\n\n## Installation\n\n1. Clone the repository:\n   ```bash\n   git clone git@github.com:adwxith/Role-Based-Access-Control-RBAC-Backend.git\n   cd Role-Based-Access-Control-RBAC-Backend\n   ```\n\n2. Install project dependencies:\n   ```bash\n   npm install\n   ```\n\n---\n\n## Setup\n\n### Environment Variables\nCreate a `.env` file in the root directory with the following:\n```plaintext\nACCESS_SECRET_KEY=your-secret-key\n```\n\n**Note:** \n- Database credentials are defined in `docker-compose.yml` and do not need to be specified here.\n- Change `ACCESS_SECRET_KEY` to a strong, unique value.\n\n---\n\n### Database Setup\n\n1. Start PostgreSQL using Docker Compose:\n   ```bash\n   docker-compose up -d\n   ```\n\n2. Find the PostgreSQL container ID:\n   ```bash\n   docker ps\n   ```\n\n3. Access the PostgreSQL container:\n   ```bash\n   docker exec -it \u003cpostgres_container_id\u003e psql -U postgres -d authdb\n   ```\n\n4. Create the `users` table:\n   ```sql\n   CREATE TABLE users (\n       id SERIAL PRIMARY KEY,\n       username VARCHAR(255) NOT NULL,\n       password VARCHAR(255) NOT NULL,\n       role VARCHAR(50) NOT NULL\n   );\n   ```\n\n---\n\n### Roles and Permissions\n\nRoles and their associated permissions are defined in `roles.js`:\n```javascript\nconst roles = {\n    admin: ['dashboard-edit', 'read', 'write', 'delete'],\n    moderator: ['read', 'write'],\n    user: ['read']\n};\n\nmodule.exports = roles;\n```\n\n**Admin Role Setup During Development:**\n- During development, you can create an admin user using the `/createUser` endpoint. Use a tool like Postman to make a `POST` request with the following body:\n   ```json\n   {\n       \"username\": \"adminUser\",\n       \"password\": \"adminPassword\",\n       \"role\": \"admin\"\n   }\n   ```\n\n**Default Role for Users:**\n- After development, the `createUser` API will automatically set the role to `user` by default. Any role beyond `user` can only be assigned by an admin through the `/admin/update-role` API.\n\n---\n\n## Docker Setup\n\n### Building and Running the Docker Containers\n\n1. Build the Docker image:\n   ```bash\n   docker build -t rbac-backend .\n   ```\n\n2. Start the application and PostgreSQL database:\n   ```bash\n   docker-compose up -d\n   ```\n\n3. Stop the containers:\n   ```bash\n   docker-compose down\n   ```\n\n---\n\n## API Documentation\n\n### 1. Login - `/login` (POST)\n**Description:** Authenticates a user and generates JWT tokens.\n\n#### Request Body:\n```json\n{\n  \"username\": \"exampleUser\",\n  \"password\": \"userPassword\"\n}\n```\n\n---\n\n### 2. Create User - `/createUser` (POST)\n**Description:** Registers a new user. The role is set to `user` by default after development, but during development, you can assign roles explicitly.\n\n#### Request Body:\n```json\n{\n  \"username\": \"newUser\",\n  \"password\": \"newUserPassword\",\n  \"role\": \"user\"  \n}\n```\n\n---\n\n### 3. Role-Based Routes\n\n- **Admin Route** - `/admin` (GET): Requires `dashboard-edit` permission.\n  - Response: List of all users.\n- **Moderator Route** - `/moderator` (GET): Requires `write` permission.\n- **User Route** - `/user` (GET): Requires `read` permission.\n- **Profile Route** - `/profile` (GET): Accessible by all authenticated users.\n\n---\n\n### 4. Logout - `/logout` (POST)\n**Description:** Logs out the user and adds the JWT token to a blacklist.\n\n#### Request Header:\n```plaintext\nAuthorization: Bearer \u003caccess_token\u003e\n```\n\n---\n\n## Role Management\n\n### Updating a User's Role - `/admin/update-role` (PUT)\n**Description:** Allows an admin to update the role of a specific user. Only users with the `admin` role can perform this action.\n\n#### Request Body:\n```json\n{\n  \"userId\": 1,\n  \"newRole\": \"moderator\"\n}\n```\n\n#### Headers:\n```plaintext\nAuthorization: Bearer \u003cadmin_access_token\u003e\n```\n\n#### Example Response:\n- **Success:**\n  ```json\n  {\n    \"message\": \"User role updated successfully\",\n    \"updatedUser\": {\n      \"id\": 1,\n      \"username\": \"exampleUser\",\n      \"role\": \"moderator\"\n    }\n  }\n  ```\n- **Failure (Not Authorized):**\n  ```json\n  {\n    \"message\": \"Access denied. Only admins can perform this action.\"\n  }\n  ```\n\n---\n\n## API Testing with Postman\n\n1. **Login**:\n   - **Endpoint:** `http://localhost:3000/login`\n   - **Method:** `POST`\n   - **Body:**\n     ```json\n     {\n       \"username\": \"exampleUser\",\n       \"password\": \"userPassword\"\n     }\n     ```\n\n2. **Create User**:\n   - **Endpoint:** `http://localhost:3000/createUser`\n   - **Method:** `POST`\n   - **Body:**\n     ```json\n     {\n       \"username\": \"newUser\",\n       \"password\": \"newUserPassword\",\n       \"role\": \"user\"  \n     }\n     ```\n\n3. **Update Role**:\n   - **Endpoint:** `http://localhost:3000/admin/update-role`\n   - **Method:** `PUT`\n   - **Body:**\n     ```json\n     {\n       \"userId\": 1,\n       \"newRole\": \"moderator\"\n     }\n     ```\n   - **Header:** \n     ```plaintext\n     Authorization: Bearer \u003cadmin_access_token\u003e\n     ```\n\n4. **Admin Route**:\n   - **Endpoint:** `http://localhost:3000/admin`\n   - **Method:** `GET`\n   - **Header:** \n     ```plaintext\n     Authorization: Bearer \u003cadmin_access_token\u003e\n     ```\n\n5. **Logout**:\n   - **Endpoint:** `http://localhost:3000/logout`\n   - **Method:** `POST`\n   - **Header:** \n     ```plaintext\n     Authorization: Bearer \u003caccess_token\u003e\n     ```\n\n---\n\n## Best Practices\n\n1. **Environment Variables**:\n   - Never upload `.env` files to version control in production.\n   - Use secure methods like deployment secrets for sensitive keys.\n\n2. **Role Management**:\n   - Restrict role updates to admins only.\n   - Regularly audit user roles and permissions.\n\n3. **ACCESS_SECRET_KEY**:\n   - Use a unique and strong key for `ACCESS_SECRET_KEY`.\n\n---\n\n## License\nThis project is licensed under the MIT License.\n\n--- \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadwxith%2Frole-based-access-control-rbac-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadwxith%2Frole-based-access-control-rbac-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadwxith%2Frole-based-access-control-rbac-backend/lists"}