1

I am building and react express app, I am not using server side rendering, where I am facing problem in session management in the client side

So problem. Is like this

I have express route /, /login, /logout, /dashboard And I also have the same routing in react using react router dom

I used express-session to set the useremail as a session variable in /login route of express app Then I redirect user to /dashboard . Before redirect user to /dashboard I put authentication code where I check session.useremail is set or not

But when user hit /dashboard to server . Server do authentication . But if user navigate to /dashboard in react client app it just show all the dashboard

2
  • "But if user navigate to /dashboard in react client app it just show all the dashboard", you mean a user without authenticated? Are you checking your Dashboard component for authentication on your client side? Commented Apr 19, 2018 at 14:36
  • Exactly my question is how do we authenticate user in /dashboard in client side using express session? Commented Apr 19, 2018 at 17:06

1 Answer 1

1

You need to check if a user is authenticated or not in your client side. This means if session holds a user or not at that time. Checking authentication depends how you set up your client side logic. But first, you need an Express route to check if session has a user or not. Then you can write your client side code.

If you won't use Redux to handle your state, then you need to go to that Express route every time in a component where you want to check the authentication status. After getting your result you can show the component to a client or redirect them somewhere else depending on that result. I am not a pro, maybe there is another easy way to skip going to that route every time. Maybe we can check for a user in the entry point of the application and write authentication information in a key in localStorage. Then going to Express route, we can check that key in localStorage. I prefer using Redux, handling authentication state there.

If you will use Redux, you can write an action creator and reducer to check the authentication status and set your state (like auth) and check the state where you need.

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

Comments

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.