Skip to content

Commit

Permalink
feat(login): NotifyService for login messages
Browse files Browse the repository at this point in the history
This commit cleans up the auth/login messages by moving server-sent
error messages into `growl-notification` service.  It also exposes a new
method on the `NotifyService` called `fatal()` to signal fatal errors
using the same colorscheme as `NotifyService.handleError()`.  The name
fatal was chosen to encourage developers NOT to use this, except in
very rare cases.  The `danger()` method should be preferred in almost
all scenarios.

This commit also adds the `NotifyService` into the `SessionService` to
display welcome and goodbye messages on login/logout events.

This brings us closer to finishing #222, closing all error handling and
text-related issues.
  • Loading branch information
jniles committed May 5, 2016
1 parent 3ef5632 commit f360025
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
37 changes: 18 additions & 19 deletions client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"CREATE" : "Create Chart of Accounts",
"GENERATE" : "Generate Account Report",
"HELP_TXT_1" : "Chart of accounts for",
"HELP_TXT_2" : "Account information and relationshions are shown in the table on the right",
"HELP_TXT_2" : "Account information and relationships are shown in the table on the right",
"HELP_TXT_3" : "Click 'Add Account' to add an account",
"NEW" : "New Account",
"NOT_FOUND" : "Aucun compte enregistrés...",
"TITLE" : "Account Managment",
"NOT_FOUND" : "Not accounts found.",
"TITLE" : "Account Management",
"TITLE_ACCOUNT" : "Title Account"
},
"AUTH": {
Expand All @@ -33,9 +33,9 @@
"LOGIN" : "Login",
"LOGIN_PLACEHOLDER_PASSWORD" : "Enter your password",
"LOGIN_PLACEHOLDER_USERNAME" : "Enter your username",
"LOGOUT_SUCCESS" : "You are now logged out.",
"RESET" : "Reset",
"TOO_MANY_TRYS" : "Forgot your username or password? Please ask the system administrator to reset it."
"TOO_MANY_TRYS" : "Forgot your username or password? Please ask the system administrator to reset it.",
"WELCOME" : "Welcome to bhima!",
"GOODBYE" : "Thank you for using bhima. Come back soon!"
},
"BILLING_SERVICES": {
"BTN": {
Expand Down Expand Up @@ -107,7 +107,7 @@
"BACK" : "Back to Debtor Groups",
"CREATE" : "Create Debtor Group",
"CREATE_ANOTHER" : "Create another Debtor Group",
"CREATED" : "Debtor group recorded successfully",
"CREATED" : "Debtor group recorded successfully",
"EDIT" : "Edit Debtor Group",
"GROUPS" : "Debtor Groups",
"HELP_TXT_1" : "This interface allows you to review, find, and configure debtor groups",
Expand All @@ -134,7 +134,7 @@
"INFO" : "With this configuration option checked all members of this debtor group will be exempt from paying automatically applied billing services",
"LABEL" : "This group is exempt from paying billing services"
}
},
},
"TITLE" : "Debtor Group Management",
"SUBSCRIBED" : "Debtors subscribed",
"UPDATED" : "Debtor group record updated"
Expand Down Expand Up @@ -181,7 +181,8 @@
"ERRORS": {
"ERR_INTERNET_DISCONNECTED" : "You are not connected to the network.",
"NO_EXCHANGE_RATE" : "No Exchange rate defined",
"UNAUTHORIZED" : "Unauthorized"
"UNAUTHORIZED" : "You have submitted a bad username and password combination. Please login in with valid credentials to continue.",
"ERR_INTERNET_DISCONNECTED" : "You are not connected to the network."
},
"EXCHANGE": {
"ADDING_RATE" : "Adding an exchange Rate",
Expand Down Expand Up @@ -227,15 +228,15 @@
"CONFIRM_DELETE" : "Do you confirm the deletion ?"
},
"ERRORS": {
"CONNECTION" : "Connexion error",
"CONNECTION" : "Connection error",
"GENERIC" : "The form contains errors.",
"HAS_ERROR" : "Contains an error",
"HAS_ERRORS" : "The form contains errors. Please correct them and resubmit.",
"MIN_CURRENCY_UNIT" : "This value is too small for the selected currency.",
"MISSING" : "Missing information",
"MISSING" : "This form is missing required fields (marked in red).",
"MISSING_CASHBOX_ID" : "Missing Cashbox",
"MISSING_DEBTOR_ID" : "Identifying missing Debtor",
"RECORD_ERROR" : "Please resolve any errors on the form. (Marked in red)",
"RECORD_ERROR" : "Please resolve any errors on the form (marked in red).",
"REQUIRED" : "This value is required."
},
"INFOS": {
Expand All @@ -248,7 +249,6 @@
"GROUPS_PATIENT" : "Assign the patient to patient groups from the options below. Press 'confirm' to permenantly update the patient's subscribed groups.",
"ITEMS_FULL" : "There are no additional inventory items that be assigned to this invoice.",
"LOADING" : "Loading",
"MANY_TRYS" : "Many trys",
"NOT_CONFIGURED" : "The accounts for this currency have not been configured yet.",
"NO_INVOICES_SELECTED" : "No invoices selected",
"NO_RECIPIENT" : "An invoice must have a recipient before assigning items.",
Expand All @@ -257,7 +257,6 @@
"PATIENT_FOUND" : "Patient Found",
"PATIENT_NOT_FOUND" : "Patient Not Found",
"SAVE_SUCCESS" : "Save success",
"UNAUTHORIZED" : "Unauthorized",
"UPDATE_SUCCESS" : "Updated successfully"
},
"LABELS": {
Expand Down Expand Up @@ -566,14 +565,14 @@
"VIEW_PATIENT_RECORDS" : "View Patient Records"
},
"PATIENT_REGISTRY" : {
"DAY" : "day",
"DAY" : "day",
"DAYS" : "days",
"MONTH" : "month",
"MONTHS" : "months",
"YEAR_OLD" : "year old",
"YEARS_OLD" : "years old",
"YEARS_OLD" : "years old",
"TITLE" : "Patients Registry"
},
},
"PERMISSIONS": {
"ADDING_USER" : "Add a user",
"ADD_USER" : "Add a New User",
Expand Down Expand Up @@ -721,7 +720,7 @@
"EXCHANGE_RATE" : "Exchange Rate",
"FILTERING": {
"LABEL" : "Filter"
},
},
"FIRST_NAME" : "First Name",
"FUNCTION" : "Function",
"GENDER" : "Gender",
Expand All @@ -734,7 +733,7 @@
"IS_WAREHOUSE" : "Is Warehouse ?",
"LABEL" : "Label",
"LAST_NAME" : "Last Name",
"LAST_TRANSACTION" : "Last Transactions",
"LAST_TRANSACTION" : "Last Transactions",
"LAST_VISIT" : "Last Visit",
"LISTS" : "Registered price list",
"LOADING" : "Fetching data from the server.",
Expand Down
12 changes: 6 additions & 6 deletions client/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"LOGIN" : "Se connecter",
"LOGIN_PLACEHOLDER_PASSWORD" : "Entrez votre mot de passe",
"LOGIN_PLACEHOLDER_USERNAME" : "Entrez votre nom d'utilisateur",
"LOGOUT_SUCCESS" : "Vous n'etes pas connecté",
"RESET" : "Annuler",
"TOO_MANY_TRYS" : "Si vous avez oublié votre login ou votre mot de passe? S'il vous plaît demander à l'administrateur du système pour le réinitialiser."
"TOO_MANY_TRYS" : "Avez-vous oublié votre login ou votre mot de passe? S'il vous plaît demander à l'administrateur du système pour le réinitialiser.",
"WELCOME" : "Bienvenue à bhima!",
"GOODBYE" : "Merci d'utiliser bhima! Reviennez bientôt!"
},
"BILLING_SERVICES": {
"BTN": {
Expand Down Expand Up @@ -180,7 +180,7 @@
"ERRORS": {
"ERR_INTERNET_DISCONNECTED" : "Vous n'êtes pas connecté à un réseau.",
"NO_EXCHANGE_RATE" : "Aucun taux de change defini",
"UNAUTHORIZED" : "Non autorisé"
"UNAUTHORIZED" : "Vous avez soumis une mauvais nom et mot de passe. Entrez un nom et mot de passe valid pour continuer."
},
"EXCHANGE": {
"ADDING_RATE" : "Ajout d'un taux",
Expand Down Expand Up @@ -675,7 +675,7 @@
},
"SETTINGS": {
"TITLE" : "Paramètres"
},
},
"SUBSIDY": {
"ADDING_SUBSIDY" : "Ajout d'une subvention",
"ADD_SUBSIDY" : "Ajouter subvention",
Expand Down Expand Up @@ -944,4 +944,4 @@
"TITLE" : "Bordereau de transfert"
}
}
}
}
8 changes: 7 additions & 1 deletion client/src/js/services/NotifyService.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function NotifyService($translate) {
service.danger = danger;
service.info = info;
service.warn = warn;
service.fatal = fatal;

service.handleError = handleError;

Expand All @@ -76,6 +77,11 @@ function NotifyService($translate) {
setNotification(key, ttl, formatOptions.warn);
}

// this should only be used in rare circumstances, such as the login page
function fatal(key, ttl) {
setNotification(key, ttl, formatOptions.error);
}

/**
* This method is not simply a formatting alias - it accepts an error object
* and parses it to show relevent information in the notification.
Expand All @@ -90,7 +96,7 @@ function NotifyService($translate) {
var message = $translate.instant(key);

// if the request has overridden the time to live, use that, otherwise use the global default
var ttl = ttl || TTL;
ttl = ttl || TTL;
var formatNotification = {
ttl : ttl,
message : message
Expand Down
10 changes: 8 additions & 2 deletions client/src/js/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module('bhima.services')
.service('SessionService', SessionService);

SessionService.$inject = [
'$sessionStorage', '$http', '$location', 'util', '$rootScope'
'$sessionStorage', '$http', '$location', 'util', '$rootScope', 'NotifyService'
];

/**
Expand All @@ -19,7 +19,7 @@ SessionService.$inject = [
* @module services/SessionService.js
* @constructor
*/
function SessionService($sessionStorage, $http, $location, util, $rootScope) {
function SessionService($sessionStorage, $http, $location, util, $rootScope, Notify) {
var service = this;

// set up the storage instance
Expand Down Expand Up @@ -83,6 +83,9 @@ function SessionService($sessionStorage, $http, $location, util, $rootScope) {
// notify login event
$rootScope.$emit('login');

// display welcome message
Notify.info('AUTH.WELCOME', 5000);

return session;
});
}
Expand All @@ -104,6 +107,9 @@ function SessionService($sessionStorage, $http, $location, util, $rootScope) {

// navigate to the main page
$location.url('/login');

// bid the user farewell
Notify.info('AUTH.GOODBYE', 5000);
});
}

Expand Down
22 changes: 3 additions & 19 deletions client/src/partials/login/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
<!-- @fixme - style hacks for content centering -->
<div class="center-block" style="margin-top : calc(50vh - 170px); max-width: 400px;">

<p ng-show="LoginCtrl.excessiveAttempts" class="text-info">
<span class="glyphicon glyphicon-info-sign"></span>
{{ "AUTH.TOO_MANY_TRYS" | translate }}
</p>

<div class="panel panel-default">

<div class="panel-heading clearfix">
Expand Down Expand Up @@ -95,30 +90,19 @@

<div
class="form-group"
ng-class="{ 'has-error' : LoginForm.$submitted && (LoginForm.$invalid || LoginCtrl.error) }"
ng-class="{ 'has-error' : LoginForm.$submitted && LoginForm.$invalid }"
>
<button id="submit" class="btn btn-default" type="submit">
{{ "FORM.BUTTONS.CONNECTION" | translate }}
</button>

<div class="help-block" ng-show="LoginForm.$submitted && (LoginForm.$invalid || LoginCtrl.error)" style="margin-top:10px;">
<div class="help-block" ng-show="LoginForm.$submitted && LoginForm.$invalid" style="margin-top:10px;">

<!-- client-side validation error: requires both username and password -->
<!-- client-side validation error: requires username, password, and project -->
<p ng-show="LoginForm.$invalid">
<span class="glyphicon glyphicon-exclamation-sign"></span>
{{ "FORM.ERRORS.MISSING" | translate }}
</p>

