diff options
author | Ernst Widerberg <ernst@sunet.se> | 2021-11-25 09:20:19 +0100 |
---|---|---|
committer | Ernst Widerberg <ernst@sunet.se> | 2021-11-25 09:20:19 +0100 |
commit | 9e03be9890049250e174cbb7e0c89c27ce39e41f (patch) | |
tree | 95ce9108444b576b375b76ee964c4d09594c4e16 /src/components/Login.js | |
parent | d2dae19096e29dcf3af8ef4664acabcd5698ad21 (diff) |
Fix error handling: Throw only Error objects
Diffstat (limited to 'src/components/Login.js')
-rw-r--r-- | src/components/Login.js | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/src/components/Login.js b/src/components/Login.js index 6379386..4339539 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -17,7 +17,7 @@ class Login extends React.Component { this.state = { email: "", password: "", - error: false + loginFailed: false }; this.login = this.login.bind(this); @@ -31,21 +31,20 @@ class Login extends React.Component { headers: { Authorization: "Basic " + btoa(email + ":" + password) } }) .then(resp => { - if (resp.status !== 200) throw resp; + if (resp.status === 401) { + // Unauthorized: Wrong email/password + this.setState({ loginFailed: true }); + return; + } + if (resp.status !== 200) + throw new Error( + `Unexpected HTTP response code from JWT server: ${resp.status} ${resp.statusText}` + ); return resp; }) .then(resp => resp.json()) .then(data => { this.props.setToken(data.access_token); - }) - .catch(resp => { - if (resp.status === 401) this.setState({ error: true }); - else - this.props.setError( - new Error( - `Unexpected response status: ${resp.status} ${resp.statusText}` - ) - ); }); } @@ -106,7 +105,7 @@ class Login extends React.Component { > Sign in </Button> - {this.state.error && ( + {this.state.loginFailed && ( <p className="error">Wrong username or password</p> )} </Paper> |