-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updating code for JWT tokens and adding singleton to update the JWT before it expires. * adding better check if token is invalid
- Loading branch information
Showing
4 changed files
with
77 additions
and
67 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,71 @@ | ||
import defaults from 'superagent-defaults' | ||
import superRequest from 'superagent' | ||
import jwtDecode from 'jwt-decode' | ||
import moment from 'moment' | ||
|
||
import store from './store' | ||
import history from './history' | ||
import { serverURLs } from './config' | ||
|
||
// Create a defaults context | ||
|
||
let request = function() { | ||
const refreshToken = (() => { | ||
let blocking = false; | ||
|
||
const refresh = (token) => { | ||
superRequest | ||
.post(serverURLs.refresh_token) | ||
.set('Accept', 'application/json') | ||
.send({'token': token}) | ||
.then(res => { | ||
blocking = false; | ||
store.dispatch({type: 'LOGIN_USER', user: res.body}); | ||
}) | ||
.catch(err => { | ||
blocking = false; | ||
store.dispatch({type: 'LOGOUT_USER'}); | ||
history.push('/login'); | ||
}) | ||
}; | ||
|
||
return { | ||
instance: (token) => { | ||
if (!blocking) { | ||
blocking = true; | ||
refresh(token) | ||
} | ||
} | ||
}; | ||
})(); | ||
|
||
// Create a defaults context | ||
export const request = () => { | ||
let customRequest = defaults(); | ||
|
||
// Setup some defaults | ||
customRequest.set('Accept', 'application/json'); | ||
// Add the user token if the user is logged in | ||
const user = store.getState().user; | ||
if (user.id) { | ||
customRequest.set('Authorization', 'Token ' + user.token); | ||
let decodedToken = undefined; | ||
try { decodedToken = jwtDecode(user.token); } catch(err) {} | ||
|
||
// Check if the user's token is outdated. | ||
// The token expired after 14 days. | ||
// See: https://github.com/open-eats/openeats-api/blob/master/base/settings.py#L174 | ||
if (decodedToken === undefined) { | ||
// If the token is undefined. | ||
// Log the user out and direct them to the login page. | ||
store.dispatch({type: 'LOGOUT_USER'}); | ||
history.push('/login'); | ||
} else if (moment(new Date()).add(10, 'days') > moment.unix(decodedToken.exp)) { | ||
// If it is then call for a refreshed token. | ||
// If the token is to old, the request will fail and | ||
// the user will be logged-out and redirect to the login screen. | ||
refreshToken.instance(user.token); | ||
} | ||
customRequest.set('Authorization', 'JWT ' + user.token); | ||
} | ||
|
||
// Make sure every request we get is json | ||
customRequest.set('Accept', 'application/json'); | ||
|
||
return customRequest; | ||
}; | ||
|
||
module.exports.request = request; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -965,7 +965,7 @@ babel-register@^6.26.0: | |
mkdirp "^0.5.1" | ||
source-map-support "^0.4.15" | ||
|
||
babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.23.0, babel-runtime@^6.26.0: | ||
babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0: | ||
version "6.26.0" | ||
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" | ||
dependencies: | ||
|
@@ -2221,15 +2221,9 @@ fb-watchman@^2.0.0: | |
dependencies: | ||
bser "^2.0.0" | ||
|
||
fbemitter@^2.0.0: | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/fbemitter/-/fbemitter-2.1.1.tgz#523e14fdaf5248805bb02f62efc33be703f51865" | ||
dependencies: | ||
fbjs "^0.8.4" | ||
|
||
fbjs@^0.8.0, fbjs@^0.8.4, fbjs@^0.8.9: | ||
version "0.8.15" | ||
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.15.tgz#4f0695fdfcc16c37c0b07facec8cb4c4091685b9" | ||
fbjs@^0.8.16: | ||
version "0.8.16" | ||
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.16.tgz#5e67432f550dc41b572bf55847b8aca64e5337db" | ||
dependencies: | ||
core-js "^1.0.0" | ||
isomorphic-fetch "^2.1.1" | ||
|
@@ -2239,9 +2233,9 @@ fbjs@^0.8.0, fbjs@^0.8.4, fbjs@^0.8.9: | |
setimmediate "^1.0.5" | ||
ua-parser-js "^0.7.9" | ||
|
||
fbjs@^0.8.16: | ||
version "0.8.16" | ||
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.16.tgz#5e67432f550dc41b572bf55847b8aca64e5337db" | ||
fbjs@^0.8.9: | ||
version "0.8.15" | ||
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.15.tgz#4f0695fdfcc16c37c0b07facec8cb4c4091685b9" | ||
dependencies: | ||
core-js "^1.0.0" | ||
isomorphic-fetch "^2.1.1" | ||
|
@@ -2328,13 +2322,6 @@ flatten@^1.0.2: | |
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782" | ||
|
||
[email protected]: | ||
version "3.1.3" | ||
resolved "https://registry.yarnpkg.com/flux/-/flux-3.1.3.tgz#d23bed515a79a22d933ab53ab4ada19d05b2f08a" | ||
dependencies: | ||
fbemitter "^2.0.0" | ||
fbjs "^0.8.0" | ||
|
||
for-each@~0.3.2: | ||
version "0.3.2" | ||
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.2.tgz#2c40450b9348e97f281322593ba96704b9abd4d4" | ||
|
@@ -3399,24 +3386,14 @@ jsprim@^1.2.2: | |
json-schema "0.2.3" | ||
verror "1.10.0" | ||
|
||
kefir-bus@^2.2.0: | ||
version "2.2.1" | ||
resolved "https://registry.yarnpkg.com/kefir-bus/-/kefir-bus-2.2.1.tgz#481f18ba274fc5580175db5cc67ce7a8228ea250" | ||
|
||
kefir@^3.5.1: | ||
version "3.7.4" | ||
resolved "https://registry.yarnpkg.com/kefir/-/kefir-3.7.4.tgz#edc6192f686be611ac5c882426f74bf0ee287ef7" | ||
dependencies: | ||
symbol-observable "1.0.4" | ||
jwt-decode@^2.2.0: | ||
version "2.2.0" | ||
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-2.2.0.tgz#7d86bd56679f58ce6a84704a657dd392bba81a79" | ||
|
||
keycode@^2.1.2: | ||
version "2.1.9" | ||
resolved "https://registry.yarnpkg.com/keycode/-/keycode-2.1.9.tgz#964a23c54e4889405b4861a5c9f0480d45141dfa" | ||
|
||
keymirror@^0.1.1: | ||
version "0.1.1" | ||
resolved "https://registry.yarnpkg.com/keymirror/-/keymirror-0.1.1.tgz#918889ea13f8d0a42e7c557250eee713adc95c35" | ||
|
||
kind-of@^2.0.1: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-2.0.1.tgz#018ec7a4ce7e3a86cb9141be519d24c8faa981b5" | ||
|
@@ -3773,6 +3750,10 @@ [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkd | |
dependencies: | ||
minimist "0.0.8" | ||
|
||
moment@^2.21.0: | ||
version "2.21.0" | ||
resolved "https://registry.yarnpkg.com/moment/-/moment-2.21.0.tgz#2a114b51d2a6ec9e6d83cf803f838a878d8a023a" | ||
|
||
[email protected]: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" | ||
|
@@ -4785,15 +4766,6 @@ react-router@^4.2.0: | |
prop-types "^15.5.4" | ||
warning "^3.0.0" | ||
|
||
[email protected]: | ||
version "1.3.2" | ||
resolved "https://registry.yarnpkg.com/react-smooth-collapse/-/react-smooth-collapse-1.3.2.tgz#e1348593a759235af0e8508cdc95431f18f30e13" | ||
dependencies: | ||
babel-runtime "^6.23.0" | ||
kefir "^3.5.1" | ||
kefir-bus "^2.2.0" | ||
prop-types "^15.6.0" | ||
|
||
[email protected]: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/react-spinkit/-/react-spinkit-2.1.2.tgz#55c037bd73e99e4b69bf2e37c6227474e74a99f6" | ||
|
@@ -5541,7 +5513,7 @@ svgo@^0.7.0: | |
sax "~1.2.1" | ||
whet.extend "~0.9.9" | ||
|
||
symbol-observable@1.0.4, symbol-observable@^1.0.3: | ||
symbol-observable@^1.0.3: | ||
version "1.0.4" | ||
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.4.tgz#29bf615d4aa7121bdd898b22d4b3f9bc4e2aa03d" | ||
|
||
|