I'm testing a simple JWT authentication flow using RestClient and am using @variable file variables during the flow to store tokens from the respones. Specifically, I am trying to understand if and how the @access_token variable can be reassigned upon refreshing the token.
Context:
- Login Endpoint: User logs in, and the server responds with an access token and a refresh token, which are stored in
@access_tokenand@refresh_token, respectively. - Access Token Expiry: The access token is set to expire in 20s (for testing purposes).
- Refresh Token: The
/tokenendpoint is required to be hit with the{{refresh_token}}to get a new access token when the initial token expires.
Workflow Example:
@baseUrl1 = http://localhost:3000
@baseUrl2 = http://localhost:4000
@access_token = {{login.response.body.accessToken}}
@refresh_token = "{{login.response.body.refreshToken}}"
### LOGIN
# @name login
POST {{baseUrl2}}/login HTTP/1.1
Content-Type: application/json
{
"username": "Jim"
}
### GET POSTS USING ACCESS TOKEN
GET {{baseUrl1}}/posts HTTP/1.1
Authorization: Bearer {{access_token}}
### REFRESH ACCESS TOKEN
# @name get_refreshed_token
POST {{baseUrl2}}/token HTTP/1.1
Content-Type: application/json
{
"token": {{refresh_token}}
}
### [QUERY] REASSIGN ACCESS TOKEN?
# @access_token = {{get_refreshed_token.response.body.accessToken}}
### LOGOUT
DELETE {{baseUrl2}}/logout HTTP/1.1
Content-Type: application/json
{
"token": {{refresh_token}}
}
What I Tried:
I attempted to reassign the @access_token directly after obtaining a new access token when the original token expires, using the following line in my code:
# @access_token = {{get_refreshed_token.response.body.accessToken}}
Expectations:
I expected that when I uncommented and utilized the line above, @access_token would update with the new token received from the /token endpoint, allowing the new token to be used in future API requests without issue.
Actual Outcome:
Unexpectedly, RestClient seems to retain only the last assignment of the @access_token. Thus, when I try to access an endpoint requiring the new token (like /posts), it fails and throws an error due to using the old, expired token.
Additional Context:
- If the requests are occurring for the first time and the
/tokenendpoint hasn't been hit (meaningget_refreshed_tokenis empty), then@access_tokenbecomes undefined because there is no value to assign. - If I hit
/loginafter/token, the/logindoes not overwrite@access_tokenwith the line@refresh_token = "{{login.response.body.refreshToken}}"placed at the top of the code. RestClient seems to consistently prioritize the last@access_token.
Additional Notes:
I've reviewed RestClient documentation and forums looking for information on variable reassignment but haven't found asolution
My point of question:
In the "[QUERY] REASSIGN ACCESS TOKEN?" section of the code, you'll notice a commented line where I'm was testing to see what would happen if I the reassignment of @access_token. But when this line is uncommented, and I hit the /login endpoint, followed by /posts (with the access_token), it throws an error due to the wrong access token, that second @access_token is either empty or an old value from using /token to assign a access_token to @access_token.
Questions:
- Is it possible to reassign the
@access_tokenin RestClient after it has been initially set? - If possible, what is the correct way to achieve this without hindering subsequent API calls?
- If not, is there an alternative approach to managing refreshed access tokens within RestClient?