Skip to content
This repository has been archived by the owner on Apr 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #489 from common-group/feature/renew-api-token
Browse files Browse the repository at this point in the history
✨ verificar expiração do token jwt
  • Loading branch information
thiagocatarse authored Sep 17, 2020
2 parents 22944ba + 7f962d3 commit 8944ad7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 65 deletions.
60 changes: 0 additions & 60 deletions legacy/src/api.js

This file was deleted.

73 changes: 73 additions & 0 deletions legacy/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import m from 'mithril'
import Postgrest from 'mithril-postgrest'
import { default as jwt_decode } from 'jwt-decode'

const platformTokenMeta = document.querySelector('[name="common-platform-token"]')
const platformToken = platformTokenMeta ? platformTokenMeta.getAttribute('content') : null
const commonRequestHeader = { 'Platform-Code' : platformToken }

const apiInit = (api, apiMeta, authUrl, globalHeader = {}) => {
api.init(apiMeta.getAttribute('content'), { method: 'GET', url: authUrl }, globalHeader, isTokenExpired)
}

const catarse = new Postgrest(m)
const catarseApiMeta = document.querySelector('[name="api-host"]')
apiInit(catarse, catarseApiMeta, '/api_token')

const catarseMoments = new Postgrest(m)
const catarseApiMomentsMeta = document.querySelector('[name="api-moments-host"]')
apiInit(catarseMoments, catarseApiMomentsMeta, '/api_token')

const commonPayment = new Postgrest(m)
const commonPaymentApiMeta = document.querySelector('[name="common-payment-api-host"]')
apiInit(commonPayment, commonPaymentApiMeta, '/api_token/common', commonRequestHeader)

const commonProject = new Postgrest(m)
const commonProjectApiMeta = document.querySelector('[name="common-project-api-host"]')
apiInit(commonProject, commonProjectApiMeta, '/api_token/common', commonRequestHeader)

const commonAnalytics = new Postgrest(m)
const commonAnalyticsApiMeta = document.querySelector('[name="common-analytics-api-host"]')
apiInit(commonAnalytics, commonAnalyticsApiMeta, '/api_token/common', commonRequestHeader)

const commonNotification = new Postgrest(m)
const commonNotificationApiMeta = document.querySelector('[name="common-notification-api-host"]')
apiInit(commonNotification, commonNotificationApiMeta, '/api_token/common', commonRequestHeader)

// not a postgrest instance, but pretend it is to get free pagination
const commonRecommender = new Postgrest(m)
const commonRecommenderApiMeta = document.querySelector('[name="common-recommender-api-host"]')
apiInit(commonRecommender, commonRecommenderApiMeta, '/api_token/common', commonRequestHeader)

const commonCommunity = new Postgrest(m)
const commonCommunityApiMeta = document.querySelector('[name="common-community-api-host"]')
apiInit(commonCommunity, commonCommunityApiMeta, '/api_token/common', commonRequestHeader)

const commonProxy = new Postgrest(m)
const commonProxyApiMeta = document.querySelector('[name="common-proxy-api-host"]')
apiInit(commonProxy, commonProxyApiMeta, '/api_token/common_proxy', commonRequestHeader)

async function isTokenExpired(token : string) : Promise<boolean> {
if (token) {
try {
const decoded = jwt_decode(token)
const expirationTime = Number(decoded.exp) * 1000
return Date.now() >= expirationTime
} catch(error) {
return false
}
}
return false
}

export {
catarse,
catarseMoments,
commonPayment,
commonProject,
commonAnalytics,
commonNotification,
commonRecommender,
commonCommunity,
commonProxy
}
4 changes: 0 additions & 4 deletions legacy/src/c.js

This file was deleted.

4 changes: 4 additions & 0 deletions legacy/src/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as root from './modules'
import 'babel-polyfill'

export default { root }
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/underscore": "^1.9.4",
"babel-eslint": "^7.1.1",
"babel-loader": "^8.0.6",
"babel-polyfill": "^6.26.0",
"css-loader": "^3.4.2",
"eslint": ">=4.18.2",
"eslint-config-airbnb-base": "^11.3.2",
Expand All @@ -49,6 +50,7 @@
"jasmine-matchers": "^0.2.3",
"jasmine-species": "^0.8.5",
"jshint-stylish": "^2.2.1",
"jwt-decode": "^3.0.0-beta.2",
"karma": "^0.13.22",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^0.3.8",
Expand All @@ -74,7 +76,6 @@
"build:dev": "./node_modules/.bin/webpack-dev-server"
},
"dependencies": {
"@babel/polyfill": "^7.8.3",
"@babel/runtime": "^7.8.3",
"@sentry/browser": "^5.7.1",
"chart.js": "1.0.2",
Expand Down

0 comments on commit 8944ad7

Please sign in to comment.