From 1ea6b0b75b0c259dc7f1eac06957750b7dbd4b03 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Thu, 13 Sep 2018 16:58:05 +0100 Subject: [PATCH] Handle and show login/signup errors --- app/src/frontend/app.js | 2 ++ app/src/frontend/error-box.js | 20 +++++++++++++++++ app/src/frontend/login.js | 32 ++++++++++++++------------- app/src/frontend/signup.js | 36 +++++++++++++++++++------------ app/src/frontend/styles/forms.css | 4 ++-- app/src/server.js | 4 +++- app/src/user.js | 12 ++++++++--- 7 files changed, 75 insertions(+), 35 deletions(-) create mode 100644 app/src/frontend/error-box.js diff --git a/app/src/frontend/app.js b/app/src/frontend/app.js index 80ecac94..0f8336fa 100644 --- a/app/src/frontend/app.js +++ b/app/src/frontend/app.js @@ -30,6 +30,8 @@ class App extends React.Component { } login(user) { + console.log("Logging in") + console.log(user) this.setState({user: user}); } diff --git a/app/src/frontend/error-box.js b/app/src/frontend/error-box.js new file mode 100644 index 00000000..c533016c --- /dev/null +++ b/app/src/frontend/error-box.js @@ -0,0 +1,20 @@ +import React, { Fragment } from 'react'; + +const ErrorBox = (props) => ( + + { + (props.msg)? + ( +
+ { + (typeof props.msg === 'string' || props.msg instanceof String)? + props.msg + : 'Unexpected error' + } +
+ ) : null + } +
+); + +export default ErrorBox; diff --git a/app/src/frontend/login.js b/app/src/frontend/login.js index 9b961eab..2529e4a0 100644 --- a/app/src/frontend/login.js +++ b/app/src/frontend/login.js @@ -1,13 +1,16 @@ import React, { Component } from 'react'; import { Redirect, Link } from 'react-router-dom'; +import ErrorBox from './error-box'; + class Login extends Component { constructor(props) { super(props); this.state = { username: '', password: '', - show_password: '' + show_password: '', + error: undefined }; this.handleChange = this.handleChange.bind(this); @@ -26,7 +29,6 @@ class Login extends Component { handleSubmit(event) { event.preventDefault(); - const login = this.props.login fetch('/login', { method: 'POST', body: JSON.stringify(this.state), @@ -37,20 +39,19 @@ class Login extends Component { res => res.json() ).then(function(res){ if (res.error) { - console.error(res.error); // tell user + this.setState({error: res.error}) } else { - console.log(res); // redirect back + this.setState({error: undefined}) fetch('/users/me').then( (res) => res.json() - ).then(function(user){ - console.log(user) - login(user); - }).catch(function(err){ - console.error(err); - }) + ).then( + (user) => this.props.login(user) + ).catch( + (err) => this.setState({error: err}) + ) } - }).catch( - err => console.error(err) + }.bind(this)).catch( + (err) => this.setState({error: err}) ); } @@ -62,6 +63,7 @@ class Login extends Component {

Log in

+
- +
- Would you like to create an account? + Would you like to create an account instead?
Sign Up diff --git a/app/src/frontend/signup.js b/app/src/frontend/signup.js index 03477e91..7ff27f39 100644 --- a/app/src/frontend/signup.js +++ b/app/src/frontend/signup.js @@ -1,6 +1,8 @@ import React, { Component } from 'react'; import { Redirect, Link } from 'react-router-dom'; +import ErrorBox from './error-box'; + class SignUp extends Component { constructor(props) { super(props); @@ -10,7 +12,8 @@ class SignUp extends Component { confirm_email: '', password: '', show_password: '', - confirm_conditions: false + confirm_conditions: false, + error: undefined }; this.handleChange = this.handleChange.bind(this); @@ -39,17 +42,19 @@ class SignUp extends Component { res => res.json() ).then(function(res){ if (res.error) { - console.error(res.error); // tell user + this.setState({error: res.error}) } else { - console.log(res); // redirect back - fetch('/users/me').then(function(user){ - this.props.login(user); - }).catch(function(err){ - console.error(err); - }) + this.setState({error: undefined}) + fetch('/users/me').then( + (res) => res.json() + ).then( + (user) => this.props.login(user) + ).catch( + (err) => this.setState({error: err}) + ) } - }).catch( - err => console.error(err) + }.bind(this)).catch( + (err) => this.setState({error: err}) ); } @@ -64,6 +69,7 @@ class SignUp extends Component {

Create an account to start colouring in.

+ - +
-