vladimirponomarev / authentication-in-react-apps

:key: Source code for a tutorial on implementing authentication in React applications.
https://vladimirponomarev.com/blog/authentication-in-react-apps-creating-components
MIT License
247 stars 79 forks source link

Direct URL to auth pages doesn't work #8

Open ohadbp opened 7 years ago

ohadbp commented 7 years ago

Hey,

first of all thank you for this tutorial - very helpful. One question tho, using this code if i go directly to http://localhost:3000/login , instead of going to '/' and then click on the topbar option - we get 404 since don't have this route on the server side.

One way to solve it is to move everything to server side rendering but i would like to avoid it. What would be the best way to solve this ? while keeping keep rendering on the client side

Thanks.

MaffooBristol commented 7 years ago

It's actually quite simple. After all the express.static definitions in index.js, put in a catch-all that redirects non-static files to the index.html. React-router will then catch these and send the correct route:

const path = require('path');
app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, './server/static/index.html'));
});