Skip to content

Instantly share code, notes, and snippets.

@rottmann
Last active August 29, 2015 14:07
Show Gist options
  • Save rottmann/81b7330f574132e04dbe to your computer and use it in GitHub Desktop.
Save rottmann/81b7330f574132e04dbe to your computer and use it in GitHub Desktop.
React-Router with Auth
// Router
var routes = (
<Routes location="history">
<Route name="index" path="/" handler={App}>
<Route name="test" path="/test/" handler={Test} />
</Route>
<Route name="login" path="/login/" handler={Login}>
</Route>
<Redirect to="index" />
</Routes>
);
React.renderComponent(routes, document.body);
// AuthMixin
var AuthMixin = {
auth: null,
statics: {
willTransitionTo: function(transition) {
if ( ! AuthMixin.auth.isAuthenticated()) {
transition.redirect('/login/');
}
}
}
};
module.exports = AuthMixin;
@rottmann
Copy link
Author

AuthMixin.auth is a custom auth object -> e.g. accessing cookies / a database / ...

@srconklin
Copy link

I am fairly new to React. I am trying to do what i think you are accomplishing here which is to use React Router with a authentication "interceptor" to authenticate a user on each request...I am also using fluxxor but i am having trouble getting the big picture of how this all comes together..
Do you have a more built example that using this gist so that i can gain some context of its use?

thanks for any help
Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment