0

Hi i am working on react and redux single page application and i am facing a issue.

In my App from the admin panel the admin can make the app close and open.Mean if the app is closed from admin side then we will redirect our users to a error page route and if the app is open from the admin panel then we will allow the users to access and view the pages of our app.

but i am facing a issue i am coming from PHP and Laravel Background in their i can just add a middle ware in each of my routes and i can achieve my goal easily.

I have done some research on it and i find out that i will have to create a Higher Order Component for this and call the api when the app starts and will have to store the api result in local storage but if i call the api and store the api result in my local storage and then with in the next 5 mins admin close the restaurant then how i can update my client local storage so that i can restrict the user access from viewing the pages while the app is closed from admin panel.

so that is why i need to send a ajax request before each route to verify if the user can view the page or not.

Can anyone help me out on this issue here how can i achieve my above goal .

2 Answers 2

3

I think sending an HTTP request before any route is not an optimal solution for your problem.

The simplest way is to just return an HTTP error in response to an authorized HTTP request if the restaurant is closed. So you can set up some HTTP request interceptor on the client side and intercept each HTTP response - if it contains an error response - then just redirect the user to the error page.

Check this for the HTTP interceptors

Sign up to request clarification or add additional context in comments.

10 Comments

but we have some static pages to in our website which we want restrict the user access to like home page because if the users dont send a http request and visits the home page directly then he will see the home page and will think the resturant is open.
so we need a way to stop over all app so waiting for a user to perform a HTTP request action is not a suitable way i think.kindly correct me if i am wrong.
In this case, in my opinion, the best way is to check whether the app is enabled or not when you return the home page. So, if the app is not enabled - return HTTP 404 or something like this. If you don't control the process of rendering an HTML page on the server, then there is no way to forbid the user access to the page, because anyway - he can clean the local-storage, cookie or whatever and then access your page.
Thanks i will also wait for another developer to answer my question to so that i chose the best way.
also can you please explain this process more in detail please how can i do this. "If you don't control the process of rendering an HTML page on the server"
|
1

If you are using connected-react-router

It dispatches an action with type '@@router/LOCATION_CHANGE' everytime the route changes.

In your case you need to do some api calls before route changes.

The perfect place for that are redux middlewares.

Here is redux flow,

dispatch action -> store gets the action and passes to middlewares -> middlewares do some stuff and pass on -> reducers get the action and calculate new state

So you will have a middleware which does your api calls and checks what you need. Then you decide to pass the action to the reducer thus change the route, or halt the action and redirect.

You can add redux middlewares for '@@router/LOCATION_CHANGE'

Something like this

import { push } from 'connected-react-router'
const loadingMiddleware = (store) => (next) => (action) => {
  if (action.type === '@@router/LOCATION_CHANGE') {
    // TODO: do api call here
    if (appIsClosed){
      // redirect to error route
      dispatch(push('/error'))
      // halt current action by returning void
      return;
    }
  }
  // pass onto next middleware
  return next(action);
};

export default loadingMiddleware;

Then add this middleware when creating the store.

import { createStore, combineReducers, applyMiddleware } from 'redux'

const todoApp = combineReducers(reducers)
const store = createStore(
  todoApp,
  // applyMiddleware() tells createStore() how to handle middleware
  applyMiddleware(loadingMiddleware)
)

3 Comments

Can you please explain your answer more in detail.I am a beginner at react.Hope you dont mind....
@usama yes no problem. Redux middlewares pretty advanced stuff, at least the docs say so. Added as much explanation as possible, and added some useful links. Let me know if there is anything else.
Thanks i will look into the redux middle wares and wil get back to you so that i can understand your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.