Last Updated on : 2024-11-18 11:33:12download
Tuya Smart Life App SDK supports registration and login with a user’s mobile phone number and password.
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
TuyaHomeSdk.getUserInstance().getWhiteListWhoCanSendMobileCodeSuccess(IWhiteListCallback callback);
Parameters
Parameter | Description |
---|---|
callback | The callback. |
Example
TuyaHomeSdk.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();
}
});
This API method has been updated starting from the SDK version 3.25.0. If you are using an earlier version, you can update the version. For more information, see Update to v3.25.0.
API description
TuyaHomeSdk.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:
|
callback | The callback. |
Verifies a verification code that is used for login, registration, or password resetting with a mobile phone number.
API description
TuyaHomeSdk.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:
|
callback | The callback. |
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
TuyaHomeSdk.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.
TuyaHomeSdk.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.
TuyaHomeSdk.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();
}
});
API description
TuyaHomeSdk.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.
TuyaHomeSdk.getUserInstance().loginWithPhonePassword("86", "13666666666", "123456", new ILoginCallback() {
@Override
public void onSuccess(User user) {
Toast.makeText(mContext, "Logged in successfully. Username:" +TuyaHomeSdk.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();
}
});
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
TuyaHomeSdk.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.
TuyaHomeSdk.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.
TuyaHomeSdk.getUserInstance().loginWithPhone("86", "13355555555", "123456", new ILoginCallback() {
@Override
public void onSuccess(User user) {
Toast.makeText(mContext, "Logged in successfully. Username:" +TuyaHomeSdk.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, error, Toast.LENGTH_SHORT).show();
}
});
API description
TuyaHomeSdk.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.
TuyaHomeSdk.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.
TuyaHomeSdk.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();
}
});
Enables binding an email address with an app account and includes the APIs for the verification code service and email address binding.
API description
TuyaHomeSdk.getUserInstance().sendBindVerifyCodeWithEmail(@NonNull String countryCode, @NonNull String email, @NonNull IResultCallback callback);
Parameters
Parameter | Description |
---|---|
countryCode | The country code. For example, 86 means mainland China. |
The email address of the user. | |
callback | The callback. |
API description
TuyaHomeSdk.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. |
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.
TuyaHomeSdk.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.
TuyaHomeSdk.getUserInstance().bindEmail("86", "123456@123.com", "123456", TuyaHomeSdk.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.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback