Skip to content

Instantly share code, notes, and snippets.

@jrouleau jrouleau/routes.js
Created Sep 15, 2015

Embed
What would you like to do?
Split Index with react-router 1.0.0
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
You can’t perform that action at this time.