Add after-response scripts to automatically save access and refresh tokens to Bruno environment variables after successful authentication. This eliminates manual token copying during testing. Changes: - Refresh Token.yml: Add script to save tokens from refresh response - Register User.yml: Add script to save tokens from registration response
92 lines
2.7 KiB
YAML
92 lines
2.7 KiB
YAML
info:
|
|
name: Register User
|
|
type: http
|
|
seq: 2
|
|
|
|
http:
|
|
method: POST
|
|
url: "{{base_url}}/api/auth/register"
|
|
body:
|
|
type: json
|
|
data: ""
|
|
auth: inherit
|
|
|
|
runtime:
|
|
scripts:
|
|
- type: after-response
|
|
code: |-
|
|
function onResponse(res) {
|
|
try {
|
|
const responseBody = res.getBody();
|
|
const token = responseBody.access_token;
|
|
const refreshToken = responseBody.refresh_token;
|
|
|
|
if (token) {
|
|
bru.setEnvVar("token", token, { persist: true });
|
|
console.log("Access token saved:", token);
|
|
}
|
|
|
|
if (refreshToken) {
|
|
bru.setEnvVar("refresh_token", refreshToken, { persist: true });
|
|
console.log("Refresh token saved:", refreshToken);
|
|
}
|
|
|
|
if (!token && !refreshToken) {
|
|
console.log("No tokens found in response.");
|
|
}
|
|
} catch (error) {
|
|
console.error("Error in post-response script:", error.message);
|
|
}
|
|
}
|
|
onResponse(res);
|
|
|
|
settings:
|
|
encodeUrl: true
|
|
timeout: 0
|
|
followRedirects: true
|
|
maxRedirects: 5
|
|
|
|
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"
|