React-router: How to authenticate routes using Passport?

Created on 26 Jan 2015  路  8Comments  路  Source: ReactTraining/react-router

Hi

With React Router, the routing is handled on the client side. May I know how do we authenticate the route the user is trying to access and redirect to a login page if he has not been authenticated?

Most helpful comment

https://github.com/rackt/react-router/blob/master/examples/auth-flow/app.js#L129-164

@morenoh149 Not only assumed, but implemented. From what I can tell the global auth object is meant to simulate an authentication API. In auth-flow's case it's using localstorage. However, even though Passport.js is running server-side you can create a similar solution with a few tweaks. On your server, create endpoints for authentication, such as /login, /logout, and even /verify if you wish. Client-side, for login, re-code auth.login to make an AJAX call to the /login endpoint and share the result with your React app.

All 8 comments

At the moment I try to find a solution for the same problem. But the auth-flow example seems to scale badly.

In my app some pages should be visible to everyone, other pages should be visible only for logged in users or administrators. My current idea is to replace the routes every time the login status changes. I have not tried that yet, because @gaearon's #658 has not landed on npm. Also with #712 it should be possible to make the routes conditional.

I am new to the router, so probably I have missed something.

Thanks for the links and discussion. It's been very informative.
For something like https://github.com/rackt/react-router/blob/master/examples/auth-flow/app.js#L8 it's assuming a global auth variable. How could I get a passport instance exposed in this way?

https://github.com/rackt/react-router/blob/master/examples/auth-flow/app.js#L129-164

@morenoh149 Not only assumed, but implemented. From what I can tell the global auth object is meant to simulate an authentication API. In auth-flow's case it's using localstorage. However, even though Passport.js is running server-side you can create a similar solution with a few tweaks. On your server, create endpoints for authentication, such as /login, /logout, and even /verify if you wish. Client-side, for login, re-code auth.login to make an AJAX call to the /login endpoint and share the result with your React app.

@kilometers amazingly informative. Thank you.

Is there a React router 4 example out there? I came across this one https://github.com/bodiddlie/headache-diary, which is helpful and uses concepts from the docs, but it'd be great to see an example project of how to use passport for oauth server side and rr client side. I have an approach that sets a cookie server side and I can read that from React but trying to figure out how to turn that into a state that I can share in my components is my current dilemma. Thanks!

@gaearon These links are dead, can you please update it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davetgreen picture davetgreen  路  3Comments

wzup picture wzup  路  3Comments

ryansobol picture ryansobol  路  3Comments

maier-stefan picture maier-stefan  路  3Comments

alexyaseen picture alexyaseen  路  3Comments