Skip to content

Commit

Permalink
🚚 remove proxy and create custom axios instance
Browse files Browse the repository at this point in the history
  • Loading branch information
heytulsiprasad committed Sep 22, 2020
1 parent be5878b commit 98e5ef4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@
"eslint-plugin-react-hooks": "^4.1.2",
"eslint-watch": "^7.0.0",
"prettier": "^2.1.2"
},
"proxy": "http://localhost:5000"
}
}
7 changes: 3 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect, useState } from "react";
import axios from "axios";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import { connect } from "react-redux";
// import axios from "axios";
import API from "./utils/axiosInstance";

import "./App.css";
import SignupContainer from "./containers/SignupContainer";
Expand All @@ -11,16 +12,14 @@ import PublicRoute from "./containers/PublicRoute";
import Dashboard from "./containers/Dashboard";
import EditProfile from "./containers/EditProfile";
import FourOhFour from "./containers/FourOhFour";
// import store from "./redux/store";

import { setAuthState } from "./redux/actions/authActions";

const App = ({ isAuth, setAuthState }) => {
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
axios
.get("/auth/state", { withCredentials: true })
API.get("/auth/state", { withCredentials: true })
.then((res) => {
setAuthState(res.data);
setIsLoading(false);
Expand Down
8 changes: 5 additions & 3 deletions src/components/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ const Footer = ({ type }) => {
if (type === "login") {
bottomText = (
<h2>
Don't have an account yet?<Link to="/signup">Register</Link>
Don't have an account yet?
<Link to="/signup">Register</Link>
</h2>
);
} else if (type === "signup") {
bottomText = (
<h2>
Already a member?<Link to="/login">Login</Link>
Already a member?
<Link to="/login">Login</Link>
</h2>
);
}

const baseURL = "http://localhost:5000";
const baseURL = process.env.REACT_APP_BACKEND_URL;

return (
<FooterParent>
Expand Down
19 changes: 6 additions & 13 deletions src/redux/actions/authActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import API from "../../utils/axiosInstance";

import {
SET_CURRENT_USER,
Expand Down Expand Up @@ -27,8 +27,7 @@ export const registerUser = (userData, history) => (dispatch) => {
// Clear previous errors
dispatch(clearErrors());

axios
.post("/auth/signup", userData)
API.post("/auth/signup", userData)
.then((res) => {
// console.log(res);

Expand Down Expand Up @@ -67,8 +66,7 @@ export const loginUser = (userData, history) => (dispatch) => {
// Clear previous errors
dispatch(clearErrors());

axios
.post("/auth/login", userData)
API.post("/auth/login", userData)
.then((res) => {
const {
name,
Expand Down Expand Up @@ -103,8 +101,7 @@ export const loginUser = (userData, history) => (dispatch) => {

// Get user profile
export const fetchUserProfile = (callback) => (dispatch) => {
axios
.get("/profile", { withCredentials: true })
API.get("/profile", { withCredentials: true })
.then((res) => {
const {
name,
Expand Down Expand Up @@ -141,10 +138,7 @@ export const editUserProfile = (userdata, history) => (dispatch) => {
// Clear previous errors
dispatch(clearErrors());

console.log(userdata);

axios
.post("/profile/edit", userdata, { withCredentials: true })
API.post("/profile/edit", userdata, { withCredentials: true })
.then(() => history.push("/"))
.catch((err) => {
// console.dir(err);
Expand All @@ -155,8 +149,7 @@ export const editUserProfile = (userdata, history) => (dispatch) => {
// Logout user

export const logoutUser = (history) => (dispatch) => {
axios
.get("/auth/logout", { withCredentials: true })
API.get("/auth/logout", { withCredentials: true })
.then(() => {
dispatch({ type: CLEAR_CURRENT_USER });
history.push("/login");
Expand Down
7 changes: 7 additions & 0 deletions src/utils/axiosInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import axios from "axios";

const API = axios.create({
baseURL: process.env.REACT_APP_BACKEND_URL,
});

export default API;

0 comments on commit 98e5ef4

Please sign in to comment.