Skip to content

Commit

Permalink
Fix authentication woes
Browse files Browse the repository at this point in the history
The common.js file was being loaded and setting the default headers to
use a Bearer token of null for all future requests (if a token did not
exist in localStorage).
  • Loading branch information
binarymason committed May 15, 2017
1 parent 014ffef commit 4d25d4e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/actions/account-actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import './common';
import './jwt';
import {
CREATE_SESSION,
NETWORK_ERROR,
Expand All @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/challenge-actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import './common';
import './jwt';
import {
FETCH_CHALLENGES,
FETCH_REPOS,
Expand Down
2 changes: 0 additions & 2 deletions src/actions/common.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions src/actions/jwt.js
Original file line number Diff line number Diff line change
@@ -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)));
2 changes: 1 addition & 1 deletion src/actions/leaderboard-actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import './common';
import './jwt';
import {
FETCH_LEADERBOARD,
NETWORK_ERROR,
Expand Down

0 comments on commit 4d25d4e

Please sign in to comment.