test(bruno): add token handling scripts to auth endpoints
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
This commit is contained in:
@@ -2,40 +2,76 @@ info:
|
||||
name: Refresh Access Token
|
||||
type: http
|
||||
seq: 1
|
||||
|
||||
http:
|
||||
method: POST
|
||||
url: '{{base_url}}/api/auth/refresh'
|
||||
auth: inherit
|
||||
url: "{{base_url}}/api/auth/refresh"
|
||||
headers:
|
||||
- name: ""
|
||||
value: application/json
|
||||
body:
|
||||
type: json
|
||||
jsonBody: "{\n \"refresh_token\": \"{{refresh_token"
|
||||
headers:
|
||||
- key: Content-Type
|
||||
value: application/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: |-
|
||||
## Refresh Access Token
|
||||
|
||||
|
||||
Refreshes an access token using a valid refresh token. Returns a new access token with 1-hour expiration.
|
||||
|
||||
|
||||
**Method:** POST
|
||||
|
||||
|
||||
**Endpoint:** /api/auth/refresh
|
||||
|
||||
|
||||
**Authentication:** Not required (refresh token is in request body)
|
||||
|
||||
|
||||
**Request Body:**
|
||||
- `refresh_token` (string): Valid refresh token UUID
|
||||
|
||||
|
||||
**Response:**
|
||||
- `access_token` (string): New JWT access token (1 hour expiration)
|
||||
- `token_type` (string): Token type (usually "Bearer")
|
||||
- `expires_in` (number): Token lifetime in seconds (3600)
|
||||
|
||||
|
||||
**Status Codes:**
|
||||
- 200: Success - new access token generated
|
||||
- 401: Unauthorized - invalid or expired refresh token
|
||||
|
||||
|
||||
**Example Response (Success):**
|
||||
```json
|
||||
{
|
||||
|
||||
@@ -2,15 +2,49 @@ info:
|
||||
name: Register User
|
||||
type: http
|
||||
seq: 2
|
||||
|
||||
http:
|
||||
method: POST
|
||||
url: '{{base_url}}/api/auth/register'
|
||||
auth: inherit
|
||||
url: "{{base_url}}/api/auth/register"
|
||||
body:
|
||||
type: json
|
||||
jsonBody: "{\n \"email\": \"testuser@example.com\",\n \"username\": \"testuser\"\
|
||||
,\n \"password\": \"Test@Pass123!\",\n \"first_name\": \"Test\",\n \
|
||||
\ \"last_name\": \"User\""
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user