Register and Login with Email Address

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

Tuya Smart Life App SDK supports login with a verification code that is sent to a user’s email address.

Send a verification code

API description

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

Parameters

Parameter Description
userName The email address or mobile phone number of the user.
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. For example, 86 means mainland China.
type The purpose of the verification code. Valid values:
  • 1: register an account with an email address
  • 2: login to the app with an email address
  • 3: reset the password of an account that is registered with an email address
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

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

Verify a verification code

API description

Verifies a verification code that is used for login, registration, or password resetting with an email address.

- (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.
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 an email address
  • 2: login to the app with an email address
  • 3: reset the password of an account that is registered with an email address
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 an account with an email address

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

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address of the user.
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] registerByEmail:@"your_country_code" email:@"your_email" password:@"your_password" code:@"verify_code" success:^{
    NSLog(@"register success");
} failure:^(NSError *error) {
    NSLog(@"register failure: %@", error);
}];

Swift:

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

Login to the account with the email address and password

API description

- (void)loginByEmail:(NSString *)countryCode
               email:(NSString *)email
            password:(NSString *)password
             success:(nullable TYSuccessHandler)success
             failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address of the user.
password The password.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

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

Log in to the app with the email address and verification code

To allow users to log in with an email address, you must make an API request to send a verification code.

API description

- (void)loginWithEmail:(NSString *)email
           countryCode:(NSString *)countryCode
                  code:(NSString *)code
               success:(ThingSuccessHandler)success
               failure:(ThingFailureError)failure;

Parameters

Parameter Description
email The email address.
countryCode The country code.
code The verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[ThingSmartUser sharedInstance] loginWithEmail:@"your_email" countryCode:@"your_country_code" code:@"your_code" success:^{
    NSLog(@"sendVerifyCode success");
} failure:^(NSError *error) {
    NSLog(@"sendVerifyCode failure: %@", error);
}];

Swift:

ThingSmartUser.sharedInstance().login(withEmail: "your_email", countryCode: "your_country_code", code: "your_code") {
    print("login success")
} failure: { error in
    if let e = error {
        print("login failure: \(e)")
    }
}

Bind an email address with the account

API description

After users log in to the app with their mobile phone numbers, they can bind email addresses to their accounts. This enables login to the app with the email address. To enable this binding, you must make an API request to send a verification code.

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

Parameters

Parameter Description
email The email address of the user.
countryCode The country code, such as 86.
code The returned verification code.
sId session Id
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

 [[ThingSmartUser sharedInstance] sendBindingVerificationCodeWithEmail:userName
                                                          countryCode:countryCode
                                                              success:^{
                                                 // send success
                                                            } failure:^(NSError *error) {

                                                                NSLog(@"sendBindingVerificationCodeWithEmail failure: %@", error);
                                                           }
];

[[ThingSmartUser sharedInstance] bindEmail:account
                          withCountryCode:countryCode
                                     code:code
                                      sId:[ThingSmartUser sharedInstance].sid
                                  success:^{
                                                 // bind successd
                                           }];

                                    } failure:^(NSError *error) {

                                          NSLog(@"bind failure: %@", error);
                                    }];

Reset account password

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

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address of the user.
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)resetPasswordByEmail {
    [[ThingSmartUser sharedInstance] resetPasswordByEmail:@"your_country_code" email:@"your_email" newPassword:@"your_password" code:@"verify_code" success:^{
        NSLog(@"resetPasswordByEmail success");
    } failure:^(NSError *error) {
        NSLog(@"resetPasswordByEmail failure: %@", error);
    }];
}

Swift:

func resetPasswordByEmail() {
    ThingSmartUser.sharedInstance()?.resetPassword(byEmail: "your_country_code", email: "your_email", newPassword: "your_password", code: "verify_code", success: {
        print("resetPasswordByEmail success")
    }, failure: { (error) in
        if let e = error {
            print("resetPasswordByEmail failure: \(e)")
        }
    })
}

Bind phone number with account

Enables binding a phone number with an app account and includes the APIs for the verification code service and phone number binding.

Get verification code for binding

API description

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

Parameters

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

Bind phone number with account

API description

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

Parameters

Parameter Description
countryCode The country code. For example, 86 means mainland China.
phoneNumber The mobile phone number.
code The verification code.
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] sendBindVerifyCode:@"yourVerifyCode" phoneNumber:@"yourPhoneNumber" success:^{

} failure:^(NSError *error) {

}];
// Binds the mobile phone number with the account.
[[ThingSmartUser sharedInstance] mobileBinding:@"yourCountryCode" phoneNumber:@"yourPhoneNumber" code:@"yourVerifyCode" success:^{

} failure:^(NSError *error) {

}];

Swift:

ThingSmartUser.sharedInstance().sendBindVerifyCode("yourCountryCode", phoneNumber: "yourPhoneNumber") {
    print("login success")
} failure: { error in
    if let e = error {
        print("login failure: \(e)")
    }
}

ThingSmartUser.sharedInstance().mobileBinding("yourCountryCode", phoneNumber: "yourPhoneNumber", code: "yourVerifyCode") {
    print("login success")
} failure: { error in
    if let e = error {
        print("login failure: \(e)")
    }
}