You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've searched existing issues and found nothing related to my issue.
Describe the bug
Running into an issue where the pre-request at the Collection level is not running before the intended request within the collection. Trying to setup a bearer token and use it across all requests within the collection. The pre-script works however the header bearer token is always null because the request is completed prior to the collection pre-request running.
.bru file to reproduce the bug
// Define variablesconstaxios=require('axios');constaccessToken=bru.getEnvVar("ACCESS_TOKEN");constrefreshToken=bru.getEnvVar("REFRESH_TOKEN");constproduct_access_id=bru.getEnvVar("PRODUCT_ACCESS_ID");constbase_url=bru.getEnvVar("baseUrl");constusername=bru.getEnvVar('USERNAME');constpassword=bru.getEnvVar('PASSWORD');consturlAuthentication=`${base_url}/session/auth/password`;consturlUserInfo=`${base_url}/session/userinfo`;asyncfunctionmyPasswordAuthentication(){try{constresponse=awaitaxios.post(urlAuthentication,{userName: username,password: password,},{headers: {"Content-Type": "application/json",},});constresponseData=response.data;if(responseData.auth_token&&responseData.token_type){bru.setEnvVar("AUTH_TOKEN",responseData.auth_token);bru.setEnvVar("TOKEN_TYPE",responseData.token_type);return"Authentication successful.";}else{thrownewError("Password Authentication failed: Missing auth token or token type in response.");}}catch(error){console.error("Password Authentication failed:",error);throwerror;}}asyncfunctionmyGetUserInfo(){try{constresponse=awaitaxios.get(urlUserInfo,{headers: {"Content-Type": "application/json","Authorization": `Bearer ${bru.getEnvVar("AUTH_TOKEN")}`,},});constresponseData=response.data;//console.log('myGetUserInfo --> response', responseData);constappId=responseData.apps.find(app=>app.appId==="economy"&&app.access.length>0);//console.log('-------> economyApp', economyApp);if(appId){bru.setEnvVar("ACCOUNT_ID",appId.access[0].accountId);bru.setEnvVar("PRODUCT_ACCESS_ID",appId.access[0].entries[0].productAccessId);bru.setEnvVar("USER_ID",responseData.userId);return"Get user information successful.";}else{thrownewError("App not found or user has no access to it.");}}catch(error){console.error("Get User Information failed:",error);throwerror;}}asyncfunctionmyRefreshToken(){try{consturlRefreshToken=`${base_url}/session/token?product_access_id=${bru.getEnvVar("PRODUCT_ACCESS_ID")}`;//console.log('----> urlRefreshToken', urlRefreshToken)constresponse=awaitaxios.get(urlRefreshToken,{headers: {"Content-Type": "application/json","Authorization": `Bearer ${bru.getEnvVar("AUTH_TOKEN")}`,},});constresponseData=response.data;// console.log("myRefreshToken() --> response ", responseData);if(responseData.refresh_token&&responseData.access_token){bru.setEnvVar("REFRESH_TOKEN",responseData.refresh_token);bru.setEnvVar("ACCESS_TOKEN",responseData.access_token);bru.setVar('token',responseData.access_token)consttoken=bru.getVar('token')console.log('--->',bru.getVar('token'))req.setHeader('Authentication',`Bearer ${token}`);// req.setHeader("Authorization", `Bearer ${token}`);return"Token refreshed successfully";}console.error("Failed to refresh token no access_token or refresh_token:",error);throwerror;}catch(error){console.error("Failed to refresh token:",error);throwerror;}}asyncfunctionprocessAuthentication(){if(!username||!password){console.error("{USERNAME} and {PASSWORD} environment variables are required.")thrownewError("{USERNAME} and {PASSWORD} environment variables are required.");}// console.log('processAuthentication() --> accessToken', accessToken);if(accessToken){try{awaitmyRefreshToken();console.log("Token refreshed successfully.");}catch(refreshError){console.warn("Token refresh failed:",refreshError);console.log("Retrying Auth, user info retrieval & token refresh");awaitauthenticate();}}else{try{awaitauthenticate();}catch(error){console.error("An error occurred during re-authentication:",error);thrownewError("Authentication process failed.");}}}// Function to handle full authentication flowasyncfunctionauthenticate(){try{awaitmyPasswordAuthentication();console.log("Password authentication completed.");awaitmyGetUserInfo();console.log("User info retrieval completed.");awaitmyRefreshToken();console.log("Token refreshed successfully after re-authentication.");}catch(error){console.error("An error occurred during the authentication flow:",error);thrownewError("Authentication process failed.");}}// Trigger the processprocessAuthentication().then(()=>{constaccessToken=bru.getEnvVar("ACCESS_TOKEN");constrefreshToken=bru.getEnvVar("REFRESH_TOKEN");constproduct_access_id=bru.getEnvVar("PRODUCT_ACCESS_ID");constbase_url=bru.getEnvVar("baseUrl");constusername=bru.getEnvVar('USERNAME');constpassword=bru.getEnvVar('PASSWORD');consttokenType=bru.getEnvVar('TOKEN_TYPE');req.setHeader("Authorization",`Bearer ${accessToken}`);console.log("Process completed successfully.");}).catch((error)=>{console.error("Final process error:",error);});
### Screenshots/Livedemolink<imgwidth="417"alt="Image"src="https://github.com/user-attachments/assets/4d5b6b04-e35a-4903-9f6b-369bfdfb3164"/>
The text was updated successfully, but these errors were encountered:
I have checked the following:
Describe the bug
Running into an issue where the pre-request at the Collection level is not running before the intended request within the collection. Trying to setup a bearer token and use it across all requests within the collection. The pre-script works however the header bearer token is always null because the request is completed prior to the collection pre-request running.
.bru file to reproduce the bug
The text was updated successfully, but these errors were encountered: