- Add role-based restrictions to POST /api/auth/register endpoint - Only admins can create admin accounts if any admin already exists - First user automatically gets admin role regardless of request - Regular users can only create user accounts, not admin accounts - Unauthenticated users can only create first admin, not subsequent admins - Reorganize Bruno collection into logical subfolders (auth/, admin/, profile/) - Update documentation to reflect new registration restrictions and security rules BREAKING CHANGES: - /api/auth/register now enforces role-based creation restrictions - Bruno collection reorganized with subfolder structure
81 lines
2.1 KiB
Plaintext
81 lines
2.1 KiB
Plaintext
meta {
|
|
name: Register User
|
|
type: http
|
|
seq: 2
|
|
}
|
|
|
|
post {
|
|
url: {{base_url}}/api/auth/register
|
|
body: json
|
|
auth: inherit
|
|
}
|
|
|
|
body:json {
|
|
{
|
|
"email": "test@example.com",
|
|
"username": "testuser",
|
|
"password": "password123",
|
|
"first_name": "Test",
|
|
"last_name": "User"
|
|
}
|
|
}
|
|
|
|
script:post-response {
|
|
function onResponse(res) {
|
|
let data = res.getBody();
|
|
return bru.setEnvVar("token", data.token, { persist: true });
|
|
}
|
|
onResponse(res);
|
|
}
|
|
|
|
settings {
|
|
encodeUrl: true
|
|
timeout: 0
|
|
}
|
|
|
|
docs {
|
|
## Register User
|
|
|
|
Creates a new user account with role-based restrictions.
|
|
|
|
**Method:** POST
|
|
|
|
**Endpoint:** /api/auth/register
|
|
|
|
**Request Body:**
|
|
- `email` (string): Email address
|
|
- `username` (string): Username
|
|
- `password` (string): Password
|
|
- `first_name` (string, optional): First name
|
|
- `last_name` (string, optional): Last name
|
|
- `role` (string): User role ("user" or "admin")
|
|
|
|
**Response:**
|
|
- `token` (string): JWT token
|
|
- `user` (object): User details
|
|
- `id` (string): User ID
|
|
- `email` (string): Email
|
|
- `username` (string): Username
|
|
- `theme` (string): User theme preference
|
|
- `first_name` (string, optional): First name
|
|
- `last_name` (string, optional): Last name
|
|
- `role` (string): User role ("user" or "admin")
|
|
|
|
**Status Codes:**
|
|
- 201: Created
|
|
- 400: Invalid input data
|
|
- 403: Forbidden - role-based restrictions apply
|
|
- 409: User exists
|
|
|
|
**Role Restrictions:**
|
|
- **First User**: Automatically gets admin role regardless of request
|
|
- **Existing Admins Present**: Only authenticated admins can create new admin accounts
|
|
- **No Admins Yet**: Anyone can create first admin (auto-assigned)
|
|
- **Regular User Creation**: Anyone can create regular user accounts
|
|
- **Unauthenticated Users**: Can only create first admin, not subsequent admins
|
|
|
|
**Examples:**
|
|
- First admin creation: `{"email": "admin@example.com", "username": "admin", "password": "password123", "role": "admin"}`
|
|
- Regular user creation: `{"email": "user@example.com", "username": "user", "password": "password123", "role": "user"}`
|
|
}
|