feat: added refresh access token and logout #38
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧪 RBAC Project Testing Guide
This guide will help you test the complete functionality of the RBAC project, including the new refresh token mechanism.
📋 Prerequisites
Install Dependencies
Set up Environment Variables
Create a
.envfile in the root directory:Start MongoDB
Make sure MongoDB is running on your system.
🚀 Step-by-Step Testing
Step 1: Start the Server
You should see:
Server is running at port : 5000Step 2: Seed the Database
You should see:
Step 3: Test the API Endpoints
3.1 Test Server Health
Expected response:
RBAC is running...3.2 Test User Registration
Expected response:
{ "success": true, "message": "User registered successfully", "user": { "id": "user_id", "username": "testuser", "email": "[email protected]", "role": "User" } }3.3 Test User Login (with Refresh Token)
Expected response:
{ "success": true, "message": "Login successful", "accessToken": "jwt_token_here", "refreshToken": "refresh_token_here", "user": { "id": "user_id", "username": "testuser", "email": "[email protected]", "fullname": "Test User", "role": "User" } }Save the tokens for next steps!
3.4 Test Protected Endpoint
curl -X GET http://localhost:5000/api/rbac-test/user-only \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"Expected response:
{ "message": "Welcome, User" }3.5 Test Token Refresh
Expected response:
{ "success": true, "message": "Token refreshed successfully", "accessToken": "new_jwt_token_here", "user": { "id": "user_id", "username": "testuser", "email": "[email protected]", "fullname": "Test User", "role": "User" } }3.6 Test Logout
Expected response:
{ "success": true, "message": "Logged out successfully" }3.7 Test Refresh After Logout (Should Fail)
Expected response:
{ "success": false, "message": "Invalid refresh token" }🔧 Using Postman/Insomnia
Collection Setup
http://localhost:5000/apiRequest Examples
1. Register User
{{base_url}}/auth/register{ "username": "testuser", "email": "[email protected]", "fullname": "Test User", "password": "password123" }2. Login User
{{base_url}}/auth/login{ "email": "[email protected]", "password": "password123" }3. Test Protected Route
{{base_url}}/rbac-test/user-onlyAuthorization:Bearer {{accessToken}}4. Refresh Token
{{base_url}}/auth/refresh{ "refreshToken": "{{refreshToken}}" }5. Logout
{{base_url}}/auth/logout{ "refreshToken": "{{refreshToken}}" }🧪 Automated Testing Script
Create a test script to verify all functionality:
🐛 Troubleshooting
Common Issues:
MongoDB Connection Error
JWT Secret Error
Port Already in Use
lsof -ti:5000 | xargs kill -9Token Validation Errors
Debug Mode:
Add this to your .env file for detailed logging:
📊 Expected Results
✅ All endpoints should return proper responses
✅ Authentication flow should work seamlessly
✅ Refresh token mechanism should function correctly
✅ Logout should invalidate tokens
✅ Protected routes should require valid tokens
✅ Role-based access should work as expected
🎯 Success Criteria
Happy Testing! 🚀