import React from 'react' import { render } from 'react-dom' import { browserHistory, Router, Route, Link } from 'react-router' import auth from './auth' const App = React.createClass({ getInitialState() { return { loggedIn: auth.loggedIn() } }, updateAuth(loggedIn) { this.setState({ loggedIn: loggedIn }) }, componentWillMount() { auth.onChange = this.updateAuth auth.login() }, render() { return (
{this.props.children ||

You are {!this.state.loggedIn && 'not'} logged in.

}
) } }) const Dashboard = React.createClass({ render() { const token = auth.getToken() return (

Dashboard

You made it!

{token}

) } }) const Login = React.createClass({ contextTypes: { router: React.PropTypes.object.isRequired }, getInitialState() { return { error: false } }, handleSubmit(event) { event.preventDefault() const email = this.refs.email.value const pass = this.refs.pass.value auth.login(email, pass, (loggedIn) => { if (!loggedIn) return this.setState({ error: true }) const { location } = this.props if (location.state && location.state.nextPathname) { this.context.router.replace(location.state.nextPathname) } else { this.context.router.replace('/') } }) }, render() { return (
(hint: password1)
{this.state.error && (

Bad login information

)}
) } }) const About = React.createClass({ render() { return

About

} }) const Logout = React.createClass({ componentDidMount() { auth.logout() }, render() { return

You are now logged out

} }) function requireAuth(nextState, replace) { if (!auth.loggedIn()) { replace({ pathname: '/login', state: { nextPathname: nextState.location.pathname } }) } } render(( ), document.getElementById('example'))