Skip to content

Instantly share code, notes, and snippets.

@jericrealubit
Created September 13, 2017 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jericrealubit/3ecf16c698dc411e291a09b0fb2d9342 to your computer and use it in GitHub Desktop.
Save jericrealubit/3ecf16c698dc411e291a09b0fb2d9342 to your computer and use it in GitHub Desktop.
The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.
authService.login = function(credentials) {
// we need this to return a promise but we also want to hook into the promise.
return ApiInterface.makeRequest('/api/auth', 'POST', credentials)
.then( function(response) { // success
Session.setAuthToken(response.data.authToken);
Session.initDataStore('order-details', credentials);
$rootScope.$emit(AUTH_EVENTS.loginSuccess);
})
.catch( function(error) { // failed
switch (error.code) {
case 500: $rootScope.$emit(AUTH_EVENTS.internalServerError); break;
case 523: $rootScope.$emit(AUTH_EVENTS.vendorUnavailable); break;
case 422:
if (error.errors[0] == 'invalid orderId') {
$rootScope.$emit(AUTH_EVENTS.validationErrorOrderId);
} else if (error.errors[0] == 'The post code must be between 3 and 4 digits.') {
$rootScope.$emit(AUTH_EVENTS.validationErrorPostCode);
} else {
$rootScope.$emit(AUTH_EVENTS.loginFailed);
}
break;
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment