Skip to content

Commit

Permalink
client: Distinguish login failure from server error
Browse files Browse the repository at this point in the history
The 403 or 500 HTTP errors have nothing to do with selfoss credentials so we should show a different error.
While at it, move the error detection to the request function and remove redundant redirect.
  • Loading branch information
jtojnar committed Oct 4, 2022
1 parent 7a0aff6 commit b61c6a2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
7 changes: 7 additions & 0 deletions assets/js/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export class HttpError extends Error {
this.name = 'HttpError';
}
}

export class LoginError extends Error {
constructor(message) {
super(message);
this.name = 'LoginError';
}
}
11 changes: 10 additions & 1 deletion assets/js/requests/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoginError } from '../errors';
import * as ajax from '../helpers/ajax';

/**
Expand All @@ -16,7 +17,15 @@ export function getInstanceInfo() {
export function login(credentials) {
return ajax.post('login', {
body: new URLSearchParams(credentials)
}).promise.then(response => response.json());
}).promise.then(
response => response.json()
).then((data) => {
if (data.success) {
return Promise.resolve();
} else {
return Promise.reject(new LoginError(data.error));
}
});
}

/**
Expand Down
18 changes: 6 additions & 12 deletions assets/js/selfoss-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,13 @@ var selfoss = {
username,
password
};
return login(credentials).then((data) => {
if (data.success) {
selfoss.setSession();
selfoss.history.push('/');
// init offline if supported and not inited yet
return login(credentials).then(() => {
selfoss.setSession();
// init offline if supported and not inited yet
selfoss.dbOffline.init();
if ((!selfoss.db.storage || selfoss.db.broken) && selfoss.db.enableOffline.value) {
// Initialize database in offline mode when it has not been initialized yet or it got broken.
selfoss.dbOffline.init();
if ((!selfoss.db.storage || selfoss.db.broken) && selfoss.db.enableOffline.value) {
// Initialize database in offline mode when it has not been initialized yet or it got broken.
selfoss.dbOffline.init();
}
return Promise.resolve();
} else {
return Promise.reject(new Error(data.error));
}
});
},
Expand Down
14 changes: 12 additions & 2 deletions assets/js/templates/LoginForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import classNames from 'classnames';
import { SpinnerBig } from './Spinner';
import { useHistory, useLocation } from 'react-router-dom';
import { HttpError, LoginError } from '../errors';
import { ConfigurationContext } from '../helpers/configuration';
import { LocalizationContext } from '../helpers/i18n';

Expand All @@ -20,9 +21,18 @@ function handleLogIn({

selfoss.login({ username, password, offlineEnabled }).then(() => {
history.push('/');
}).catch(() => {
}).catch((err) => {
const message =
err instanceof LoginError
? selfoss.app._('login_invalid_credentials')
: selfoss.app._('login_error_generic', {
errorMessage:
err instanceof HttpError
? `HTTP ${err.response.status} ${err.message}`
: err.message,
});
history.replace('/sign/in', {
error: selfoss.app._('login_invalid_credentials')
error: message,
});
}).finally(() => {
setLoading(false);
Expand Down
1 change: 1 addition & 0 deletions assets/locale/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"lang_login_username": "Jméno",
"lang_login_password": "Heslo",
"lang_login_invalid_credentials": "Špatné jméno nebo heslo",
"lang_login_error_generic": "Pokus o přihlášení selhal: {errorMessage}",
"lang_no_title": "Bez titulku",
"lang_error": "Nastala chyba",
"lang_error_unauthorized": "Pro pokračování se prosím {link_begin}přihlaste{link_end}.",
Expand Down
1 change: 1 addition & 0 deletions assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"lang_login_username": "Username",
"lang_login_password": "Password",
"lang_login_invalid_credentials": "Wrong username/password",
"lang_login_error_generic": "Could not log in: {errorMessage}",
"lang_login_offline": "Offline storage",
"lang_no_title": "No title",
"lang_experimental": "Experimental",
Expand Down

0 comments on commit b61c6a2

Please sign in to comment.