Register and Login with Phone Number

Last Updated on : 2023-04-17 08:31:00download

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.

- (void)getWhiteListWhoCanSendMobileCodeSuccess:(ThingSuccessString)success
                                        failure:(ThingFailureError)failure;

Parameters

Parameter Description
success The success callback.
failure The failure callback. An error message is returned.

ObjC:

[[ThingSmartUser sharedInstance] getWhiteListWhoCanSendMobileCodeSuccess:^(NSString *regions) {

} failure:^(NSError *error) {

}];

Swift:

ThingSmartUser.sharedInstance().getWhiteListWhoCanSendMobileCodeSuccess({ regions in

}, failure: { error in

})

Send verification code

Make sure that you can call this API method. You can call getWhiteListWhoCanSendMobileCodeSuccess to check your permissions.

API description

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

- (void)sendVerifyCodeWithUserName:(NSString *)userName
                            region:(NSString *)region
                       countryCode:(NSString *)countryCode
                              type:(NSInteger)type
                           success:(ThingSuccessHandler)success
                           failure:(ThingFailureError)failure;

Parameters

Parameter Description
userName The mobile phone number.
region The area in which an account is registered. You can call [ThingSmartUser regionListWithCountryCode:success:failure:] or [ThingSmartUser getDefaultRegionWithCountryCode:] to get the value.
countryCode The country code, such as 86.
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
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[ThingSmartUser sharedInstance] sendVerifyCodeWithUserName:@"yourUsename"
                                                    region:region
                                               countryCode:@"yourCountryCode"
                                                      type:1
                                                   success:^{
    NSLog(@"sendVerifyCode success");
} failure:^(NSError *error) {
    NSLog(@"sendVerifyCode failure: %@", error);
}];

Swift:

ThingSmartUser.sharedInstance().sendVerifyCode(withUserName: "yourUsename", region: region, countryCode: "yourCountryCode", type: 1, success: {
    print("sendVerifyCode success")
}, failure: { error in
    if let error = error {
        print("sendVerifyCode failure: \(error)")
    }
})

Verify verification code

API description

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

- (void)checkCodeWithUserName:(NSString *)userName
                       region:(NSString *_Nullable)region
                  countryCode:(NSString *)countryCode
                         code:(NSString *)code
                         type:(NSInteger)type
                      success:(ThingSuccessBOOL)success
                      failure:(ThingFailureError)failure;

Parameters

Parameter Description
userName The mobile phone number or email address used as the username.
region The area in which the account is registered. Default value: nil.
countryCode The country code, such as 86.
code The returned 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
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[ThingSmartUser sharedInstance] checkCodeWithUserName:@"email_or_phone_number" region:@"region" countryCode:@"your_country_code" code:@"verify_code" type:1 success:^(BOOL result) {
		if (result) {
				NSLog(@"valid code!");
    } else {
				NSLog(@"invalid code!");
    }
} failure:^(NSError *error) {
		NSLog(@"check code failure: %@", error);
}];

Swift:

ThingSmartUser.sharedInstance()?.checkCode(withUserName: "email_or_phone_number", region: "region", countryCode: "your_country_code", code: "verify_code", type: type, success: { (result) in
		if result {
				print("valid code!")
		} else {
				print("invalid code!")
		}
}, failure: { (error) in
		if let error = error {
				print("check code failure: \(error)")
		}
})

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

- (void)registerByPhone:(NSString *)countryCode
            phoneNumber:(NSString *)phoneNumber
               password:(NSString *)password
                   code:(NSString *)code
                success:(nullable ThingSuccessHandler)success
                failure:(nullable ThingFailureError)failure;

Parameters

Parameter Description
countryCode The country code, such as 86.
phoneNumber The mobile phone number.
password The password.
code The returned verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[ThingSmartUser sharedInstance] registerByPhone:@"your_country_code" phoneNumber:@"your_phone_number" password:@"your_password" code:@"verify_code" success:^{
    NSLog(@"register success");
} failure:^(NSError *error) {
    NSLog(@"register failure: %@", error);
}];

Swift:

ThingSmartUser.sharedInstance()?.register(byPhone: "your_country_code", phoneNumber: "your_phone_number", password: "your_password", code: "verify_code", success: {
    print("register success")
}, failure: { (error) in
    if let e = error {
        print("register failure: \(e)")
    }
})

Log in to app with phone number and password

API description

- (void)loginByPhone:(NSString *)countryCode
         phoneNumber:(NSString *)phoneNumber
            password:(NSString *)password
             success:(nullable ThingSuccessHandler)success
             failure:(nullable ThingFailureError)failure;

