User Management

Last Updated on : 2024-06-12 10:22:54download

Tuya Smart Community App SDK supports login to a mobile app with a password.

To enhance data security, Tuya has optimized the verification code service for mobile phones and placed limits on users. To use the verification code service, contact your account manager of Tuya or submit a ticket to activate the service.

Register a user with a mobile phone number

The SDK enables registration with the mobile phone number and password and includes the APIs for the verification code service and user registration.

Get a verification code

API description

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

Example

ObjC:

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

Swift:

TuyaSmartUser.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)")
    }
})

Parameters

Parameter Description
userName The mobile phone number.
region The region in which the verification code service is available for the user. Default value: "".
countryCode The country code. Set the value to 86 to specify mainland China.
type The type of verification code. Valid values:
  • 1: registration
  • 3: password resetting
callback The callback.

Register with the mobile phone number and password

API description

Register with the mobile phone number and password

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

Parameters

Parameter Description
countryCode The country code. Set the value to 86 to specify mainland China.
phoneNumber The mobile phone number.
passwd The login password.
code The verification code.
callback The callback.

Example

ObjC:

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

TuyaSmartUser.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 a user with a mobile phone number

API description

Enables login to the app with a mobile phone number and a password.

- (void)loginByPhone:(NSString *)countryCode
       phoneNumber:(NSString *)phoneNumber
          password:(NSString *)password
           success:(nullable TYSuccessHandler)success
           failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code. Set the value to 86 to specify mainland China.
phone The mobile phone number.
passwd The login password.
callback The callback.

Example

ObjC:

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

Swift:

TuyaSmartUser.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)")
    }
})

Reset the password

Implement the following two steps: get the verification code + reset the password. For more information about how to get the verification code, see the API description of sendVerifyCodeWithUserName.

API description

Resets the password by using a mobile phone

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

Parameters

Parameter Description
countryCode The country code. Set the value to 86 to specify mainland China.
phone The mobile phone number.
code The verification code.
newPasswd The new password.
callback The callback.

Example

ObjC:

- (void)resetPasswordByPhone {
	[TuyaSmartUser 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() {
    TuyaSmartUser.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)")
        }
    })
}

Send different types of verification codes

Users can get different types of verification codes depending on their purposes.

API description

Verifies the validity of a verification code that can be used for login, registration, or password resetting.

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

Parameters

Parameter Description
userName The user’s mobile phone number.
region The region in which the verification code service is available for the user. Default value: "".
countryCode The country code. Set the value to 86 to specify mainland China.
code The verification code.
type The purpose of a verification code. Valid values:
  • 1: registration
  • 2: login
  • 3: password resetting
  • 8: user deletion
callback The callback.

Example

ObjC:

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

TuyaSmartUser.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)")
		}
})

User management

Tuya IoT Platform supports user management. For example, bind users, modify user information, process session expiration, and delete users.

Change a user avatar

API description

Uploads a custom user avatar.

- (void)updateHeadIcon:(UIImage *)headIcon
               success:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
file The user avatar file.
callback The callback.

Example

ObjC:

- (void)updateHeadIcon:(UIImage *)headIcon {
	[[TuyaSmartUser sharedInstance] updateHeadIcon:headIcon success:^{
		NSLog(@"update head icon success");
	} failure:^(NSError *error) {
		NSLog(@"update head icon failure: %@", error);
	}];
}

Swift:

func updateHeadIcon(_ headIcon: UIImage) {
    TuyaSmartUser.sharedInstance()?.updateHeadIcon(headIcon, success: {
        print("update head icon success")
    }, failure: { (error) in
        if let e = error {
            print("update head icon failure: \(e)")
        }
    })
}

Change a nickname

API description

Changes a nickname.

- (void)updateNickname:(NSString *)nickName
               success:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
name The nickname.
callback The callback.

Example

ObjC:

- (void)modifyNickname:(NSString *)nickname {
	[[TuyaSmartUser sharedInstance] updateNickname:nickname success:^{
		NSLog(@"updateNickname success");
	} failure:^(NSError *error) {
		NSLog(@"updateNickname failure: %@", error);
	}];
}

Swift:

func modifyNickname(_ nickName: String) {
    TuyaSmartUser.sharedInstance()?.updateNickname(nickName, success: {
        print("updateNickname success")
    }, failure: { (error) in
        if let e = error {
            print("updateNickname failure: \(e)")
        }
    })
}

Synchronize user information

If user information such as the avatar or nickname is changed, this API method is used to synchronize the latest user information. If the user is logged in from multiple mobile phones and one of the mobile phones has user information changed, the changes are synchronized to the other mobile phone when the user information is checked on the mobile phone.

API description

Synchronizes user information.

- (void)updateUserInfo:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
callback The callback.

Example

- (void)updateUserInfo {
    [[TuyaSmartUser sharedInstance] updateUserInfo:^{
		NSLog(@"update userInfo success");
	} failure:^(NSError *error) {
		NSLog(@"update userInfo failure: %@", error);
	}];
}
func updateUserInfo() {
    TuyaSmartUser.sharedInstance()?.updateUserInfo({
        print("updateNickname success")
    }, failure: { (error) in
        if let e = error {
            print("updateNickname failure: \(e)")
        }
    })
}

Log out

API description

Logs out of the current user and switches to another user.

- (void)loginOut:(nullable TYSuccessHandler)success
         failure:(nullable TYFailureError)failure;

Parameters

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

Example

ObjC:

- (void)loginOut {
	[TuyaSmartUser sharedInstance] loginOut:^{
		NSLog(@"logOut success");
	} failure:^(NSError *error) {
		NSLog(@"logOut failure: %@", error);
	}];
}

Swift:

func loginOut() {
    TuyaSmartUser.sharedInstance()?.loginOut({
        print("logOut success")
    }, failure: { (error) in
        if let e = error {
            print("logOut failure: \(e)")
        }
    })
}

Delete a user

API description

Deletes a user. During the week following this delete operation, if the user is logged in again, the delete request is canceled. If not, the user is permanently disabled and all its information is deleted after this week.

- (void)cancelAccount:(nullable TYSuccessHandler)success
              failure:(nullable TYFailureError)failure;

Parameters

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

Example

ObjC:

- (void)cancelAccount {
	[TuyaSmartUser sharedInstance] cancelAccount:^{
		NSLog(@"cancel account success");
	} failure:^(NSError *error) {
		NSLog(@"cancel account failure: %@", error);
	}];
}

Swift:

func cancelAccount() {
    TuyaSmartUser.sharedInstance()?.cancelAccount({
        print("cancel account success")
    }, failure: { (error) in
        if let e = error {
            print("cancel account failure: \(e)")
        }
    })
}