diff --git a/application/src/components/login/login-form/loginForm.js b/application/src/components/login/login-form/loginForm.js index 73aba471..cd76d9a9 100644 --- a/application/src/components/login/login-form/loginForm.js +++ b/application/src/components/login/login-form/loginForm.js @@ -8,16 +8,25 @@ const mapActionsToProps = dispatch => ({ } }) +const mapStateToProps = (state) => ({ + auth: state.auth, +}) + class LoginForm extends Component { state = { email: "", password: "", } + componentDidUpdate() { + if (this.props.auth && this.props.auth.token) { + this.props.onLogin(); + } + } + login(e) { e.preventDefault(); this.props.commenceLogin(this.state.email, this.state.password); - this.props.onLogin(); } onChange(key, val) { @@ -43,4 +52,4 @@ class LoginForm extends Component { } } -export default connect(null, mapActionsToProps)(LoginForm); \ No newline at end of file +export default connect(mapStateToProps, mapActionsToProps)(LoginForm); \ No newline at end of file diff --git a/application/src/router/GuardedRoute.js b/application/src/router/GuardedRoute.js new file mode 100644 index 00000000..7a33bc05 --- /dev/null +++ b/application/src/router/GuardedRoute.js @@ -0,0 +1,12 @@ +import React from 'react'; +import { Route, Redirect } from "react-router-dom"; + +const GuardedRoute = ({ component: Component, auth, ...rest }) => ( + ( + auth && auth.token + ? + : + )} /> +) + +export default GuardedRoute; \ No newline at end of file diff --git a/application/src/router/appRouter.js b/application/src/router/appRouter.js index 23bbe273..94c02841 100644 --- a/application/src/router/appRouter.js +++ b/application/src/router/appRouter.js @@ -1,16 +1,23 @@ import React from 'react'; import { BrowserRouter as Router, Route } from 'react-router-dom'; import { Main, Login, OrderForm, ViewOrders } from '../components'; +import { connect } from 'react-redux'; +import GuardedRoute from './GuardedRoute'; + + +const mapStateToProps = (state) => ({ + auth: state.auth, +}) const AppRouter = (props) => { return ( - - + + ); } -export default AppRouter; +export default connect(mapStateToProps, null)(AppRouter);