Parameters

Parameter Description
countryCode The country code, such as 86.
phoneNumber The mobile phone number.
password The password.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[ThingSmartUser sharedInstance] loginByPhone:@"your_country_code" phoneNumber:@"your_phone_number" password:@"your_password" success:^{
		NSLog(@"login success");
} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
}];

Swift:

ThingSmartUser.sharedInstance()?.login(byPhone: "your_country_code", phoneNumber: "your_phone_number", password: "your_password", success: {
    print("login success")
}, failure: { (error) in
    if let e = error {
        print("login failure: \(e)")
    }
})

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 entered verification code.

API description

- (void)loginWithMobile:(NSString *)mobile
            countryCode:(NSString *)countryCode
                   code:(NSString *)code
                success:(ThingSuccessHandler)success
                failure:(ThingFailureError)failure;

Parameters

Parameter Description
mobile The mobile phone number.
countryCode The country code, such as 86.
code The returned verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[ThingSmartUser sharedInstance] loginWithMobile:@"your_phone_number" countryCode:@"your_country_code" code:@"verify_code" success:^{
		NSLog(@"login success");
} failure:^(NSError *error) {
    NSLog(@"login failure: %@", error);
}];

Swift:

ThingSmartUser.sharedInstance()?.login(withMobile: "your_phone_number", countryCode: "your_country_code", code: "verify_code", success: {
    print("login success")
}, failure: { (error) in
    if let e = error {
        print("login failure: \(e)")
    }
})

Reset account password

To allow users to reset the password of an account that is registered with a mobile phone number, you must make an API request to send a verification code.

API description

- (void)resetPasswordByPhone:(NSString *)countryCode
                 phoneNumber:(NSString *)phoneNumber
                 newPassword:(NSString *)newPassword
                        code:(NSString *)code
                     success:(nullable ThingSuccessHandler)success
                     failure:(nullable ThingFailureError)failure;

Parameters

Parameter Description
countryCode The country code, such as 86.
phoneNumber The mobile phone number.
newPassword The new password.
code The returned verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

- (void)resetPasswordByPhone {
	[ThingSmartUser sharedInstance] resetPasswordByPhone:@"your_country_code" phoneNumber:@"your_phone_number" newPassword:@"your_password" code:@"verify_code" success:^{
		NSLog(@"resetPasswordByPhone success");
	} failure:^(NSError *error) {
		NSLog(@"resetPasswordByPhone failure: %@", error);
	}];
}

Swift:

func resetPasswordByPhone() {
    ThingSmartUser.sharedInstance()?.resetPassword(byPhone: "your_country_code", phoneNumber: "your_phone_number", newPassword: "your_password", code: "verify_code", success: {
        print("resetPasswordByPhone success")
    }, failure: { (error) in
        if let e = error {
            print("resetPasswordByPhone failure: \(e)")
        }
    })
}

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

- (void)sendBindingVerificationCodeWithEmail:(NSString *)email
                                 countryCode:(NSString *)countryCode
                                     success:(nullable ThingSuccessHandler)success
                                     failure:(nullable ThingFailureError)failure;

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
email The email address of the user.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Bind email address

API description

- (void)bindEmail:(NSString *)email
  withCountryCode:(NSString *)countryCode
             code:(NSString *)code
              sId:(NSString *)sId
          success:(nullable ThingSuccessHandler)success
          failure:(nullable ThingFailureError)failure;

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.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

ObjC:

// Returns a verification code for binding.
[[ThingSmartUser sharedInstance] sendBindingVerificationCodeWithEmail:@"yourEmail" countryCode:@"yourCountryCode" success:^{

} failure:^(NSError *error) {

}];

// Binds the email address with the account.
[[ThingSmartUser sharedInstance] bindEmail:@"yourEmail" withCountryCode:@"yourCountryCode" code:@"yourVerifyCode" sId:@"yourSId" success:^{

} failure:^(NSError *error) {

}];

Swift:

ThingSmartUser.sharedInstance().sendBindingVerificationCode(withEmail: "yourEmail", countryCode: "yourCountryCode") {
    print("login success")
} failure: { error in
    if let e = error {
        print("login failure: \(e)")
    }
}

ThingSmartUser.sharedInstance().bindEmail("yourEmail", withCountryCode: "yourCountryCode", code: "yourVerifyCode", sId: "sId") {
    print("login success")
} failure: { error in
    if let e = error {
        print("login failure: \(e)")
    }
}

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 Process session expiration.