Customer Accounts

The Mobile Buy SDK provides support for letting customers create and log in to accounts on your shop. Being logged in allows customers to see their past orders, and streamlines the web checkout process, by using their existing address information.

For detailed examples of how to handle customer accounts, please see the sample app.

Signing up

// Create credential items with email, first name, last name,
// password and password confirmation
final AccountCredentials accountCredentials = new AccountCredentials("user@example.com", "password123", "password123", "afirstname", "alastname");

// The customer will be retrieved automatically if the sign up was successful
buyClient.createCustomer(accountCredentials, new Callback<Customer>() {
            @Override
            public void success(Customer customer) {
                // save the customer or token for later use
            }

            @Override
            public void failure(BuyClientError error) {
                // handle error
            }
        });

Logging in

Logging in creates a temporary authentication token. The token is used to verify access to protected customer data. Once the token is created, the BuyClient keeps a copy, which it submits with every following request.

// Create credential items with email and password
final AccountCredentials accountCredentials = new AccountCredentials("user@example.com", "password123");

buyClient.loginCustomer(accountCredentials, new Callback<Customer>() {
            @Override
            public void success(Customer customer) {
                // save the customer or token for later use
            }

            @Override
            public void failure(BuyClientError error) {
                // handle error
            }
        });     

Save the token if you want to persist authenticated sessions across application launches. The token can be retrieved from the BuyClient by:

String token = buyClient.getCustomerToken();

The token must be set on any BuyClient objects that you want to use to access Customer data. The token can be set when the BuyClient is built, or set later using:

buyClient.setCustomerToken(token);

If the token is not set and an attempt is made to access Customer data, a BuyClientError will be returned, with a IllegalStateException as the content.

Ensuring the user stays logged in

You can delay the expiration of a login session by renewing the associated token. The value of the token may change, so be sure to refresh your stored version. You should renew the token periodically. A reasonable strategy is to renew every time the app becomes active.

buyClient.renewCustomer(new Callback<CustomerToken>() {
            @Override
            public void success(CustomerToken response) {
                // save the token for later use
            }

            @Override
            public void failure(BuyClientError error) {
                // handle error
            }
        });

Logging out

Logging out is synonymous with deleting the user's authentication token. You should always log out via the API.

If log out is successful, delete the token from any storage that you have used to persist the token. The BuyClient will automatically delete its copy of the token if log out succeeds. If log out fails because the token has already expired, then this will not be reported as an error.

       buyClient.logoutCustomer(new Callback<Void>() {
            @Override
            public void success(Void response) {
                // delete local in-memory and cached copies of the token, update UI
            }

            @Override
            public void failure(BuyClientError error) {
                // handle error
            }
        });

Customer activation

Customers that complete a checkout through the web and enable "remember my details" will have an account created automatically. They will receive an email from Shopify letting them know this. The email will include an account activation link. This link contains a token that you can use to activate their account.

You will need to extract the customer id and the activation token from the activation link URL, and pass those to the API. The customer will be logged in automatically on successful activation.

The format of the activation link URL is: http://domain.myshopify.com/account/activate/CUSTOMER_ID/ACTIVATION_TOKEN

final AccountCredentials accountCredentials = new AccountCredentials(password);

buyClient.activateCustomer(CUSTOMER_ID, ACTIVATION_TOKEN, accountCredentials, new Callback<Customer>() {
            @Override
            public void success(Customer response) {
                // save the token for later use
            }

            @Override
            public void failure(BuyClientError error) {
                // handle error
            }
        });