Skip to content

Instantly share code, notes, and snippets.

@jrouleau
Created September 15, 2015 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrouleau/3849f5432123af746b22 to your computer and use it in GitHub Desktop.
Save jrouleau/3849f5432123af746b22 to your computer and use it in GitHub Desktop.
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