From b1fca3ab349d0adb75fcc6069c4cc504392803e6 Mon Sep 17 00:00:00 2001 From: SamR1 Date: Mon, 25 Dec 2017 20:11:10 +0100 Subject: [PATCH] Client: handle user authentication --- mpwo_client/src/actions/index.js | 3 +- mpwo_client/src/components/App.jsx | 35 +++++++-- mpwo_client/src/components/User/Form.jsx | 78 +++++++++++--------- mpwo_client/src/components/User/UserForm.jsx | 28 ++++--- mpwo_client/src/index.js | 2 +- mpwo_client/src/utils.js | 1 + 6 files changed, 92 insertions(+), 55 deletions(-) create mode 100644 mpwo_client/src/utils.js diff --git a/mpwo_client/src/actions/index.js b/mpwo_client/src/actions/index.js index 2a80528e..ffbded78 100644 --- a/mpwo_client/src/actions/index.js +++ b/mpwo_client/src/actions/index.js @@ -33,6 +33,7 @@ export function getProfile(dispatch) { .then(ret => { if (ret.status === 'success') { dispatch(ProfileSuccess(ret)) + // window.location.href = '/' } else { dispatch(ProfileError(ret.message)) } @@ -95,7 +96,7 @@ export function handleUserFormSubmit(event, formType) { event.preventDefault() return (dispatch, getState) => { const state = getState() - let { formData } = state.formData + const { formData } = state.formData formData.formData = state.formData.formData if (formType === 'Login') { dispatch(login(formData.formData)) diff --git a/mpwo_client/src/components/App.jsx b/mpwo_client/src/components/App.jsx index 0d522f45..d3e40427 100644 --- a/mpwo_client/src/components/App.jsx +++ b/mpwo_client/src/components/App.jsx @@ -1,13 +1,15 @@ import React from 'react' -import { Route, Switch } from 'react-router-dom' +import { Redirect, Route, Switch } from 'react-router-dom' import './App.css' import Dashboard from './Dashboard' import Logout from './Logout' import NavBar from './NavBar' import UserForm from './User/UserForm' +import { isLoggedIn } from '../utils' export default class App extends React.Component { + constructor(props) { super(props) this.props = props @@ -18,21 +20,38 @@ export default class App extends React.Component {
- + ( + isLoggedIn() ? ( + + ) : ( + + ) + )} + /> ( - + isLoggedIn() ? ( + + ) : ( + + ) )} /> ( - + isLoggedIn() ? ( + + ) : ( + + ) )} /> diff --git a/mpwo_client/src/components/User/Form.jsx b/mpwo_client/src/components/User/Form.jsx index 6a31f75f..666c3a4d 100644 --- a/mpwo_client/src/components/User/Form.jsx +++ b/mpwo_client/src/components/User/Form.jsx @@ -5,48 +5,56 @@ export default function Form (props) { return (

{props.formType}

-

-
- props.handleUserFormSubmit(event, props.formType)} - > - {props.formType === 'Register' && +
+
+
+
+

+ + props.handleUserFormSubmit(event, props.formType)} + > + {props.formType === 'Register' && +
+ +
+ }
-
- } -
- +
+
+ +
+ +
-
- -
- - +
+
+
) } diff --git a/mpwo_client/src/components/User/UserForm.jsx b/mpwo_client/src/components/User/UserForm.jsx index a9e90da0..f0099a65 100644 --- a/mpwo_client/src/components/User/UserForm.jsx +++ b/mpwo_client/src/components/User/UserForm.jsx @@ -1,22 +1,30 @@ import React from 'react' import { connect } from 'react-redux' +import { Redirect } from 'react-router-dom' import Form from './Form' import { handleFormChange, handleUserFormSubmit } from '../../actions' +import { isLoggedIn } from '../../utils' function UserForm(props) { return (
- {props.message !== '' && ( - {props.message} - )} -
props.onHandleFormChange(event)} - handleUserFormSubmit={(event, formType) => - props.onHandleUserFormSubmit(event, formType)} - /> + {isLoggedIn() ? ( + + ) : ( +
+ { props.message !== '' && ( + {props.message} + )} + props.onHandleFormChange(event)} + handleUserFormSubmit={(event, formType) => + props.onHandleUserFormSubmit(event, formType)} + /> +
+ )}
) } diff --git a/mpwo_client/src/index.js b/mpwo_client/src/index.js index c09e306a..f9cbdc4a 100644 --- a/mpwo_client/src/index.js +++ b/mpwo_client/src/index.js @@ -24,7 +24,7 @@ export const store = createStore( ) ) -if (window.localStorage.getItem('authToken') !== null) { +if (window.localStorage.authToken !== null) { store.dispatch(loadProfile()) } diff --git a/mpwo_client/src/utils.js b/mpwo_client/src/utils.js new file mode 100644 index 00000000..85f770ac --- /dev/null +++ b/mpwo_client/src/utils.js @@ -0,0 +1 @@ +export const isLoggedIn = () => !!window.localStorage.authToken