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) {
console.log("Logging in")
console.log(user)
if (user.error) {
this.logout();
return
}
console.log("Logging in", user);
this.setState({user: user});
}

View File

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