Skip to content

Commit

Permalink
🚚 fixing cors issues on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
heytulsiprasad committed Sep 23, 2020
1 parent 98e5ef4 commit 18544f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import { connect } from "react-redux";
// import axios from "axios";
import axios from "axios";
import API from "./utils/axiosInstance";

import "./App.css";
Expand All @@ -19,7 +19,8 @@ const App = ({ isAuth, setAuthState }) => {
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
API.get("/auth/state", { withCredentials: true })
axios
.get("http://localhost:5000/auth/state", { withCredentials: true })
.then((res) => {
setAuthState(res.data);
setIsLoading(false);
Expand Down
11 changes: 3 additions & 8 deletions src/components/Profile/EditInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class EditInfo extends React.Component {

componentDidUpdate(prevProps) {
if (prevProps.isLoading !== this.props.isLoading) {
const {
name, email, phone, bio, image
} = prevProps.user;
const { name, email, phone, bio, image } = prevProps.user;

this.setState((state) => ({
...state,
Expand Down Expand Up @@ -123,9 +121,7 @@ class EditInfo extends React.Component {
};

render() {
const {
name, email, phone, bio, image
} = this.state.profile;
const { name, email, phone, bio, image } = this.state.profile;
const error = this.props.errors;

return (
Expand Down Expand Up @@ -180,9 +176,8 @@ class EditInfo extends React.Component {
name="email"
type="email"
placeholder="Enter your email..."
value={email || ""}
defaultValue={email || ""}
error={error.error}
onChange={this.inputChangeHandler}
required
/>
<div style={{ marginTop: "23px" }}>
Expand Down
31 changes: 22 additions & 9 deletions src/redux/actions/authActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import API from "../../utils/axiosInstance";
import axios from "axios";

import {
SET_CURRENT_USER,
Expand All @@ -8,6 +8,11 @@ import {
SET_AUTH_STATE,
} from "./types";

const backendURL =
process.env.NODE_ENV === "production"
? process.env.REACT_APP_BACKEND_PROD_URL
: process.env.REACT_APP_BACKEND_DEV_URL;

export const setCurrentUser = (newUser) => ({
type: SET_CURRENT_USER,
payload: newUser,
Expand All @@ -27,10 +32,11 @@ export const registerUser = (userData, history) => (dispatch) => {
// Clear previous errors
dispatch(clearErrors());

API.post("/auth/signup", userData)
axios
.post(`${backendURL}/auth/signup`, userData, {
withCredentials: true,
})
.then((res) => {
// console.log(res);

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

API.post("/auth/login", userData)
axios
.post(`${backendURL}/auth/login`, userData, {
withCredentials: true,
})
.then((res) => {
const {
name,
Expand All @@ -91,7 +100,6 @@ export const loginUser = (userData, history) => (dispatch) => {
};

dispatch(setCurrentUser(user));

history.push("/");
})
.catch((err) => {
Expand All @@ -101,7 +109,8 @@ export const loginUser = (userData, history) => (dispatch) => {

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

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

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

const API = axios.create({
baseURL: process.env.REACT_APP_BACKEND_URL,
});
// const backendURL =
// process.env.NODE_ENV === "production"
// ? process.env.REACT_APP_BACKEND_PROD_URL
// : process.env.REACT_APP_BACKEND_DEV_URL;

export default API;
// const API = axios.create({
// baseURL: backendURL,
// });

// export default API;

1 comment on commit 18544f7

@vercel
Copy link

@vercel vercel bot commented on 18544f7 Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.