-
Notifications
You must be signed in to change notification settings - Fork 18
/
signin.js
55 lines (47 loc) · 1.36 KB
/
signin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { Component } from 'react'
import SigninForm from './signin_form'
import * as actions from '../../actions'
import { connect } from 'react-redux'
import { Redirect } from 'react-router-dom'
class Signin extends Component {
componentWillUnmount() {
if (this.props.errorMessage) {
this.props.authError(null)
}
}
displayRedirectMessages() {
const location = this.props.location
return location.state && <div className="alert alert-danger">{location.state.message}</div>
}
handleSubmit({email, password}) {
this.props.signinUser({email, password})
}
getRedirectPath() {
const locationState = this.props.location.state
if (locationState && locationState.from.pathname) {
return locationState.from.pathname // redirects to referring url
} else {
return '/'
}
}
render() {
return (this.props.authenticated) ?
<Redirect to={{
pathname: this.getRedirectPath(), state: {
from: this.props.location
}
}}/>
:
<div>
{this.displayRedirectMessages()}
<SigninForm onSubmit={this.handleSubmit.bind(this)} errorMessage={this.props.errorMessage}/>
</div>
}
}
function mapStateToProps(state) {
return {
authenticated: state.auth.authenticated,
errorMessage: state.auth.error
}
}
export default connect(mapStateToProps, actions)(Signin)