Usage
5.1 API Endpoints
1. User Token
Path: /user/token
Methods: POST
Middleware: refreshToken
2. User Logout
Path: /user/logout
Methods: DELETE
Middleware: logoutUser
3. User Login
Path: /user/login
Methods: POST
Middleware: login
4. Fetch User Data
Path: /fetch-data/:userId
Methods: GET
Middleware: authenticateToken, fetchUserData
Note: :userId should be replaced with the actual user ID.
5. User Signup
Path: /user/signup
Methods: POST
Middleware: signup
6. Update User Data
Path: /user/update/:userId
Methods: PUT
Middleware: authenticateToken, updateUserData
Note: :userId should be replaced with the actual user ID.
7. Delete User
Path: /delete-user/:userId
Methods: DELETE
Middleware: authenticateToken, deleteUser
Note: :userId should be replaced with the actual user ID.
5.2 Example Requests and Responses
Provide examples of requests and responses for each endpoint here.
1. User Token
Request Example:
POST /user/token
Content-Type: application/json
{
"username": "example_username",
"password": "example_password"
}
Response Example:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
2. User Logout
Request Example:
DELETE /user/logout
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Response Example:
{
"message": "Logged out successfully"
}
3. User Login
Request Example:
POST /user/login
Content-Type: application/json
{
"username": "example_username",
"password": "example_password"
}
Response Example:
{
"message": "Login successful",
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
4. Fetch User Data
Request Example:
GET /fetch-data/123456
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Response Example:
{
"user_id": 123456,
"name": "John Doe",
"email": "john@example.com"
}
5. User Signup
Request Example:
POST /user/signup
Content-Type: application/json
{
"username": "example_username",
"password": "example_password",
"email": "example@example.com"
}
Response Example:
{
"message": "Signup successful",
"user_id": 789012,
"username": "example_username",
"email": "example@example.com"
}
6. Update User Data
Request Example:
PUT /user/update/789012
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
{
"name": "Updated Name",
"email": "updated@example.com"
}
Response Example:
{
"message": "User data updated successfully"
}
7. Delete User
Request Example:
DELETE /delete-user/789012
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Response Example:
{
"message": "User deleted successfully"
}
5.3 Handling Requests
Describe how the server handles requests, including error handling, validation, and any other relevant processes.
Error Handling:
The server returns appropriate HTTP status codes and error messages for different scenarios, such as unauthorized access, invalid requests, or server errors.
Validation:
Request payloads are validated to ensure they meet the required format and constraints. For example, during signup, the server checks for valid email addresses and strong passwords.
Authorization:
Endpoints requiring authentication include middleware to verify the validity of access tokens provided in the request headers.
Data Manipulation:
Endpoints for updating or deleting user data verify the user's identity through authentication tokens and perform the necessary operations securely.
Documentation:
Clear documentation is provided for each endpoint, including usage examples, expected request and response formats, and any additional notes or considerations for developers integrating with the API.
Alternative Curl Commands
1. Fetch User Data
curl -X GET \
http://localhost:3001/fetch-data/SadRedCat \
-H 'Authorization: Bearer Token'
2. Delete User
curl -X DELETE \
http://localhost:3001/delete-user/test \
-H 'Authorization: Bearer Token'
3. User Login
curl -X POST \
http://localhost:3001/user/login \
-H 'Content-Type: application/json' \
-d '{
"username": "SadRedCat",
"password": "123456"
}'
4. User Signup
curl -X POST \
http://localhost:3001/user/signup \
-H 'Content-Type: application/json' \
-d '{
"_id": "test2",
"pass": "123456"
}'
5. Save User (Testing)
curl -X POST \
http://localhost:3001/save-user \
-H 'Content-Type: application/json' \
-d '{
"_id": "test",
"pass": "123456"
}'
6. Update User Data
curl -X PUT \
http://localhost:3001/user/update/test \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer token' \
-d '{
"password":"123456",
"Userbio": "Updated bio test",
"Userburntime": 3,
"Userstorylinks": [
"https://updated_link1test.com",
"https://updated_link2test.com"
],
"Userstorytimes": [ 200, 300]
}'
7. User Logout
curl -X DELETE \
http://localhost:3001/user/logout \
-H 'Content-Type: application/json' \
-d '{
"token": "token"
}'
8. User Token
curl -X POST \
http://localhost:3001/user/token \
-H 'Content-Type: application/json' \
-d '{
"token": "token"
}'

