User API

Overview

The User API provides endpoints for retrieving and managing user information. These endpoints allow users to view and update their profiles, manage their preferences, and interact with other users on the BattleScript platform.

User Endpoints

GET

/api/user/profile

Description: Retrieves the profile of the currently authenticated user.

Headers:

{
  "Authorization": "Bearer {token}"
}

Response:

{
  "success": true,
  "data": {
    "id": "string",
    "username": "string",
    "email": "string",
    "bio": "string",
    "avatarUrl": "string",
    "createdAt": "string",
    "lastLogin": "string",
    "scriptCount": number,
    "followerCount": number,
    "followingCount": number
  }
}

Example:

// Get current user profile
const token = localStorage.getItem('authToken');

fetch('/api/user/profile', {
  headers: {
    'Authorization': `Bearer ${token}`
  }
})
.then(response => response.json())
.then(data => {
  console.log('User profile:', data.data);
})
.catch(error => console.error('Error:', error));
GET

/api/user/{id}

Description: Retrieves the public profile of a specific user by ID.

Path Parameters:

  • id - The ID of the user to retrieve

Response:

{
  "success": true,
  "data": {
    "id": "string",
    "username": "string",
    "bio": "string",
    "avatarUrl": "string",
    "createdAt": "string",
    "scriptCount": number,
    "followerCount": number
  }
}

Example:

// Get a specific user's profile
fetch('/api/user/123456')
  .then(response => response.json())
  .then(data => {
    console.log('User profile:', data.data);
  })
  .catch(error => console.error('Error:', error));
PUT

/api/user/profile

Description: Updates the profile of the currently authenticated user.

Headers:

{
  "Authorization": "Bearer {token}"
}

Request Body:

{
  "username": "string",
  "bio": "string",
  "avatarUrl": "string"
}

Response:

{
  "success": true,
  "message": "Profile updated successfully",
  "data": {
    "id": "string",
    "username": "string",
    "email": "string",
    "bio": "string",
    "avatarUrl": "string",
    "updatedAt": "string"
  }
}

Example:

// Update user profile
const token = localStorage.getItem('authToken');

fetch('/api/user/profile', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    username: 'newUsername',
    bio: 'BattleScript enthusiast and game developer',
    avatarUrl: 'https://example.com/avatar.jpg'
  })
})
.then(response => response.json())
.then(data => {
  console.log('Profile updated:', data.data);
})
.catch(error => console.error('Error:', error));
PUT

/api/user/password

Description: Updates the password of the currently authenticated user.

Headers:

{
  "Authorization": "Bearer {token}"
}

Request Body:

{
  "currentPassword": "string",
  "newPassword": "string"
}

Response:

{
  "success": true,
  "message": "Password updated successfully"
}

Example:

// Update password
const token = localStorage.getItem('authToken');

fetch('/api/user/password', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    currentPassword: 'oldPassword123',
    newPassword: 'newSecurePassword456'
  })
})
.then(response => response.json())
.then(data => {
  console.log('Password updated:', data.message);
})
.catch(error => console.error('Error:', error));

User Scripts Endpoints

GET

/api/user/scripts

Description: Retrieves the scripts created by the currently authenticated user.

Headers:

{
  "Authorization": "Bearer {token}"
}

Query Parameters:

  • page - Page number for pagination (default: 1)
  • limit - Number of scripts per page (default: 20)
  • sort - Sort field (e.g., "createdAt", "title")
  • order - Sort order ("asc" or "desc")

Response:

{
  "success": true,
  "data": {
    "scripts": [
      {
        "id": "string",
        "title": "string",
        "description": "string",
        "tags": ["string"],
        "isPublic": boolean,
        "createdAt": "string",
        "updatedAt": "string",
        "likes": number,
        "downloads": number
      }
    ],
    "pagination": {
      "total": number,
      "page": number,
      "limit": number,
      "pages": number
    }
  }
}

Example:

// Get current user's scripts
const token = localStorage.getItem('authToken');

fetch('/api/user/scripts?sort=createdAt&order=desc', {
  headers: {
    'Authorization': `Bearer ${token}`
  }
})
.then(response => response.json())
.then(data => {
  console.log('My scripts:', data.data.scripts);
})
.catch(error => console.error('Error:', error));
GET

/api/user/{id}/scripts

Description: Retrieves the public scripts created by a specific user.

Path Parameters:

  • id - The ID of the user whose scripts to retrieve

Query Parameters:

  • page - Page number for pagination (default: 1)
  • limit - Number of scripts per page (default: 20)
  • sort - Sort field (e.g., "createdAt", "title")
  • order - Sort order ("asc" or "desc")

Response:

{
  "success": true,
  "data": {
    "scripts": [
      {
        "id": "string",
        "title": "string",
        "description": "string",
        "tags": ["string"],
        "createdAt": "string",
        "updatedAt": "string",
        "likes": number,
        "downloads": number
      }
    ],
    "pagination": {
      "total": number,
      "page": number,
      "limit": number,
      "pages": number
    }
  }
}

Example:

// Get another user's scripts
fetch('/api/user/123456/scripts?sort=likes&order=desc')
  .then(response => response.json())
  .then(data => {
    console.log('User scripts:', data.data.scripts);
  })
  .catch(error => console.error('Error:', error));

Related Topics