Skip to content

Commit

Permalink
Update login script to only login when the JWT expires
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythicaeda committed Nov 22, 2023
1 parent 7625619 commit bddc103
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions src/packages/api-playground/api-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,49 @@ export default (app: Express) => {
const initialQuery = '{ plan { id name } }';
const initialPreRequestScript =
`
// Fetch a new token from the Gateway
const res = await altair.helpers.request(
'POST',
'/auth/login', // AUTH ENDPOINT OF THE DEPLOYMENT
{
body: { "username": "<YOUR_AERIE_USERNAME>", "password": "<YOUR_AERIE_PASSWORD>"}, // CREDENTIALS TO LOG IN AS
headers: {"Content-Type": "application/json"}
});
if(res.success) {
const token = res.token;
await altair.helpers.setEnvironment("user", token);
} else {
altair.log(res);
}`;
const nowInSeconds = () => Date.now() / 1000;
const tokenExpiry = await altair.storage.get("token_exp") || 0;
if (nowInSeconds() >= Number(tokenExpiry)) {
// Fetch a new token from the Gateway
const res = await altair.helpers.request(
'POST',
'/auth/login', // AUTH ENDPOINT OF THE DEPLOYMENT
{
body: { "username": "<YOUR_AERIE_USERNAME>", "password": "<YOUR_AERIE_PASSWORD>"}, // CREDENTIALS TO LOG IN AS
headers: {"Content-Type": "application/json"}
});
if(res.success) {
const token = res.token;
await altair.storage.set("token", token);
// Set JWT expiry
const atob = await altair.importModule('atob');
const body = JSON.parse(atob(token.split('.')[1]));
await altair.storage.set("token_exp", body.exp);
} else { altair.log(res); }
}
// Set the token in the environment
const token = await altair.storage.get("token");
altair.helpers.setEnvironment('user', token);`;

const initialHeaders = {"Authorization": "Bearer {{user}}", "x-hasura-role": "viewer"};
const initialSettings = {
addQueryDepthLimit: 5,
enableExperimental: true,
"plugin.list": [
"altair-graphql-plugin-graphql-explorer"
],
"request.withCredentials": true,
"schema.reloadOnStart": true,
"script.allowedCookies": [
"user"
],
tabSize: 2,
theme: "dracula",
addQueryDepthLimit: 5,
enableExperimental: true,
"plugin.list": [
"altair-graphql-plugin-graphql-explorer"
],
"request.withCredentials": true,
"schema.reloadOnStart": true,
"script.allowedCookies": [
"user"
],
tabSize: 2,
theme: "dracula",
}

/*
'initialEnvironments',
'instanceStorageNamespace',
'initialSettings',
'initialSubscriptionsProvider',
'initialSubscriptionsPayload',
'preserveState',
'initialHttpMethod',
'initialWindows',
'disableAccount',
*/
app.use('/api-playground', altairExpress({
disableAccount:true,
endpointURL,
initialHeaders,
initialPreRequestScript,
Expand Down

0 comments on commit bddc103

Please sign in to comment.