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
- Move Login script to external file
- Exclude `static/` directory from esLint
  • Loading branch information
Mythicaeda committed Dec 7, 2023
1 parent de528eb commit a376a8c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
ignorePatterns: ["static/*"],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
Expand Down
18 changes: 2 additions & 16 deletions src/packages/api-playground/api-playground.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
import { altairExpress } from 'altair-express-middleware';
import type { Express } from 'express';
import { getEnv } from '../../env.js';
import { readFileSync } from 'fs';

export default (app: Express) => {
const { GQL_API_URL: endpointURL, GQL_API_WS_URL: subscriptionsEndpoint } = getEnv();
const initialQuery = '{ plan { id name } }';
const initialHeaders = { Authorization: 'Bearer {{user}}', 'x-hasura-role': 'viewer' };
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 initialPreRequestScript = readFileSync('static/api-playground/pre-request-script.js').toString();
const initialSettings = {
addQueryDepthLimit: 5,
enableExperimental: true,
Expand Down
24 changes: 24 additions & 0 deletions static/api-playground/pre-request-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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);

0 comments on commit a376a8c

Please sign in to comment.