User Properties¶
When a user registers with an application, he provides information which establishes user's identity. For one application these properties may include name, phone number and email address, for another it could be user id, age and gender. Backendless User Service allows each application to have a custom set of properties associated with the user entity.
Defining properties with the API¶
User properties must be defined in the object representing a user. When user registers or user object is updated, Backendless saves the property values in the Users
table of Backendless Data service. If a property is not declared on the server-side, Backendless automatically creates a definition for it. This behavior can be turned off/on using the Dynamic Schema Definition configuration setting in the console (select Data > Configuration). The setting is turned on by default:
Set/Update User Property¶
Important
When setting or updating a user property, the values must be of "primitive" data types (boolean, string, number, date). To assign a non-primitive value, use the Data relation API .
The following API can be used to set or update properties in a user object:
Methods to set/add user properties are available in the BackendlessUser
class. When registering a new user, the methods below can be used on a new instance of BackendlessUser
. When updating an existing user, the instance must be retrieved from the server (either via login or standard data object retrieval).
// sets the new property to the user object
- (void)setPropertyWithPropertyName:(NSString * _Nonnull)name propertyValue:(id _Nonnull)value;
// clears (removes) all user properties and replaces them with the specified collection
- (void)setPropertiesWithProperties:(NSDictionary<NSString *,id> * _Nonnull)properties;
// sets the new property to the user object
func addProperty(propertyName: String, propertyValue: Any)
// clears (removes) all user properties and replaces them with the specified collection
func setProperties(properties: [String : Any])
Retrieve User Property Values¶
The following API can be used to get or delete user property values:
// retrieves value of a property with the specified name
- (id)getPropertyWithPropertyName:(NSString * _Nonnull)name;
// retrieves all properties for the user object
- (NSDictionary<String*, id> *)getProperties;
// removes the property identified by the name
- (void)removePropertyWithPropertyName:(NSString * _Nonnull)name;
// removes all properties identified by names
- (void)removePropertiesWithPropertiesToRemove:(NSArray<NSString *> * _Nonnull)propertyNames;
// retrieves value of a property with the specified name
func getProperty(propertyName: String) -> Any?
// retrieves all properties for the user object
func getProperties() -> [String: Any]
// removes the property identified by the name
func removeProperty(propertyName: String)
// removes all properties identified by names
func removeProperties(propertiesToRemove: [String])
Example¶
The example below describes how to retrieve the phoneNumber
user property.
[Backendless.shared.userService loginWithIdentity:@"spidey@backendless.com" password:@"greeng0blin" responseHandler:^(BackendlessUser *loggedInUser) {
NSString *phoneNumber = [loggedInUser getPropertyWithPropertyName:@"phoneNumber"];
if (phoneNumber) {
NSLog(@"User's email: %@, phoneNumber: %@", loggedInUser.email, phoneNumber);
}
} errorHandler:^(Fault *fault) {
NSLog(@"Error: %@", fault.message);
}];
Backendless.shared.userService.login(identity: "spidey@backendless.com", password: "greeng0blin", responseHandler: { loggedInUser in
if let phoneNumber = loggedInUser.getProperty(propertyName: "phoneNumber") as? String {
print("User's email: \(loggedInUser.email), phoneNumber: \(phoneNumber)")
}
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
Defining properties with Console¶
User property management is available in the schema editor of the Users
table. To open the schema editor:
- Login to Backendless Console and select your application.
- Click the Data icon, and select the Users table.
- Click the Schema menu.
There are built-in user properties, such as email
, name
and password
. The email
and name
properties can be deleted if they do not apply to your app, although it is recommended to keep the email
property, since it is used for other purposes (for instance, password recovery). The password
property cannot be deleted.
Identity Property/Column¶
It is required that one of the properties is marked as identity. This is the property Backendless uses for the Login and Restore Password operations. As users register, Backendless ensures the value for the identity property is unique.
Password Property/Column¶
"password" is a special property/column. Backendless automatically adds the column when an application is created. The following rules apply to the password column:
- Password cannot be removed from the application.
- Password cannot be used as identity.
- Password is a required property