Register and Log in with Phone Number

Last Updated on : 2024-11-22 02:19:31download

Commercial Lock App SDK allows users to register, log in, and reset their password using their mobile phone number.

Send verification code

API description

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

Parameter description

Parameter Description
userName The mobile phone number.
region The region. Default value: "".
countryCode The country code. For example, 86 for mainland China.
type The purpose of the verification code. Valid values:
  • 1: Register with a mobile phone number.
  • 2: Log in with a mobile phone number.
  • 3: Reset the password of an account registered with a mobile phone number.
callback The callback.

Validate verification code

This method validates the verification code that is used for login, registration, or password resetting with a mobile phone number.

API description

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

Parameter description

Parameter Description
userName The username.
region The region. Default value: "".
countryCode The country code. For example, 86 for mainland China.
code The verification code.
type The purpose of the verification code. Valid values:
  • 1: Register with a mobile phone number.
  • 2: Log in with a mobile phone number.
  • 3: Reset the password of an account registered with a mobile phone number.
  • 8: Delete an account registered with a mobile phone number.
callback The callback.

Register with 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

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

Parameter description

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

Example

// Get verification code.
ThingOSUser.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 received", Toast.LENGTH_SHORT).show();
      }
    });
// Register an account with the mobile phone number.
ThingOSUser.getUserInstance().registerAccountWithPhone("86","13666666666","123456","124332", new IRegisterCallback() {
  @Override
  public void onSuccess(User user) {
    Toast.makeText(mContext, "Registration successful", 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 with phone number and password

API description

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

Parameter description

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

Example

// Log in with a mobile phone number.
ThingOSUser.getUserInstance().loginWithPhonePassword("86", "13666666666", "123456", new ILoginCallback() {
  @Override
  public void onSuccess(User user) {
    Toast.makeText(mContext, "You are logged in, username:" +ThingOSUser.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 with phone number and verification code

Make an API request to send a verification code to the mobile phone for login and then verify the verification code. Set the code parameter to the received verification code.

API description

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

Parameter description

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

Example

// Get verification code.
ThingOSUser.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 received", Toast.LENGTH_SHORT).show();
      }
    });
// log in with a mobile phone number.
ThingOSUser.getUserInstance().loginWithPhone("86", "13355555555", "123456", new ILoginCallback() {
  @Override
  public void onSuccess(User user) {
    Toast.makeText(mContext, "You are logged in, username:" +ThingOSUser.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
  }
  @Override
  public void onError(String code, String error) {
    Toast.makeText(mContext, error, Toast.LENGTH_SHORT).show();
  }
});

Reset the password of a phone number account

API description

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

Parameter description

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

Example

// Get verification code.
ThingOSUser.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 received", Toast.LENGTH_SHORT).show();
      }
    });
// Reset password.
ThingOSUser.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

Bind a user account with an email address. Two methods are used in this process: obtaining a verification code for binding and binding an email address.

Get verification code for binding

API description

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

Parameter description

Parameter Description
countryCode The country code. For example, 86 for mainland China.
email The email address.
callback The callback.

Bind email address

API description

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

Parameter description

Parameter Description
countryCode The country code. For example, 86 for mainland China.
email The email address.
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

// Get verification code.
ThingOSUser.getUserInstance().sendBindVerifyCodeWithEmail("86", "123456@123.com", new IResultCallback() {
    @Override
    public void onError(String code, String error) {
        // TODO
    }

    @Override
    public void onSuccess() {
        // TODO
    }
});
// Bind an email address.
ThingOSUser.getUserInstance().bindEmail("86", "123456@123.com", "123456", ThingOSUser.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.