Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Method
GET
Comments
Descriptionreturns MyAccount object if logged in, else empty object

Useful for determining if the customer is logged in without triggering a 401 Unauthorized response from the server.

In the demo, this call is only used on the main page to determine whether to show the login screen or not.

Most of the MyAccount pages will use security like this:

Code Block
languagejavascript
var redirectToLogin = function () {
 var location_href = "index.html";
 if (location.hash && location.hash.length > 0) {
 location_href += "?hash=" + location.hash.substring(1);
 }
 location.href = location_href;
};
var theDocument = jQuery(document);
theDocument.ajaxError(function (event, xhr) {

 if (xhr.status == 401)
 redirectToLogin();
});

 
However, that's not desired for the main page, so loggedIn provides a way of checking without triggering a 401.

Cookies

UltraCartMerchantId - Merchant ID

UltraCartShoppingCartId - Cart ID

 
Path Parametersnone 
Query Parameters

none

 
Headers

none

 
Receives Jsonnone 
Returns JsonMyAccount object if logged in, else empty object

Note: To avoid json parser errors from the likes of FireFox, if the customer is not logged in, the method does NOT return null. It returns an empty object. So your check for logged in status should be something like this:

.done(function (result) {
if(result && result.email){
// proceed with success path

settings = result;

} else {
settings = null; // if the result is an empty object, then customer is not logged in.
}
})

By checking both 'result' and 'result.email'

Notice the example below checks for both result and result.email, so it's checking for a valid object that has some properties as well.

By checking both 'result' and 'result.email' for values, you'll get correct results as to whether the customer is logged in or not.

Example   
Code Block
themeDJango
languagejavascript
linenumberstrue
  this.loggedIn = function (options) {
    options = options || {};
    var settings = null;
    jQuery.ajax({
      url: restUrl + '/loggedIn',
      type: 'get',
      async: (options.success || options.failure) ? true : false,
      headers: { "cache-control": "no-cache" },
      cache: false,
      dataType: 'json'
    }).done(function (result) {
              if(result && result.email){
                settings = result;
              } else {
                settings = null; // if the result is an empty object, then customer is not logged in.
              }
              if (options.success) {
                options.success(settings);
              }
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
              if (options.failure) {
                options.failure(jqXHR, textStatus, errorThrown);
              }
            });

    return settings;
  };

 

 

/rest/myaccount/login

Method
GET or POST
Comments
Description

logs in a customer and returns their base information

(MyAccount object)

There are two ways to login: GET or POST.

For the GET, the three parameters are submitted. For POST, a CustomerCredentials object is submitted (it's a simple object with three fields).

Cookies

UltraCartShoppingCartId - Cart ID

not required, but if passed along and valid, the same shopping cart will continue to be used. Otherwise, a new cart is created and Set-Cookie headers will accompany a successful login. 
Path Parametersnone 
Query Parameters

For GET:

merchantId

email

password

For POST:

none

 
Headers

none

 
Receives JsonCustomerCredentials 
Returns JsonMyAccount object if successful

A failed login will result in a 401 Unauthorized http status.

Any missing parameers will result in a 400 Bad Request http status.

Example 
Code Block
themeDJango
languagejavascript
linenumberstrue
  this.login = function (email, password, options) {
    options = options || {};
    var credentials = {merchantId: merchantId, email: email, password: password};
    var account = null;
    jQuery.ajax({
      url: restUrl + '/login',
      data: JSON.stringify(credentials),
      type: 'post',
      async: (options.success || options.failure) ? true : false,
      headers: { "cache-control": "no-cache" },
      contentType: 'application/json; charset=UTF-8',
      cache: false,
      dataType: 'json'
    }).done(function (result) {
              account = result;
              if (options.success) {
                options.success(account);
              }
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
              if (options.failure) {
                options.failure(jqXHR, textStatus, errorThrown);
              }
            });
    return account;
  };

...