Bravis
Bravis

Reputation: 91

Private Route with Redux and react router

I have a simple app with auth and private route, I want to get data from the server if the user has token, the back end is ready and works fine, and I log in I have data in redux about the user, but I don't know how to handle with refresh page, where should I do dispatch to call action? if I do it in privateRoute.js, it works strange I want to call server ones, but I did it 3-4 times.

Here are my components without calling sessionActions, sessionActions should update loggedIn and the user would go to /login pages

PrivateRoute

import React, { useState, useEffect } from 'react';
import { Route, Redirect } from 'react-router-dom';
import PropTypes from 'prop-types';
import  sessionAction  from '../store/actions/sessionAction';

const PrivateRoute = ({ component: Component, ...rest }) => {
  const { path, dispatch, loggedIn } = rest;
  });

  return (
    <Route
      path={path}
      render={(props) => (loggedIn ? <Component {...props} />
     <Component {...props}/>
        : (<Redirect to="/login" />))}
    />
  );
};

PrivateRoute.propTypes = {
  component: PropTypes.func.isRequired,
};
export default PrivateRoute;

sessionAction

const sessionAction = (path) => (dispatch) => {
  return sessionServices(path)
    .then((response) => {
      console.log(response);
      const { data } = response;
      if (!data.error) {
        dispatch(success(data));
      }
      dispatch(failure(data.error.text));
    })
    .catch((error) => error);
};

sessionService

import axios from 'axios';

axios.defaults.withCredentials = true;

const sessionServices = (path) => axios({
  method: 'post',
  url: `http://localhost:4000/api/pages${path}`,
})
  .then((response) => response)
  .catch((error) => console.log(error));

export default sessionServices;

Upvotes: 0

Views: 3500

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281686

You must dispatch the actions to fetch user data from the server in your App component which is the top-level component. Also, maintain a loading state in the reducer to render a Loader until the user data is fetched.

const App = props => {
    useEffect(() {
      this.props.sessionActions('/session')
    }, []);
    if(this.state.isLoading) return <Loader />;
    const { loggedIn, user } = this.props;
    return (
      <Router>
         {/* Your Private routes and other routes here */}
      </Router>
    )

}

const mapStateToProps = (state) => {
   return {
      isLoading: state.auth.isLoading,
      user: state.auth.user,
      loggedIn: state.auth.loggedIn
   }
}
export default connect(mapStateToProps, { sessionAction })(App);

Upvotes: 1

Related Questions