Register and Login with Phone Number

Last Updated on : 2023-05-31 10:15:47download

Tuya Smart Life App SDK supports registration and login with a user’s mobile phone number and password.

Query areas for verification code service

To strengthen data security, Tuya has optimized the verification code service and placed limits on accounts. The verification code service is available only in limited areas.

To enable verification with mobile phone SMS messages, you must subscribe to and configure the service Verify with Mobile Phone SMS. After this service is enabled, users can register and log in with an app account or reset the password using their mobile phone number. They can also bind their mobile phone number with the app. For more information, see Subscribe to and Configure SMS Verification.

API description

ThingHomeSdk.getUserInstance().getWhiteListWhoCanSendMobileCodeSuccess(IWhiteListCallback callback);

Parameters

Parameter Description
callback The callback.

Example

ThingHomeSdk.getUserInstance().getWhiteListWhoCanSendMobileCodeSuccess(new IWhiteListCallback() {
      @Override
      public void onSuccess(WhiteList whiteList) {
        Toast.makeText(mContext, "Support for the verification code service is checked successfully:" + whiteList.getCountryCodes(), Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onError(String code, String error) {
        Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
      }
    });

Send verification code

API description

ThingHomeSdk.getUserInstance().sendVerifyCodeWithUserName(String userName, String region, String countryCode, int type, IResultCallback callback);

Parameters

Parameter Description
userName The mobile phone number.
region The area in which the verification code service is available for the account. Default value: "".
countryCode The country code. For example, 86 means mainland China.
type The purpose of the verification code. Valid values:
  • 1: register an account with a mobile phone number
  • 2: login to the app with a mobile phone number
  • 3: reset the password of an account that is registered with a mobile phone number
callback The callback.

Verify verification code

Verifies a verification code that is used for login, registration, or password resetting with a mobile phone number.

API description

ThingHomeSdk.getUserInstance().checkCodeWithUserName(String userName, String region, String countryCode, String code, int type, IResultCallback callback)

Parameters

Parameter Description
userName The username.
region The area in which the verification code service is available for the account. Default value: "".
countryCode The country code. For example, 86 means mainland China.
code The verification code.
type The purpose of the verification code. Valid values:
  • 1: register an account with a mobile phone number
  • 2: login to the app with a mobile phone number
  • 3: reset the password of an account that is registered with a mobile phone number
  • 8: unregister an account that is registered with a mobile phone number
callback The callback.

Register account with mobile phone number

To allow users to register an account with a mobile phone number, you must make an API request to send a verification code.

API description

ThingHomeSdk.getUserInstance().registerAccountWithPhone(final String countryCode, final String phoneNumber, final String passwd, final String code, final IRegisterCallback callback);

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
phoneNumber The mobile phone number.
passwd The password.
code The verification code.
callback The callback.

Example

// Returns a verification code to a mobile phone.
ThingHomeSdk.getUserInstance().sendVerifyCodeWithUserName("13666666666", "", "86", 1, new IResultCallback() {
      @Override
      public void onError(String code, String error) {
        Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onSuccess() {
        Toast.makeText(mContext, "Verification code returned successfully.", Toast.LENGTH_SHORT).show();
      }
    });
// Registers an account with a mobile phone number and a password.
ThingHomeSdk.getUserInstance().registerAccountWithPhone("86","13666666666","123456","124332", new IRegisterCallback() {
  @Override
  public void onSuccess(User user) {
    Toast.makeText(mContext, "Registered successfully.", Toast.LENGTH_SHORT).show();
  }
  @Override
  public void onError(String code, String error) {
    Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
  }
});

Log in to the app with the mobile phone number and password

API description

ThingHomeSdk.getUserInstance().loginWithPhonePassword(String countryCode, String phone, String passwd, final ILoginCallback callback);

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
phone The mobile phone number.
passwd The password for login to the app.
callback The callback.

Example

// Enables login to the app with the mobile phone number and password.
ThingHomeSdk.getUserInstance().loginWithPhonePassword("86", "13666666666", "123456", new ILoginCallback() {
  @Override
  public void onSuccess(User user) {
    Toast.makeText(mContext, "Logged in successfully. Username:" +ThingHomeSdk.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onError(String code, String error) {
    Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
  }
});

Log in to app with phone number and verification code

To enable login with a mobile phone number and a verification code, you must make an API request to send a verification code to the mobile phone for login and then make an API request to verify the verification code. During the latter call, the user enters the returned verification code to pass the verification.

API description

ThingHomeSdk.getUserInstance().loginWithPhone(String countryCode, String phone, String code, final ILoginCallback callback)

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
phone The mobile phone number.
code The verification code.
callback The callback.

Example

// Returns a verification code to a mobile phone.
ThingHomeSdk.getUserInstance().sendVerifyCodeWithUserName("13666666666", "", "86", 2, new IResultCallback() {
      @Override
      public void onError(String code, String error) {
        Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onSuccess() {
        Toast.makeText(mContext, "Verification code returned successfully.", Toast.LENGTH_SHORT).show();
      }
    });
// Enables login with the verification code.
ThingHomeSdk.getUserInstance().loginWithPhone("86", "13355555555", "123456", new ILoginCallback() {
  @Override
  public void onSuccess(User user) {
    Toast.makeText(mContext, "Logged in successfully. Username:" +ThingHomeSdk.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
  }
  @Override
  public void onError(String code, String error) {
    Toast.makeText(mContext, error, Toast.LENGTH_SHORT).show();
  }
});

Reset account password

API description

ThingHomeSdk.getUserInstance().resetPhonePassword(final String countryCode, final String phone, final String code, final String newPasswd, final IResetPasswordCallback callback);

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
phone The mobile phone number.
code The verification code.
newPasswd The new password.
callback The callback.

Example

// Returns a verification code to a mobile phone.
ThingHomeSdk.getUserInstance().sendVerifyCodeWithUserName("13666666666", "", "86", 3, new IResultCallback() {
      @Override
      public void onError(String code, String error) {
        Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onSuccess() {
        Toast.makeText(mContext, "Verification code returned successfully.", Toast.LENGTH_SHORT).show();
      }
    });
// Resets the password for login with the mobile phone number.
ThingHomeSdk.getUserInstance().resetPhonePassword("86", "13555555555", "123456", "123123", new IResetPasswordCallback(){
  @Override
  public void onSuccess() {
    Toast.makeText(mContext, "Password reset successfully.", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onError(String code, String error) {
    Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
  }
});

Bind email address with account

Enables binding an email address with an app account and includes the APIs for the verification code service and email address binding.

Get verification code for binding

API description

ThingHomeSdk.getUserInstance().sendBindVerifyCodeWithEmail(@NonNull String countryCode, @NonNull String email, @NonNull IResultCallback callback);

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
email The email address of the user.
callback The callback.

Bind email address

API description

ThingHomeSdk.getUserInstance().bindEmail(@NonNull String countryCode, @NonNull String email, @NonNull String code, @NonNull String sId, @NonNull IResultCallback callback);

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
email The email address of the user.
code The verification code.
sId The login session ID of the user. You can query the user object to get the value of sessionID.
callback The callback.

Example

// Returns a verification code for binding.
ThingHomeSdk.getUserInstance().sendBindVerifyCodeWithEmail("86", "123456@123.com", new IResultCallback() {
	@Override
    public void onError(String code, String error) {
    	// TODO
    }

    @Override
    public void onSuccess() {
		// TODO
    }
});
// Binds the email address with the account.
ThingHomeSdk.getUserInstance().bindEmail("86", "123456@123.com", "123456", ThingHomeSdk.getUserInstance().getUser().getSid(), new IResultCallback() {
	@Override
	public void onError(String code, String error) {
		// TODO
	}

	@Override
	public void onSuccess() {
		// TODO
	}
});

If an account is logged in on different mobile phones, after the password is reset with one of the phones, the app on other phones triggers the callback of session expiration. You must implement the actions after the callback. For example, navigate to the login page. For more information, see Login session expiration.