diff --git a/src/actions/account-actions.js b/src/actions/account-actions.js index d00e062..2483774 100644 --- a/src/actions/account-actions.js +++ b/src/actions/account-actions.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import './common'; +import './jwt'; import { CREATE_SESSION, NETWORK_ERROR, @@ -12,12 +12,11 @@ import { } from './action-types'; export const createSession = (googleOauthJWT) => { + window.localStorage.setItem('auth_token', googleOauthJWT); const sessionUrl = '/session'; - const authHeaders = { Authorization: `Bearer ${googleOauthJWT}` }; const requestConfig = { method: 'post', url: sessionUrl, - headers: authHeaders, }; return (dispatch) => { diff --git a/src/actions/challenge-actions.js b/src/actions/challenge-actions.js index 0ac2663..cab485e 100644 --- a/src/actions/challenge-actions.js +++ b/src/actions/challenge-actions.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import './common'; +import './jwt'; import { FETCH_CHALLENGES, FETCH_REPOS, diff --git a/src/actions/common.js b/src/actions/common.js index 18b4232..0477950 100644 --- a/src/actions/common.js +++ b/src/actions/common.js @@ -1,5 +1,3 @@ import axios from 'axios'; -const jwt = window.localStorage.getItem('auth_token'); -axios.defaults.headers.common.Authorization = `Bearer ${jwt}`; axios.defaults.baseURL = process.env.REACT_APP_API_URL; diff --git a/src/actions/jwt.js b/src/actions/jwt.js new file mode 100644 index 0000000..e4dfad2 --- /dev/null +++ b/src/actions/jwt.js @@ -0,0 +1,12 @@ +import axios from 'axios'; +import './common'; + +axios.interceptors.request.use((config) => { + const token = window.localStorage.getItem('auth_token'); + + if (token != null) { + config.headers.Authorization = `Bearer ${token}`; // eslint-disable-line + } + + return config; +}, err => (Promise.reject(err))); diff --git a/src/actions/leaderboard-actions.js b/src/actions/leaderboard-actions.js index 5294c3b..98075f5 100644 --- a/src/actions/leaderboard-actions.js +++ b/src/actions/leaderboard-actions.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import './common'; +import './jwt'; import { FETCH_LEADERBOARD, NETWORK_ERROR,