Client: handle user authentication
This commit is contained in:
parent
c66f3fd9d7
commit
b1fca3ab34
@ -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))
|
||||||
|
@ -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={() => (
|
||||||
<UserForm
|
isLoggedIn() ? (
|
||||||
formType={'Register'}
|
<Redirect to="/" />
|
||||||
/>
|
) : (
|
||||||
|
<UserForm
|
||||||
|
formType={'Register'}
|
||||||
|
/>
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
exact path="/login"
|
exact path="/login"
|
||||||
render={() => (
|
render={() => (
|
||||||
<UserForm
|
isLoggedIn() ? (
|
||||||
formType={'Login'}
|
<Redirect to="/" />
|
||||||
/>
|
) : (
|
||||||
|
<UserForm
|
||||||
|
formType={'Login'}
|
||||||
|
/>
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
<Route exact path="/logout" component={Logout} />
|
<Route exact path="/logout" component={Logout} />
|
||||||
|
@ -5,48 +5,56 @@ export default function Form (props) {
|
|||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>{props.formType}</h1>
|
<h1>{props.formType}</h1>
|
||||||
<hr /><br />
|
<div className="container">
|
||||||
<form onSubmit={event =>
|
<div className="row">
|
||||||
props.handleUserFormSubmit(event, props.formType)}
|
<div className="col-md-3" />
|
||||||
>
|
<div className="col-md-6">
|
||||||
{props.formType === 'Register' &&
|
<hr /><br />
|
||||||
|
<form onSubmit={event =>
|
||||||
|
props.handleUserFormSubmit(event, props.formType)}
|
||||||
|
>
|
||||||
|
{props.formType === 'Register' &&
|
||||||
|
<div className="form-group">
|
||||||
|
<input
|
||||||
|
name="username"
|
||||||
|
className="form-control input-lg"
|
||||||
|
type="text"
|
||||||
|
placeholder="Enter a username"
|
||||||
|
required
|
||||||
|
onChange={props.onHandleFormChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<input
|
<input
|
||||||
name="username"
|
name="email"
|
||||||
className="form-control input-lg"
|
className="form-control input-lg"
|
||||||
type="text"
|
type="email"
|
||||||
placeholder="Enter a username"
|
placeholder="Enter an email address"
|
||||||
required
|
required
|
||||||
onChange={props.onHandleFormChange}
|
onChange={props.onHandleFormChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
}
|
<div className="form-group">
|
||||||
<div className="form-group">
|
<input
|
||||||
<input
|
name="password"
|
||||||
name="email"
|
className="form-control input-lg"
|
||||||
className="form-control input-lg"
|
type="password"
|
||||||
type="email"
|
placeholder="Enter a password"
|
||||||
placeholder="Enter an email address"
|
required
|
||||||
required
|
onChange={props.onHandleFormChange}
|
||||||
onChange={props.onHandleFormChange}
|
/>
|
||||||
/>
|
</div>
|
||||||
|
<input
|
||||||
|
type="submit"
|
||||||
|
className="btn btn-primary btn-lg btn-block"
|
||||||
|
value="Submit"
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="col-md-3" />
|
||||||
<input
|
</div>
|
||||||
name="password"
|
</div>
|
||||||
className="form-control input-lg"
|
|
||||||
type="password"
|
|
||||||
placeholder="Enter a password"
|
|
||||||
required
|
|
||||||
onChange={props.onHandleFormChange}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<input
|
|
||||||
type="submit"
|
|
||||||
className="btn btn-primary btn-lg btn-block"
|
|
||||||
value="Submit"
|
|
||||||
/>
|
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,30 @@
|
|||||||
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>
|
<div>
|
||||||
{props.message !== '' && (
|
{isLoggedIn() ? (
|
||||||
<code>{props.message}</code>
|
<Redirect to="/" />
|
||||||
)}
|
) : (
|
||||||
<Form
|
<div>
|
||||||
formType={props.formType}
|
{ props.message !== '' && (
|
||||||
userForm={props.formData}
|
<code>{props.message}</code>
|
||||||
onHandleFormChange={event => props.onHandleFormChange(event)}
|
)}
|
||||||
handleUserFormSubmit={(event, formType) =>
|
<Form
|
||||||
props.onHandleUserFormSubmit(event, formType)}
|
formType={props.formType}
|
||||||
/>
|
userForm={props.formData}
|
||||||
|
onHandleFormChange={event => props.onHandleFormChange(event)}
|
||||||
|
handleUserFormSubmit={(event, formType) =>
|
||||||
|
props.onHandleUserFormSubmit(event, formType)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -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
1
mpwo_client/src/utils.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const isLoggedIn = () => !!window.localStorage.authToken
|
Loading…
Reference in New Issue
Block a user