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