Created
September 15, 2015 02:48
-
-
Save jrouleau/3849f5432123af746b22 to your computer and use it in GitHub Desktop.
Split Index with react-router 1.0.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import auth from "../utils/auth"; | |
function logout(nextState, replaceState) { | |
auth.logout(); | |
replaceState(null, "/"); | |
} | |
function redirectToLogin(nextState, replaceState) { | |
if (!auth.isLoggedIn()) { | |
replaceState({ | |
nextPathname: nextState.location.pathname, | |
}, "/login"); | |
} | |
} | |
function redirectToDashboard(nextState, replaceState) { | |
if (auth.isLoggedIn()) { | |
replaceState(null, "/"); | |
} | |
} | |
export default { | |
component: require("../components/App"), | |
childRoutes: [ | |
{ path: "/logout", onEnter: logout }, | |
{ onEnter: redirectToDashboard, | |
childRoutes: [ | |
// Unauthenticated routes | |
// Redirect to dashboard if user is already logged in | |
{ path: "/login", | |
getComponent: (location, cb) => { | |
require.ensure([], (require) => { | |
cb(null, require("../components/Login")); | |
}); | |
}, | |
}, | |
{ path: "/signup", | |
getComponent: (location, cb) => { | |
require.ensure([], (require) => { | |
cb(null, require("../components/Signup")); | |
}); | |
}, | |
}, | |
], | |
}, | |
{ onEnter: redirectToLogin, | |
childRoutes: [ | |
// Protected routes that don't share the dashboard UI | |
{ path: "/user/:id", | |
getComponent: (location, cb) => { | |
require.ensure([], (require) => { | |
cb(null, require("../components/User")); | |
}); | |
}, | |
}, | |
// ... | |
], | |
}, | |
{ path: "/", | |
getComponent: (location, cb) => { | |
// Share the path | |
// Dynamically load the correct component | |
if (auth.isLoggedIn()) { | |
return require.ensure([], (require) => { | |
cb(null, require("../components/Dashboard")); | |
}); | |
} | |
return require.ensure([], (require) => { | |
cb(null, require("../components/Landing")); | |
}); | |
}, | |
indexRoute: { | |
getComponent: (location, cb) => { | |
// Only load if we're logged in | |
if (auth.isLoggedIn()) { | |
return require.ensure([], (require) => { | |
cb(null, require("../components/TabOne")); | |
}); | |
} | |
return cb(); | |
}, | |
}, | |
childRoutes: [ | |
{ onEnter: redirectToLogin, | |
childRoutes: [ | |
// Protected nested routes for the dashboard | |
{ path: "/tab2", | |
getComponent: (location, cb) => { | |
require.ensure([], (require) => { | |
cb(null, require("../components/TabTwo")); | |
}); | |
}, | |
}, | |
// ... | |
], | |
}, | |
], | |
}, | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment