Client: handle user authentication

This commit is contained in:
SamR1 2017-12-25 20:11:10 +01:00
parent c66f3fd9d7
commit b1fca3ab34
6 changed files with 92 additions and 55 deletions

View File

@ -33,6 +33,7 @@ export function getProfile(dispatch) {
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
dispatch(ProfileSuccess(ret)) dispatch(ProfileSuccess(ret))
// window.location.href = '/'
} else { } else {
dispatch(ProfileError(ret.message)) dispatch(ProfileError(ret.message))
} }
@ -95,7 +96,7 @@ export function handleUserFormSubmit(event, formType) {
event.preventDefault() event.preventDefault()
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState() const state = getState()
let { formData } = state.formData const { formData } = state.formData
formData.formData = state.formData.formData formData.formData = state.formData.formData
if (formType === 'Login') { if (formType === 'Login') {
dispatch(login(formData.formData)) dispatch(login(formData.formData))

View File

@ -1,13 +1,15 @@
import React from 'react' import React from 'react'
import { Route, Switch } from 'react-router-dom' import { Redirect, Route, Switch } from 'react-router-dom'
import './App.css' import './App.css'
import Dashboard from './Dashboard' import Dashboard from './Dashboard'
import Logout from './Logout' import Logout from './Logout'
import NavBar from './NavBar' import NavBar from './NavBar'
import UserForm from './User/UserForm' import UserForm from './User/UserForm'
import { isLoggedIn } from '../utils'
export default class App extends React.Component { export default class App extends React.Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.props = props this.props = props
@ -18,21 +20,38 @@ export default class App extends React.Component {
<div className="App"> <div className="App">
<NavBar /> <NavBar />
<Switch> <Switch>
<Route exact path="/" component={Dashboard} /> <Route
exact path="/"
render={() => (
isLoggedIn() ? (
<Dashboard />
) : (
<Redirect to="/login" />
)
)}
/>
<Route <Route
exact path="/register" exact path="/register"
render={() => ( render={() => (
isLoggedIn() ? (
<Redirect to="/" />
) : (
<UserForm <UserForm
formType={'Register'} formType={'Register'}
/> />
)
)} )}
/> />
<Route <Route
exact path="/login" exact path="/login"
render={() => ( render={() => (
isLoggedIn() ? (
<Redirect to="/" />
) : (
<UserForm <UserForm
formType={'Login'} formType={'Login'}
/> />
)
)} )}
/> />
<Route exact path="/logout" component={Logout} /> <Route exact path="/logout" component={Logout} />

View File

@ -5,6 +5,10 @@ export default function Form (props) {
return ( return (
<div> <div>
<h1>{props.formType}</h1> <h1>{props.formType}</h1>
<div className="container">
<div className="row">
<div className="col-md-3" />
<div className="col-md-6">
<hr /><br /> <hr /><br />
<form onSubmit={event => <form onSubmit={event =>
props.handleUserFormSubmit(event, props.formType)} props.handleUserFormSubmit(event, props.formType)}
@ -48,5 +52,9 @@ export default function Form (props) {
/> />
</form> </form>
</div> </div>
<div className="col-md-3" />
</div>
</div>
</div>
) )
} }

View File

@ -1,11 +1,17 @@
import React from 'react' import React from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { Redirect } from 'react-router-dom'
import Form from './Form' import Form from './Form'
import { handleFormChange, handleUserFormSubmit } from '../../actions' import { handleFormChange, handleUserFormSubmit } from '../../actions'
import { isLoggedIn } from '../../utils'
function UserForm(props) { function UserForm(props) {
return ( return (
<div>
{isLoggedIn() ? (
<Redirect to="/" />
) : (
<div> <div>
{ props.message !== '' && ( { props.message !== '' && (
<code>{props.message}</code> <code>{props.message}</code>
@ -18,6 +24,8 @@ function UserForm(props) {
props.onHandleUserFormSubmit(event, formType)} props.onHandleUserFormSubmit(event, formType)}
/> />
</div> </div>
)}
</div>
) )
} }
export default connect( export default connect(

View File

@ -24,7 +24,7 @@ export const store = createStore(
) )
) )
if (window.localStorage.getItem('authToken') !== null) { if (window.localStorage.authToken !== null) {
store.dispatch(loadProfile()) store.dispatch(loadProfile())
} }

1
mpwo_client/src/utils.js Normal file
View File

@ -0,0 +1 @@
export const isLoggedIn = () => !!window.localStorage.authToken