...
| | |
---|
Description | returns 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 |
---|
| 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 Parameters | none | |
Query Parameters | none | |
Headers | none | |
Receives Json | none | |
Returns Json | MyAccount 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. 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 |
---|
theme | DJango |
---|
language | javascript |
---|
linenumbers | true |
---|
| 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
| | |
---|
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 Parameters | none | |
Query Parameters | For GET: merchantId
email password For POST: none | |
Headers | none | |
Receives Json | CustomerCredentials | |
Returns Json | MyAccount 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 |
---|
theme | DJango |
---|
language | javascript |
---|
linenumbers | true |
---|
| 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;
}; |
|
TODO:
/myaccount/logout (GET)
/rest/myaccount/changePassword
| | |
---|
Description | Updates password | This is the only way for a user to change their password |
Cookies | UltraCartShoppingCartId - Cart ID
UltraCartMerchantID = Merchant ID
| The customer must be successfully logged in to change their password. |
Path Parameters | none | |
Query Parameters | none | |
Headers | none | |
Receives Json | ChangePasswordRequest | |
Returns Json | nothing (204 on success) | A failed login will result in a 401 Unauthorized http status. Any missing parameers will result in a 400 Bad Request http status. Any parameters too long will result in a 400 Bad Request http status |
Example |
Code Block |
---|
theme | DJango |
---|
language | javascript |
---|
linenumbers | true |
---|
| this.changePassword = function (oldPassword, newPassword, options) {
options = options || {};
jQuery.ajax({
url: '/rest/myaccount/changePassword',
data: JSON.stringify({oldPassword: oldPassword, newPassword: newPassword}),
type: 'post',
async: (options.success || options.failure) ? true : false,
headers: { "cache-control": "no-cache" },
cache: false,
contentType: 'application/json; charset=UTF-8',
dataType: 'json'
}).done(function () {
if (options.success) {
options.success();
}
})
.fail(function (jqXHR, textStatus, errorThrown) {
if (options.failure) {
options.failure(jqXHR, textStatus, errorThrown);
}
});
}; |
|
TODO:
/myaccount/changePassword (POST)
...