Skip to content

Instantly share code, notes, and snippets.

@DipandaAser
Created April 21, 2021 10:27
Show Gist options
  • Save DipandaAser/3bf372414d045b3d7a1828013212af0d to your computer and use it in GitHub Desktop.
Save DipandaAser/3bf372414d045b3d7a1828013212af0d to your computer and use it in GitHub Desktop.
Automate login and re-login to refresh token
var moment = require('moment');
var tokenExpirationDate = pm.collectionVariables.get("token-expiration-date");
var currentTime = moment().format();
if ( (tokenExpirationDate === "") || (currentTime >= tokenExpirationDate) ){
pm.sendRequest({
url: pm.collectionVariables.get("local-url") + '/login',
method: 'POST',
header: {
'content-type': 'application/json',
},
body: {
mode: 'raw',
raw: JSON.stringify({ email: pm.collectionVariables.get("admin-email"), password: pm.collectionVariables.get("admin-password") })
}
}, function (err, res) {
if (res.code == 200) {
pm.collectionVariables.set("token-expiration-date", moment().add(pm.collectionVariables.get("token-timeout"), 'm').format());
pm.collectionVariables.set("user-token", res.json().Token);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment