Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Login with Shopify sdk

    • 1
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 935
    Comment on it

    This is login tutorial using shopify sdk.

    Shopify authenticate any customer based n email id and password and returns customer details if customer already exists in shopify or shop.

    Mobile buy sdk is used to access all the methods of shopify login process like :

    First of all we have to initialize shop before login process :

    buyClient = new BuyClientBuilder()
        .shopDomain(shopUrl)
        .apiKey(shopifyApiKey)
        .appId(shopifyAppId)
        .applicationName(applicationName)
        .interceptors(logging)
        .networkRequestRetryPolicy(3, TimeUnit.MILLISECONDS.toMillis(200), 1.5f)
        .build();
    

    and then we can get shop details.

    AccountCredentials class is used to create credentials for login process, we pass email and password here :

    AccountCredentials mCredentials = new AccountCredentials(etEmail.getText().toString(), etPassword.getText().toString());
    

     

    Xml layout of login screen :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.android.allvino.LoginActivity">
    
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/spacing_6dp">
            <EditText
                android:layout_width="match_parent"
                android:id="@+id/etEmail"
                android:inputType="textEmailAddress"
                android:singleLine="true"
                android:layout_margin="@dimen/padding_ten"
                android:padding="@dimen/padding_ten"
                android:layout_height="wrap_content" />
        </android.support.design.widget.TextInputLayout>
    
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/spacing_6dp">
            <EditText
                android:layout_width="match_parent"
                android:id="@+id/etPassword"
                android:layout_margin="@dimen/padding_ten"
                android:padding="@dimen/padding_ten"
                android:inputType="textPassword"
                android:singleLine="true"
                android:layout_height="wrap_content" />
        </android.support.design.widget.TextInputLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:text="signin"
                android:id="@+id/btnLogin"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
    

    Here we have a singleton class to maintain buy client and other details :

    /**
     * Application class that maintains instances of BuyClient and Checkout for the lifetime of the app.
     */
    public class SampleApplication extends Application {
    
        private static final String SHOP_PROPERTIES_INSTRUCTION ="Add your shop credentials to a shop.properties file in the main app folder";
    
        private static com.android.allvino.SampleApplication.SampleApplication instance;
    
        private static Customer customer;
    
        public static BuyClient getBuyClient() {
            return instance.buyClient;
        }
    
        public static Customer getCustomer() {
            return customer;
        }
    
        public static void setCustomer(Customer customer) {
            com.android.allvino.SampleApplication.SampleApplication.customer = customer;
        }
    
        private BuyClient buyClient;
        private Shop shop;
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            instance = this;
    
            initializeBuyClient();
        }
    
        private void initializeBuyClient() {
            String shopUrl = BuildConfig.SHOP_DOMAIN;
            if (TextUtils.isEmpty(shopUrl)) {
                throw new IllegalArgumentException(SHOP_PROPERTIES_INSTRUCTION + "You must add 'SHOP_DOMAIN' entry in app/shop.properties, in the form '<myshop>.myshopify.com'");
            }
    
            String shopifyApiKey = BuildConfig.API_KEY;
            if (TextUtils.isEmpty(shopifyApiKey)) {
                throw new IllegalArgumentException(SHOP_PROPERTIES_INSTRUCTION + "You must populate the 'API_KEY' entry in app/shop.properties");
            }
    
            String shopifyAppId = BuildConfig.APP_ID;
            if (TextUtils.isEmpty(shopifyAppId)) {
                throw new IllegalArgumentException(SHOP_PROPERTIES_INSTRUCTION + "You must populate the 'APP_ID' entry in app/shop.properties");
            }
    
            String applicationName = getPackageName();
    
            final HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(BuildConfig.OKHTTP_LOG_LEVEL);
    
            /**
             * Create the BuyClient
             */
    
            buyClient = new BuyClientBuilder()
                .shopDomain(shopUrl)
                .apiKey(shopifyApiKey)
                .appId(shopifyAppId)
                .applicationName(applicationName)
                .interceptors(logging)
                .networkRequestRetryPolicy(3, TimeUnit.MILLISECONDS.toMillis(200), 1.5f)
                .build();
    
            buyClient.getShop(new Callback<Shop>() {
                @Override
                public void success(Shop shop) {
                    Log.e("getShop success ",shop.toJsonString());
                    SampleApplication.this.shop = shop;
                }
    
                @Override
                public void failure(BuyClientError error) {
                    Log.e("getShop err ", error.getMessage());
                    Toast.makeText(SampleApplication.this, R.string.shop_error, Toast.LENGTH_LONG).show();
                }
            });
        }
    
        public Shop getShop() {
            return shop;
        }
    
    }
    

    and now in login activity's onCreate :

    AccountCredentials mCredentials = new AccountCredentials(etEmail.getText().toString(), etPassword.getText().toString());

    SampleApplication.getBuyClient().loginCustomer(aAccountCredentials, new Callback<Customer>() {
        @Override
        public void success(final Customer token) {
            Log.e("customer login success :", token.getEmail());
        }
    
        @Override
        public void failure(final BuyClientError error) {
            Log.e("login error:", error.getRetrofitErrorBody());
        }
    });

    So by using this we can login in shopify as a Customer.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: