Register and Log in with Email Address

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

Commercial Lock App SDK supports registration and login with an email address and verification code.

Send verification code

API description

Send a verification code to the specified email address for registration.

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

Parameter description

Parameter Description
userName The email address or mobile phone number of the user.
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 or region code. For example, 86 for mainland China.
type The purpose of the verification code. Valid values:
  • 1: Register with an email address.
  • 2: Log in with an email address.
  • 3: Reset the password of an account registered with an email address.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

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

Validate verification code

API description

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

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 an email address.
  • 2: Log in with an email address.
  • 3: Reset the password of an account registered with an email address.
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 email address

To allow users to register an account 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;

Parameter description

Parameter Description
countryCode The country code, for example, 86.
email The email address.
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] 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)")
    }
})

Log in with email address and password

API description

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

Parameter description

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

Parameter description

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 to indicate the cause.

Example

Objective-C:

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

API description

Users can bind an email address with their account after logging in to the app with a mobile phone number. This enables login to the app with an 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;

Parameter description

Parameter Description
email The email address.
countryCode The country code, for example, 86.
code The verification code received and entered by the user.
sId The session ID.
success The success callback.
failure The failure callback. An error message is returned to indicate the cause.

Example

Objective-C:

 [[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 succeed
                                           }];

                                    } failure:^(NSError *error) {

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

Reset password

To allow users to reset the password of an account 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;

Parameter description

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

Bind a mobile phone number with a user account. Two methods are used in this process: obtaining a verification code for binding and binding a mobile phone number.

Get verification code for binding

API description

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

Parameter description

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

API description

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

Parameter description

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

Objective-C:

// Get verification code.
[[ThingSmartUser sharedInstance] sendBindVerifyCode:@"yourVerifyCode" phoneNumber:@"yourPhoneNumber" success:^{

} failure:^(NSError *error) {

}];

// Bind phone number.
[[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)")
    }
}