Created
April 21, 2021 10:27
-
-
Save DipandaAser/3bf372414d045b3d7a1828013212af0d to your computer and use it in GitHub Desktop.
Automate login and re-login to refresh token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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