Log in via SMS and Forgot password? Features using Huawei Account Kit in Attendance Tracker Android app (Kotlin) – Part 2
Published: 2022-03-06 08:48:06


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzkroaq6r7qfwez8frfg.jpg)
**Introduction**
In this article, we can find the **Huawei Account Kit** of **Log in via SMS** and **Forgot password?** features in Attendance Tracker app. The Log in via SMS is an alternative way of login with Huawei ID, instead of providing password user can enter SMS code from registered Phone Number. Forgot password? can be used when you forgot your password, you can reset the password using the Huawei ID and SMS code will be sent for verification. So, I will provide the series of articles on this Attendance Tracker App, in upcoming articles I will integrate other Huawei Kits.

**Account Kit**
**Huawei Account Kit** provides for developers with **simple**, **secure**, and **quick sign-in** and authorization functions. User is not required to enter accounts, passwords and waiting for authorization. User can click on **Sign In with HUAWEI ID** button to quickly and securely sign in to the app.

**Requirements**
1. Any operating system (MacOS, Linux and Windows).
2. Must have a Huawei phone with HMS 4.0.0.300 or later.
3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 and above installed.
4. Minimum API Level 24 is required.
5. Required EMUI 9.0.0 and later version devices.

**How to integrate HMS Dependencies**

- First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a [Huawei ID](https://developer.huawei.com/consumer/en/doc/start/registration-and-verification-0000001053628148).
- Create a project in android studio, refer [Creating an Android Studio Project](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#1).
- Generate a **SHA-256 certificate fingerprint**.
- To generate **SHA-256 certificate fingerprint**. On right-upper corner of android project click **Gradle**, choose **Project Name > Tasks > android**, and then click **signingReport**, as follows.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zevcc8czjmz2o1cuc16n.png)

Note: Project Name depends on the user created name.

- [Create an App in AppGallery Connect](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0).
- Download the **agconnect-services.json** file from **App information**, copy and paste in android **Project **under **app **directory, as follows.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ibxp1l0fb1exwrgvuud.png)

- Enter **SHA-256 certificate fingerprint** and click **Save **button, as follows.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3fi1juoy379r3cppnm7.png)

- Click **Manage APIs** tab and enable **Account Kit**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2if64cmq7icxkv2xbudc.jpg)

- Add the below maven URL in **build.gradle(Project)** file under the repositories of **buildscript**, **dependencies** and **allprojects**, refer [Add Configuration](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#6).
`maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'`

- Add the below plugin and dependencies in **build.gradle(Module)** file.
`apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Huawei Account Kit
implementation 'com.huawei.hms:hwid:6.3.0.301'
// Huawei Ads Kit
implementation 'com.huawei.hms:ads:3.4.51.300'`

- Now Sync the gradle.
- Add the required permission to the **AndroidManifest.xml** file.
`// Account Kit
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
// Ads Kit
<uses-permission android:name="android.permission.INTERNET" />`

**Let us move to development**

I have created a project on Android studio with empty activity let us start coding.

In the **MainActivity.kt** we can find the business logic for ads.
`class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize the Huawei Ads SDK
        HwAds.init(this)
        loadDefaultBannerAd()
    }

    // To get Banner view from the activity_main.xml. It will display at bottom of the page.
    private fun loadDefaultBannerAd() {
        val bottomBannerView = findViewById<BannerView>(R.id.hw_banner_view)
        val adParam = AdParam.Builder().build()
        bottomBannerView.adId = getString(R.string.banner_ad_id) // banner_ad_id is testw6vs28auh3
        bottomBannerView.bannerAdSize = BannerAdSize.BANNER_SIZE_SMART
        bottomBannerView.loadAd(adParam)
    }

}`

In the **activity_main.xml** we can create the UI screen.
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.huawei.hms.ads.banner.BannerView
        android:id="@+id/hw_banner_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

</LinearLayout>`

In the **Home.kt** we can find the business logic for logout.
`class Home : AppCompatActivity() {

    private var mAuthManager: AccountAuthService? = null
    private var mAuthParam: AccountAuthParams? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        img_logout.setOnClickListener(mOnClickListener)

    }

    private fun signOut() {
        mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
                     .createParams()
        mAuthManager = AccountAuthManager.getService(this@Home, mAuthParam)
        val signOutTask = mAuthManager?.signOut()
        signOutTask?.addOnSuccessListener {
            Toast.makeText(this, "signOut Success", Toast.LENGTH_SHORT).show()
            val intent = Intent(this@Home, MainActivity::class.java)
            startActivity(intent)
        }?.addOnFailureListener {
            Toast.makeText(this, "signOut fail", Toast.LENGTH_SHORT).show()
        }
    }

    private val mOnClickListener: View.OnClickListener = object : View.OnClickListener {
        override fun onClick(v: View?) {
            when (v?.id) {
                R.id.img_logout -> signOut()
            }
        }
    }

}`

In the **activity_home.xml** we can create the UI screen.
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Home">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="10dp"
        android:layout_marginTop="20dp">
        <ImageView
            android:id="@+id/img_logout"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/logout"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:text="Logout"/>
    </LinearLayout>

</LinearLayout>`

**Demo**

**Log in via SMS**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mklfj3q7rvqh9qdajk8t.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6n4j4imr702j1n0dh141.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwekx330s4wgy1j2z4ta.jpg)

**Forgot password?**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p763kkhfnyhwh11d4wy7.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/liqktil5mr7hanxfkopv.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqel8dqk3m5bl2eh0lj8.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h0t7c2t6m3vwtoalo1a.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8lu67f4wj60py91nk0m.jpg)

**Tips and Tricks**
1. Make sure you are already registered as Huawei developer.
2. Set minSDK version to **24** or later, otherwise you will get **AndriodManifest** merge issue.
3. Make sure you have added the **agconnect-services.json** file to app folder.
4. Make sure you have added **SHA-256 fingerprint** without fail.
5. Make sure all the dependencies are added properly.

**Conclusion**
In this article, we have learned about the **Huawei Account Kit** features of **Log in via SMS** and **Forgot password?** in Attendance Tracker app. The **Log in via SMS** is an alternative way of login with Huawei ID, instead of providing password user can enter SMS code from registered Phone Number. **Forgot password?** can be used when you forgot your password, you can reset the password using the Huawei ID and SMS code will be sent for verification.

I hope you have read this article. If you found it is helpful, please provide likes and comments.

**Reference**
Account Kit – [Documentation](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/get-channel-0000001074587201)
Account Kit – [Training Video](https://developer.huawei.com/consumer/en/training/course/video/101583015541549183)
Ads Kit – [Documentation](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/publisher-service-introduction-0000001070671805)
Ads Kit – [Training Video](https://developer.huawei.com/consumer/en/training/course/video/101582707399567035)