On this page

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:

Untitled
POST /user/token
Content-Type: application/json

{
  "username": "example_username",
  "password": "example_password"
}
1
Copied!

Response Example:

Untitled
{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
1
Copied!

2. User Logout

Request Example:

Untitled

DELETE /user/logout
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
1
Copied!

Response Example:

Untitled
{
  "message": "Logged out successfully"
}
1
Copied!

3. User Login

Request Example:

Untitled
POST /user/login
Content-Type: application/json

{
  "username": "example_username",
  "password": "example_password"
}
1
Copied!

Response Example:

Untitled
{
  "message": "Login successful",
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
1
Copied!

4. Fetch User Data

Request Example:

Untitled
GET /fetch-data/123456
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
1
Copied!

Response Example:

Untitled
{
  "user_id": 123456,
  "name": "John Doe",
  "email": "john@example.com"
}
1
Copied!

5. User Signup

Request Example:

Untitled
POST /user/signup
Content-Type: application/json

{
  "username": "example_username",
  "password": "example_password",
  "email": "example@example.com"
}
1
Copied!

Response Example:

Untitled
{
  "message": "Signup successful",
  "user_id": 789012,
  "username": "example_username",
  "email": "example@example.com"
}
1
Copied!

6. Update User Data

Request Example:

Untitled
PUT /user/update/789012
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

{
  "name": "Updated Name",
  "email": "updated@example.com"
}
1
Copied!

Response Example:

Untitled
{
  "message": "User data updated successfully"
}
1
Copied!

7. Delete User

Request Example:

Untitled
DELETE /delete-user/789012
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
1
Copied!

Response Example:

Untitled
{
  "message": "User deleted successfully"
}
1
Copied!

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

Untitled
curl -X GET \
  http://localhost:3001/fetch-data/SadRedCat \
  -H 'Authorization: Bearer Token'
1
Copied!

2. Delete User

Untitled
curl -X DELETE \
  http://localhost:3001/delete-user/test \
  -H 'Authorization: Bearer Token'
1
Copied!

3. User Login

Untitled
curl -X POST \
  http://localhost:3001/user/login \
  -H 'Content-Type: application/json' \
  -d '{
    "username": "SadRedCat",
    "password": "123456"
}'
1
Copied!

4. User Signup

Untitled
curl -X POST \
  http://localhost:3001/user/signup \
  -H 'Content-Type: application/json' \
  -d '{
    "_id": "test2",
    "pass": "123456"
}'
1
Copied!

5. Save User (Testing)

Untitled
curl -X POST \
  http://localhost:3001/save-user \
  -H 'Content-Type: application/json' \
  -d '{
    "_id": "test",
    "pass": "123456"
}'
1
Copied!

6. Update User Data

Untitled
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]
}'
1
Copied!

7. User Logout

Untitled
curl -X DELETE \
  http://localhost:3001/user/logout \
  -H 'Content-Type: application/json' \
  -d '{
  "token": "token"
}'
1
Copied!

8. User Token

Untitled
curl -X POST \
  http://localhost:3001/user/token \
  -H 'Content-Type: application/json' \
  -d '{
    "token": "token"
}'
1
Copied!