I have a fairly basic login setup (code below) with a few components requiring authentication. When I navigate to http://localhost:8000/, it redirects to http://localhost:8000/login and everything is fine. If I then log in, it goes back to http://localhost:8000/ and displays my main component.
However, when I navigate to http://localhost:8000/login directly, it says "Cannot GET /login". Same thing with my "about" component. It does work when I add a pound symbol: http://localhost:8000/#/login. Can anyone explain what's going on?
var React = require('react');
var Main = require('./components/main');
var Login = require('./components/login');
var About = require('./components/about');
var SessionStore = require('./stores/session-store')
import createBrowserHistory from 'history/lib/createBrowserHistory';
import { Router, Route, Link, History, IndexRoute } from 'react-router';
var App = React.createClass({
render: function() {
return <div>
{this.props.children}
</div>
}
});
function requireAuth(nextState, replaceState) {
if(!SessionStore.isLoggedIn()){
replaceState({ nextPathname: nextState.location.pathname }, '/login');
}
}
function redirectIfLoggedIn(nextState, replaceState){
if(SessionStore.isLoggedIn()){
replaceState({ nextPathname: nextState.location.pathname }, '/');
}
}
var routes = (
<Router history={createBrowserHistory()}>
<Route path="/" component={App}>
<IndexRoute component={Main} onEnter={requireAuth}/>
<Route path="login" component={Login} onEnter={redirectIfLoggedIn} />
<Route path="about" component={About} onEnter={requireAuth} />
</Route>
</Router>
)
React.render(routes, document.querySelector('.container'));