ankur_auth_rbac v1.0.3
Ankur Auth RBAC
Custom authentication and RBAC (Role-Based Access Control) for Node.js Express.
Installation
To install the package, run:
npm install ankur_auth_rbacUsage
Create a New File for Your Server:
Create a new file in your project (e.g.,
server.js) and add the following code to set up and start the server using theankur-custom-authpackage:require("dotenv").config(); const { createServer } = require("ankur_auth_rbac"); const app = createServer(); const PORT = process.env.PORT || 5000; app.listen(PORT, () => console.log(`Server running on port ${PORT}`));Configure Environment Variables:
Ensure you have a
.envfile in your project root with the necessary configuration. Here’s an example.envfile:PORT=5000 MONGO_URI=your_mongodb_uri JWT_SECRET=yoursecretReplace
your_mongodb_uriwith your actual MongoDB connection URI.
API Endpoints
User Routes
Get All Users:
- URL:
/user/ - Method:
GET - Description: Retrieves a list of all users.
- URL:
Get User Details by Username:
- URL:
/user/:username - Method:
GET - Description: Retrieves details of a user based on the username.
- URL:
Get User Details by ID:
- URL:
/user/id/:id - Method:
GET - Description: Retrieves details of a user based on the user ID.
- URL:
Search Users:
- URL:
/user/search - Method:
GET - Description: Searches for users based on query parameters.
- Example:
/user/search?query=Johnto search for users with the name "John".
- URL:
Update Username:
- URL:
/user/update-username/:id - Method:
PUT - Description: Updates the username of a user based on the user ID.
- URL:
Change User Password:
- URL:
/user/change-password/:id - Method:
PUT - Description: Changes the password of a user based on the user ID.
- URL:
Modify User Role:
- URL:
/user/modify-role/:id - Method:
PUT - Description: Modifies the role of a user based on the user ID.
- URL:
Delete User:
- URL:
/user/delete/:id - Method:
DELETE - Description: Deletes a user based on the user ID.
- URL:
Authentication Routes
Login:
- URL:
/auth/login - Method:
POST - Description: Authenticates a user and returns a token.
- URL:
Register:
- URL:
/auth/register - Method:
POST - Description: Registers a new user.
- URL:
Refresh Token:
- URL:
/auth/refresh - Method:
POST - Description: Refreshes an authentication token.
- URL:
Features
- Custom Authentication: Provides endpoints for user authentication.
- Role-Based Access Control (RBAC): Allows for fine-grained access control based on user roles.
- CORS: Enables Cross-Origin Resource Sharing for your APIs.
- MongoDB Integration: Connects to MongoDB for data storage.
Configuration
- Environment Variables:
PORT: Port number for the server to listen on.MONGODB_URI: MongoDB connection URI.JWT_SECRET: JWT secret key.
Testing
You can test the server by creating a new project and running:
node server.jsEnsure that the environment variables are correctly set in the .env file.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
If you’d like to contribute to this project, please fork the repository and submit a pull request. For major changes, please open an issue first to discuss what you’d like to change.
Contact
For any questions or support, please reach out to Ankur Auti.