From ffe3cba510fd95eb179e211f8a0fa2ccaf7e2c50 Mon Sep 17 00:00:00 2001 From: Alexander Wong Date: Sun, 3 Sep 2017 12:15:37 -0600 Subject: [PATCH] Fixed auth token header, Moved dispatch out of constructors --- src/api/baseApi.js | 20 +++++++++++--------- src/components/Auth/Login.jsx | 3 +-- src/components/Auth/Register.jsx | 3 +-- src/components/Auth/Settings.jsx | 3 +-- src/components/Auth/VerifyEmail.jsx | 3 +-- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/api/baseApi.js b/src/api/baseApi.js index 4912329..c3dc872 100644 --- a/src/api/baseApi.js +++ b/src/api/baseApi.js @@ -10,43 +10,45 @@ const localStorage = global.process && process.env.NODE_ENV === "test" function headers() { const token = localStorage.getItem("userToken") || ""; - - return { + let header = { Accept: "application/json", - "Content-Type": "application/json", - Authorization: `Token ${token}` + "Content-Type": "application/json" }; + if (token) { + header["Authorization"] = `Token ${token}`; + } + return header; } const apiInstance = axios.create({ baseURL: API_ENDPOINT, - timeout: 3000, + timeout: 3000 }); export function get(url, params = {}) { return apiInstance - .get(url, {params, headers: headers()}) + .get(url, { params, headers: headers() }) .then(response => response.data) .catch(error => Promise.reject(error)); } export function post(url, data) { return apiInstance - .post(url, data, {headers: headers()}) + .post(url, data, { headers: headers() }) .then(response => response.data) .catch(error => Promise.reject(error)); } export function patch(url, data) { return apiInstance - .patch(url, data, {headers: headers()}) + .patch(url, data, { headers: headers() }) .then(response => response.data) .catch(error => Promise.reject(error)); } export function del(url) { return apiInstance - .delete(url, {headers: headers()}) + .delete(url, { headers: headers() }) .then(response => response.data) .catch(error => Promise.reject(error)); } diff --git a/src/components/Auth/Login.jsx b/src/components/Auth/Login.jsx index 793ecc1..5048364 100644 --- a/src/components/Auth/Login.jsx +++ b/src/components/Auth/Login.jsx @@ -13,8 +13,7 @@ import { sendLoginRequest } from "../../actions/auth/saga.actions"; import Error from "../Shared/Error"; class Login extends Component { - constructor(props) { - super(props); + componentWillMount() { this.props.dispatch(clearAuthRequestError()); this.props.dispatch(clearAuthRequestSuccess()); } diff --git a/src/components/Auth/Register.jsx b/src/components/Auth/Register.jsx index 7c4788e..582a790 100644 --- a/src/components/Auth/Register.jsx +++ b/src/components/Auth/Register.jsx @@ -14,8 +14,7 @@ import { sendRegisterRequest } from "../../actions/auth/saga.actions"; import Error from "../Shared/Error"; class Register extends Component { - constructor(props) { - super(props); + componentWillMount() { this.props.dispatch(clearAuthRequestError()); this.props.dispatch(clearAuthRequestSuccess()); } diff --git a/src/components/Auth/Settings.jsx b/src/components/Auth/Settings.jsx index 6d5279b..9ca9570 100644 --- a/src/components/Auth/Settings.jsx +++ b/src/components/Auth/Settings.jsx @@ -13,8 +13,7 @@ import { sendChangePasswordRequest } from "../../actions/auth/saga.actions"; import Error from "../Shared/Error"; class Settings extends Component { - constructor(props) { - super(props); + componentWillMount() { this.props.dispatch(clearAuthRequestError()); this.props.dispatch(clearAuthRequestSuccess()); } diff --git a/src/components/Auth/VerifyEmail.jsx b/src/components/Auth/VerifyEmail.jsx index c962873..2d07b95 100644 --- a/src/components/Auth/VerifyEmail.jsx +++ b/src/components/Auth/VerifyEmail.jsx @@ -12,8 +12,7 @@ import { sendEmailVerificationRequest } from "../../actions/auth/saga.actions"; import Error from "../Shared/Error"; class VerifyEmail extends Component { - constructor(props) { - super(props); + componentWillMount() { const emailKey = this.props.match.params.emailKey; this.props.dispatch(clearEmailVerificationError()); this.props.dispatch(clearEmailVerificationSuccess());