Skip to content

Commit

Permalink
enable react-router
Browse files Browse the repository at this point in the history
add basic router auth
  • Loading branch information
matthewglover committed Oct 6, 2016
1 parent af607dd commit c69805f
Show file tree
Hide file tree
Showing 33 changed files with 266 additions and 471 deletions.
25 changes: 5 additions & 20 deletions app/action_creators/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
// @flow
import uuid from 'uuid';
import { ADD_TODO, TOGGLE_TODO, SET_VISIBILITY_FILTER } from '../action_types';
/* eslint-disable import/prefer-default-export */
import { UPDATE_AUTH_STATUS } from '../action_types';


export const addTodo = (text: string): Object =>
({
type: ADD_TODO,
text,
id: uuid.v1(),
});

export const toggleTodo = (id: string): Object =>
({
type: TOGGLE_TODO,
id,
});

export const setVisibilityFilter = (filter: string): Object =>
export const updateAuthStatus = (isAuthenticated) =>
({
type: SET_VISIBILITY_FILTER,
filter,
type: UPDATE_AUTH_STATUS,
isAuthenticated,
});
6 changes: 2 additions & 4 deletions app/action_types/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export const ADD_TODO = 'ADD_TODO';
export const REMOVE_TODO = 'REMOVE_TODO';
export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER';
export const TOGGLE_TODO = 'TOGGLE_TODO';
/* eslint-disable import/prefer-default-export */
export const UPDATE_AUTH_STATUS = 'UPDATE_AUTH_STATUS';
75 changes: 20 additions & 55 deletions app/components/app.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,32 @@
import React, { PropTypes } from 'react';
import { Router, Route, IndexRedirect, hashHistory, Link, withRouter } from 'react-router';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { Provider } from 'react-redux';
import { Router, Route, IndexRedirect, hashHistory } from 'react-router';
import requiresAuthentication from './requires_authentication';
import Login from './login';
import Main from './main';
import Splash from './splash';

const auth = { isLoggedIn: false };

const TestApp = ({ children }) =>
<div>
<div>
<Link to="/splash">Splash</Link><br />
<Link to="/login">Login</Link><br />
</div>
{children}
</div>;
const redirectIfNotAuthenticated = (Component, redirectPath) =>
requiresAuthentication(Component, redirectPath, true);

TestApp.propTypes = {
children: PropTypes.node.isRequired,
};

const Splash = () =>
<div>
Hello from Splash Component
</div>;

const Login = withRouter(({ router }) => {
const doLogin = () => {
auth.isLoggedIn = true;
router.push('/splash');
};
const redirectIfAuthenticated = (Component, redirectPath) =>
requiresAuthentication(Component, redirectPath, false);

return (
<div>
<p>Hello from Login Component</p>
<a onClick={doLogin}>Log in</a>
</div>);
});

const redirectIfNotAuthenticated = (nextState, replace) => {
if (!auth.isLoggedIn) {
replace({
pathname: '/login',
state: { nextPathName: nextState.location.pathname },
});
}
};

const redirectIfAuthenticated = (nextState, replace) => {
if (auth.isLoggedIn) {
replace({
pathname: '/splash',
state: { nextPathName: nextState.location.pathname },
});
}
};

const App = () =>
<MuiThemeProvider>
const App = ({ store }) =>
<Provider store={store}>
<Router history={hashHistory}>
<Route path="/" component={TestApp}>
<Route path="/" component={Main}>
<IndexRedirect to="/splash" />
<Route path="splash" component={Splash} onEnter={redirectIfNotAuthenticated} />
<Route path="login" component={Login} onEnter={redirectIfAuthenticated} />
<Route path="splash" component={redirectIfNotAuthenticated(Splash, '/login')} />
<Route path="login" component={redirectIfAuthenticated(Login, '/splash')} />
</Route>
</Router>
</MuiThemeProvider>;
</Provider>;

App.propTypes = {
store: PropTypes.object.isRequired,
};

export default App;
17 changes: 0 additions & 17 deletions app/components/header.js

This file was deleted.

16 changes: 16 additions & 0 deletions app/components/login/connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { compose, pick, prop } from 'ramda';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { updateAuthStatus } from '../../action_creators/';


const mapStateToProps =
compose(pick(['isAuthenticated']), prop('auth'));

const mapDispatchToProps = dispatch =>
bindActionCreators({ updateAuthStatus }, dispatch);

const connector =
connect(mapStateToProps, mapDispatchToProps);

export default connector;
9 changes: 9 additions & 0 deletions app/components/login/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { compose } from 'ramda';
import { withRouter } from 'react-router';
import connector from './connector';
import SimpleLogin from './simple_login';

const Login =
compose(withRouter, connector)(SimpleLogin);

export default Login;
22 changes: 22 additions & 0 deletions app/components/login/simple_login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { PropTypes } from 'react';


const SimpleLogin = ({ router, updateAuthStatus, isAuthenticated }) => {
const doLogin = () => {
updateAuthStatus(!isAuthenticated);
router.push('/splash');
};

return (<div>
<p>Hello from Login Component</p>
<a onClick={doLogin}>Log in</a>
</div>);
};

SimpleLogin.propTypes = {
router: PropTypes.object.isRequired,
updateAuthStatus: PropTypes.func.isRequired,
isAuthenticated: PropTypes.bool.isRequired,
};

export default SimpleLogin;
22 changes: 22 additions & 0 deletions app/components/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { PropTypes } from 'react';
import { Link } from 'react-router';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';


const Main = ({ children }) =>
<MuiThemeProvider>
<div>
<div>
<Link to="/splash">Splash</Link><br />
<Link to="/login">Login</Link><br />
</div>
{children}
</div>
</MuiThemeProvider>;


Main.propTypes = {
children: PropTypes.node.isRequired,
};

export default Main;
51 changes: 0 additions & 51 deletions app/components/new_todo_form.js

This file was deleted.

11 changes: 11 additions & 0 deletions app/components/requires_authentication/connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { compose, pick, prop } from 'ramda';
import { connect } from 'react-redux';

// mapStateToProps :: State -> { isAuthenticated: boolean }
const mapStateToProps =
compose(pick(['isAuthenticated']), prop('auth'));

const connector =
connect(mapStateToProps);

export default connector;
38 changes: 38 additions & 0 deletions app/components/requires_authentication/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { Component, PropTypes } from 'react';
import { compose } from 'ramda';
import { withRouter } from 'react-router';
import connector from './connector';

const requiresAuthentication = (MyComponent, redirectPath, requiredAuthStatus = true) => {
class AuthenticatedComponent extends Component {

componentWillMount() {
this.checkAuth();
}

componentWillReceiveProps() {
this.checkAuth();
}

checkAuth() {
if (this.props.isAuthenticated !== requiredAuthStatus) {
this.props.router.push(redirectPath);
}
}

render() {
return this.props.isAuthenticated === requiredAuthStatus
? (<MyComponent {...this.props} />)
: null;
}
}

AuthenticatedComponent.propTypes = {
isAuthenticated: PropTypes.bool.isRequired,
router: PropTypes.object.isRequired,
};

return compose(withRouter, connector)(AuthenticatedComponent);
};

export default requiresAuthentication;
14 changes: 0 additions & 14 deletions app/components/root.js

This file was deleted.

9 changes: 9 additions & 0 deletions app/components/splash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';


const Splash = () =>
<div>
Hello from Splash Components
</div>;

export default Splash;
14 changes: 0 additions & 14 deletions app/components/square.js

This file was deleted.

21 changes: 0 additions & 21 deletions app/components/todo.js

This file was deleted.

0 comments on commit c69805f

Please sign in to comment.