Blog

Dynamic user definition with the Registration API

by on February 14, 2015

In another article, we reviewed how to register app users with the API. By default, a Backendless backend declares a user entity with three properties: email, password, and name. The “email” property is configured as identity by default, meaning its value should be passed into the login API request.

Typically user entity properties correspond to the fields in the app’s user registration form. When a user registers with the app, the data from the registration form is sent to the server using the User Registration API. While processing a user registration request, Backendless extracts information from the incoming user object and, if it contains a new (undeclared on the backend) property, the property is added to the user entity schema. Consider the following sample code:


    Synchronous API (Plain Java):

    Calendar calendar = Calendar.getInstance();
    calendar.set(1980, 5, 12 );
    Date birthDate = calendar.getTime();
    BackendlessUser user = new BackendlessUser();
    user.setEmail( "spiderman@backendless.com" );
    user.setPassword( "greeng0blin" );
    user.setProperty( "name", "Spidey" );
    user.setProperty( "birthdate", birthDate );
    user.setProperty( "employeeId", 101 );
    Backendless.UserService.register( user );

    Asynchronous API (Android and Plain Java):

    Calendar calendar = Calendar.getInstance();
    calendar.set( 1980, 5, 12 );
    Date birthDate = calendar.getTime();
    BackendlessUser user = new BackendlessUser();
    user.setEmail( "spiderman@backendless.com" );
    user.setPassword( "greeng0blin" );
    user.setProperty( "name", "Spidey" );
    user.setProperty( "birthdate", birthDate );
    user.setProperty( "employeeId", 101 );
    Backendless.UserService.register( user, new AsyncCallback<BackendlessUser>()
    {
        @Override
        public void handleResponse( BackendlessUser backendlessUser )
        {
            System.out.println( "user has been registered" );
        }
        @Override
        public void handleFault( BackendlessFault backendlessFault )
        {
            System.out.println( "server reported an error - " + backendlessFault.getMessage() );
        }
    } );

    //
    //  ViewController.m
    #import "ViewController.h"
    #import "Backendless.h"
    static NSString *APP_ID = @"YOUR-APP-ID";
    static NSString *SECRET_KEY = @"YOUR-IOS-SECRET-KEY";
    static NSString *VERSION_NUM = @"v1";
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        [backendless initApp:APP_ID secret:SECRET_KEY version:VERSION_NUM];
        [self registerUserWithDynamicPropertiesSync];
        [self registerUserWithDynamicPropertiesAsync];
    }
    #pragma mark -
    #pragma mark Private Methods
    -(void)registerUserWithDynamicPropertiesSync {
        @try {
            NSDateFormatter *dateFormatter = [NSDateFormatter new];
            [dateFormatter setDateFormat:@"dd.MM.yyyy 'at' HH:mm"];
            NSDate *birthDate = [dateFormatter dateFromString:@"12.05.1980 at 12:00"];
            BackendlessUser *user = [BackendlessUser new];
            user.email = @"spiderman@backendless.com";
            user.password = @"greeng0blin";
            [user setProperty:@"name" object:@"Spidey"];
            [user setProperty:@"birthdate" object:birthDate];
            [user setProperty:@"employeeId" object:@101];
            BackendlessUser *registeredUser = [backendless.userService registering:user];
            NSLog(@"User has been registered (SYNC): %@", registeredUser);
        }
        @catch (Fault *fault) {
            NSLog(@"Server reported an error (SYNC): %@", fault);
        }
    }
    -(void)registerUserWithDynamicPropertiesAsync {
        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"dd.MM.yyyy 'at' HH:mm"];
        NSDate *birthDate = [dateFormatter dateFromString:@"12.05.1980 at 12:00"];
        BackendlessUser *user = [BackendlessUser new];
        user.email = @"spiderman@backendless.com";
        user.password = @"greeng0blin";
        [user setProperty:@"name" object:@"Spidey"];
        [user setProperty:@"birthdate" object:birthDate];
        [user setProperty:@"employeeId" object:@101];
        [backendless.userService
         registering:user
         response:^(BackendlessUser *registeredUser) {
             NSLog(@"User has been registered (ASYNC): %@", registeredUser);
         }
         error:^(Fault *fault) {
             NSLog(@"Server reported an error (ASYNC): %@", fault);
         }];
    }
    @end

    //
    //  ViewController.swift
    import UIKit
    class ViewController: UIViewController {
        let APP_ID = "YOUR-APP-ID"
        let SECRET_KEY = "YOUR-IOS-SECRET-KEY"
        let VERSION_NUM = "v1"
        var backendless = Backendless.sharedInstance()
        override func viewDidLoad() {
            super.viewDidLoad()
            backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
            registerUserWithDynamicPropertiesSync()
            registerUserWithDynamicPropertiesAsync()
        }
        func registerUserWithDynamicPropertiesSync() {
            Types.try({ () -> Void in
                var dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "dd.MM.yyyy 'at' HH:mm"
                var birthDate = dateFormatter.dateFromString("12.05.1980 at 12:00")
                var user = BackendlessUser()
                user.email = "spiderman@backendless.com"
                user.password = "greeng0blin"
                user.setProperty("name", object:"Spidey")
                user.setProperty("birthdate", object:birthDate)
                user.setProperty("employeeId", object:101)
                var registeredUser = self.backendless.userService.registering(user)
                println("User has been registered (SYNC): \(registeredUser)")
                },
                catch: { (exception) -> Void in
                    println("Server reported an error (SYNC): \(exception as Fault)")
            })
        }
        func registerUserWithDynamicPropertiesAsync() {
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd.MM.yyyy 'at' HH:mm"
            var birthDate = dateFormatter.dateFromString("12.05.1980 at 12:00")
            var user = BackendlessUser()
            user.email = "spiderman@backendless.com"
            user.password = "greeng0blin"
            user.setProperty("name", object:"Spidey")
            user.setProperty("birthdate", object:birthDate)
            user.setProperty("employeeId", object:101)
            backendless.userService.registering(user,
                response: { (var registeredUser : BackendlessUser!) -> () in
                    println("User has been registered (ASYNC): \(registeredUser)")
                },
                error: { (var fault : Fault!) -> () in
                    println("Server reported an error (ASYNC): \(fault)")
                }
            )
        }
    }

    const Backendless = require('backendless')
    /*
     Or use `import Backendless from 'backendless'` for client side.
     If you don't use npm or yarn to install modules, you can add the following line
     <script src="//api.backendless.com/sdk/js/latest/backendless.min.js"></script>
     to your index.html file and use the global Backendless variable.
    */
    
    Backendless.initApp('YOUR_APP_ID', 'YOUR_JS_API_KEY')
    
    const birthDate = new Date('05/12/1980').getTime()
    
    const user = new Backendless.User()
    user.email = 'spiderman@backendless.com'
    user.password = 'greeng0blin'
    user.name = 'Spidey'
    user.birthdate = birthDate
    user.employeeId = 101
    
    const onRegisterSuccess = user => {
      console.log('user has been registered')
    }
    
    const onError = error => {
      console.error('Server reported an error: ', error.message)
      console.error('error code: ', error.code)
      console.error('http status: ', error.status)
    }
    
    Backendless.UserService.register(user)
      .then(onRegisterSuccess)
      .catch(onError)

    Once you run the code, Backendless creates the “birthdate” and “employeeId” properties. You can verify everything in Backendless Console:

    1. Login to Backendless Console, select your app and click the Users icon.
    2. The User Properties screen displays the user entity definition:
    3. The registered user also appears in the Users table, available in the Data screen:

    The behavior of declaring user entity properties dynamically can be turned off by using the Enable Dynamic User Definition toggle. The toggle is located on the Users > User Properties screen: