So I have an API that have the next routes for handling user auth:
- server/api/login: accepts user email and password via post, returns access_token and refresh_token if valid.
- server/api/me: accepts access_token via GET parameters, returns { email, username } if access_token is valid.
How do I handle user states with redux?
Ryan Collins
I’m a software engineer working on the web, specializing in React & GraphQL. Avid functional programmer and aspiring data scientist
I have been using Redux Auth Wrapper as of late, which lets you wrap a route in a function that will allow you to check for an authenticated state. Below is an example and you can dig through the repo.
In my store.js file, I have the following, which looks for a user object on the authReducer.
export const userIsAuthenticated = userAuthWrapper({ authSelector: state => state.authReducer.user, redirectAction: routerActions.replace, failureRedirectPath: '/login', wrapperDisplayName: 'userIsAuthenticated', });I use this along with react router to protect certain routes: Non authenticated route:
<Route path="/events/:eventId" component={Pages.EventPage} />Authenticated route:
<Route path="/create-event" component={userIsAuthenticated(Pages.CreateEventPage)} />