From aae5c1ca3243c31d5f8c115b4ab6784e055ab0f9 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Sun, 30 Sep 2018 20:28:33 +0100 Subject: [PATCH] Handle errors logging in --- app/src/frontend/app.js | 7 +++++-- app/src/frontend/login.js | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/src/frontend/app.js b/app/src/frontend/app.js index 4b9592b2..fdc29731 100644 --- a/app/src/frontend/app.js +++ b/app/src/frontend/app.js @@ -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}); } diff --git a/app/src/frontend/login.js b/app/src/frontend/login.js index 4df84858..378184b7 100644 --- a/app/src/frontend/login.js +++ b/app/src/frontend/login.js @@ -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 } return (