Handle errors logging in

This commit is contained in:
Tom Russell 2018-09-30 20:28:33 +01:00
parent 2e80c9d2e9
commit aae5c1ca32
2 changed files with 18 additions and 8 deletions

View File

@ -32,8 +32,11 @@ class App extends React.Component {
} }
login(user) { login(user) {
console.log("Logging in") if (user.error) {
console.log(user) this.logout();
return
}
console.log("Logging in", user);
this.setState({user: user}); this.setState({user: user});
} }

View File

@ -38,18 +38,25 @@ class Login extends Component {
body: JSON.stringify(this.state), body: JSON.stringify(this.state),
headers:{ headers:{
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} },
credentials: 'same-origin'
}).then( }).then(
res => res.json() res => res.json()
).then(function(res){ ).then(function(res){
if (res.error) { if (res.error) {
this.setState({error: res.error}) this.setState({error: res.error})
} else { } else {
fetch('/users/me').then( fetch('/users/me', {
credentials: 'same-origin'
}).then(
(res) => res.json() (res) => res.json()
).then( ).then(user => {
(user) => this.props.login(user) if (user.error) {
).catch( this.setState({error: user.error})
} else {
this.props.login(user)
}
}).catch(
(err) => this.setState({error: err}) (err) => this.setState({error: err})
) )
} }
@ -59,7 +66,7 @@ class Login extends Component {
} }
render() { render() {
if (this.props.user) { if (this.props.user && !this.props.user.error) {
return <Redirect to="/my-account.html" /> return <Redirect to="/my-account.html" />
} }
return ( return (