Register and Log in with Phone Number

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

Commercial Lock App SDK supports registration and login with a mobile phone number and password.

Query country/region for verification code service

To strengthen data security, Tuya has optimized the verification code service and placed limits on accounts. The verification code can only be received by the mobile phone number registered within the covered regions.

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;

Parameter description

Parameter Description
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Objective-C:

[[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;

Parameter description

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

Example

Objective-C:

[[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)")
    }
})

Validate verification code

API description

This method validates the 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;

Parameter description

Parameter Description
userName The mobile phone number or email address.
region The country or region, defaulting to nil.
countryCode The country code, for example, 86.
code The verification code received and entered by the user.
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.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

[[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 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

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

Parameter description

Parameter Description
countryCode The country code, for example, 86.
phoneNumber The mobile phone number.
password The password.
code The verification code received and entered by the user.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

[[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 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;

Parameter description

Parameter Description
countryCode The country code, for example, 86.
phoneNumber The mobile phone number.
password The password.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

[[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 with phone number and verification code

Make an API request to send a verification code to the mobile phone, and then set the code parameter to the received verification code.

API description

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

Parameter description

Parameter Description
mobile The mobile phone number.
countryCode The country code, for example, 86.
code The verification code received and entered by the user.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

[[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 password

To allow users to reset their password, 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;

Parameter description

Parameter Description
countryCode The country code, for example, 86.
phoneNumber The mobile phone number.
newPassword The new password.
code The verification code received and entered by the user.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

- (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

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

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

Parameter description

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

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

Example

Objective-C:

// Get verification code.
[[ThingSmartUser sharedInstance] sendBindingVerificationCodeWithEmail:@"yourEmail" countryCode:@"yourCountryCode" success:^{

} failure:^(NSError *error) {

}];

// Bind email address.
[[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 Login session expiration.