I'm adding authentication into my app, which uses react-router. I've patterned the client routing after the auth-flow example in react-router, but using passport instead of the localstorage that the example uses. this all works fine.
The next step is protecting routes I am defining for express in server.js
. I could send a redirect to /#/login
, but this feels brittle. What's the best way to derive a URL on the server side to a login route served by react-router?
Here's what I have now in my server.js
, which works, but feels brittle:
app.get('/protected',
// redirecting to #/login seems bad: what if we change hashhistory, etc.
passport.authenticate('local', { failureRedirect: '/#/login'}),
function(req, res) {
res.render('whatever');
});