<!-- display the server-sent error as an alert -->
<p ng-show="LoginCtrl.error">
<span class="glyphicon glyphicon-exclamation-sign"></span>
{{ LoginCtrl.error.code | translate }}
</p>

<p ng-show="LoginCtrl.excessiveAttempts" class="text-info">
<span class="glyphicon glyphicon-info-sign"></span>
{{ "FORM.INFOS.MANY_TRYS" | translate }}
</p>
</div>
</div>
</form>
Expand Down
24 changes: 12 additions & 12 deletions client/src/partials/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ angular.module('bhima.controllers')
.controller('LoginController', LoginController);

LoginController.$inject = [
'appcache', 'SessionService', 'LanguageService', 'ProjectService'
'appcache', 'SessionService', 'LanguageService', 'ProjectService',
'NotifyService'
];

/**
* Login Controller
*
* The login controller powers the bhima login page.
*/
function LoginController(AppCache, Session, Languages, Projects) {
function LoginController(AppCache, Session, Languages, Projects, Notify) {
var vm = this;

// the is the same as the SettingsContoller
var cache = AppCache('preferences');

// tracks the number of login attempts made by this user to show a
// "forgot password" message if too many requests are made
var loginAttempts = 0;
var maxLoginAttempts = 3;
var attempts = 0;
var maxAttempts = 3;

// contains the values from the login form
vm.credentials = {};
Expand Down Expand Up @@ -63,9 +64,6 @@ function LoginController(AppCache, Session, Languages, Projects) {
// logs the user in, creates the user client session
function login(form) {

// clear previous HTTP errors if they exist
delete vm.error;

// if the form is not valid, do not generate an $http request
if (form.$invalid) { return; }

Expand All @@ -76,12 +74,14 @@ function LoginController(AppCache, Session, Languages, Projects) {
})
.catch(function (response) {

// bind the $http error to the view
vm.error = response.data;
// if the user has tried too many times, display a fatal error working for
// ten seconds.
if (maxAttempts <= attempts++) {
return Notify.fatal('AUTH.TOO_MANY_TRYS', 10000);
}

// augment the count and rebind the excessive attempts variable
loginAttempts++;
vm.excessiveAttempts = (maxLoginAttempts <= loginAttempts);
// use growl-notifications to display an error at the top of the window
Notify.danger(response.data.code);
});
}
}

0 comments on commit f360025

Please sign in to comment.