From ca98f72c0c2d5983cf482e7a20aadc418f73a27d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Zen?= Date: Tue, 27 Dec 2016 16:58:02 -0200 Subject: [PATCH] Showing errors on login screen --- .../netlify-auth/AuthenticationPage.css | 5 ++ .../netlify-auth/AuthenticationPage.js | 49 ++++++++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/backends/netlify-auth/AuthenticationPage.css b/src/backends/netlify-auth/AuthenticationPage.css index c3f38b47..fd5e679e 100644 --- a/src/backends/netlify-auth/AuthenticationPage.css +++ b/src/backends/netlify-auth/AuthenticationPage.css @@ -13,6 +13,11 @@ .card img { display: block; margin: auto; + padding-bottom: 5px; +} + +.errorMsg { + color: #dd0000; } .button { diff --git a/src/backends/netlify-auth/AuthenticationPage.js b/src/backends/netlify-auth/AuthenticationPage.js index b21612b2..dade3222 100644 --- a/src/backends/netlify-auth/AuthenticationPage.js +++ b/src/backends/netlify-auth/AuthenticationPage.js @@ -10,7 +10,8 @@ export default class AuthenticationPage extends React.Component { onLogin: React.PropTypes.func.isRequired, }; - state = { username: "", password: "" }; + state = { username: "", password: "", errors: {} }; + handleChange = (name, value) => { this.setState({ ...this.state, [name]: value }); @@ -18,24 +19,56 @@ export default class AuthenticationPage extends React.Component { handleLogin = (e) => { e.preventDefault(); + + const { username, password } = this.state; + const errors = {}; + if (!username) { + errors.username = 'Make sure to enter your user name'; + } + if (!password) { + errors.password = 'Please enter your password'; + } + + if (Object.keys(errors).length > 0) { + this.setState({ errors }); + return; + } + AuthenticationPage.authClient.login(this.state.username, this.state.password, true) .then((user) => { this.props.onLogin(user); }) - .catch((err) => { throw err; }); + .catch((error) => { + this.setState({ errors: { server: error.description || error.msg || error }, loggingIn: false }); + }); }; render() { - const { loginError } = this.state; + const { errors } = this.state; return (
- {loginError &&

{loginError}

} - - - - + + {errors.server &&

+ {errors.server} +

} + +