Blog

How to update a user account using User Service API

by on February 22, 2015

The ability to update a user account is a common use-case for user management in applications. Either the user themself or the admin may need to update user properties. Backendless provides the API for updating user accounts, although app admins/developers can do so using Backendless Console as well. The code below demonstrates the API for updating a user:



    Synchronous API (Plain Java only):

    System.out.println( "[SYNC] Updating user object." );
    BackendlessUser user = Backendless.UserService.login( "spiderman@backendless.com", "greeng0blin" );
    user.setProperty( "name", "Spidey Man" );
    user = Backendless.UserService.update( user );
    System.out.println( "User has been updated" );
    System.out.println( "Name - " + user.getProperty( "name" ) );

    Asynchronous API (Plain Java and Android):

    System.out.println( "[ASYNC] Updating user object." );
    Backendless.UserService.login( "spiderman@backendless.com", "greeng0blin", new AsyncCallback<BackendlessUser>()
    {
        @Override
        public void handleResponse( BackendlessUser backendlessUser )
        {
            backendlessUser.setProperty( "phone", "212-555-1212" );
            Backendless.UserService.update( backendlessUser, new AsyncCallback<BackendlessUser>()
            {
                @Override
                public void handleResponse( BackendlessUser backendlessUser )
                {
                    System.out.println( "User has been updated" );
                    System.out.println( "Phone number - " + backendlessUser.getProperty( "phone" ) );
                }
                @Override
                public void handleFault( BackendlessFault backendlessFault )
                {
                }
            }  );
        }
        @Override
        public void handleFault( BackendlessFault backendlessFault )
        {
        }
    });

    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')
    
    console.log('Updating user object')
    
    const onLoginSuccess = user => {
      user.phone = '212-555-1212'
    
      return Backendless.UserService.update(user)
    }
    
    const onUpdateSuccess = user => {
      console.log('User has been updated')
      console.log(`Phone number - ${ user.phone }`)
    }
    
    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.login('spiderman@backendless.com', 'greeng0blin')
      .then(onLoginSuccess)
      .then(onUpdateSuccess)
      .catch(onError)
    

    Notice the code logs the user in first. If the user is logged in, the user update operation is handled with the same security rules as updating any other data object by its owner (this is reviewed in detail in a separate post).

    It is possible to update a user object without logging in as that user. Consider the following example:


      Synchronous API (Plain Java):

      System.out.println( "[SYNC] Updating user object without login." );
      BackendlessUser user = new BackendlessUser();
      user.setProperty( "objectId", "8F70A260-A415-DECC-FF1F-0B70FF98BD00" );
      user.setProperty( "age", 32 );
      user = Backendless.UserService.update( user );
      System.out.println( "User has been updated" );
      System.out.println( "Age - " + user.getProperty( "age" ) );

      Asynchronous API (Plain Java and Android):

      System.out.println( "[ASYNC] Updating user object without login." );
      BackendlessUser user = new BackendlessUser();
      user.setProperty( "objectId", "8F70A260-A415-DECC-FF1F-0B70FF98BD00" );
      user.setProperty( "age", 32 );
      Backendless.UserService.update( user, new AsyncCallback<BackendlessUser>()
      {
          @Override
          public void handleResponse( BackendlessUser backendlessUser )
          {
              System.out.println( "User has been updated" );
              System.out.println( "Age - " + backendlessUser.getProperty( "age" ) );
          }
          @Override
          public void handleFault( BackendlessFault backendlessFault )
          {
          }
      } );

      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')
      
      console.log('Updating user object without login')
      
      const user = new Backendless.User()
      
      user.objectId = '8F70A260-A415-DECC-FF1F-0B70FF98BD00'
      user.age = 32
      
      const onUpdateSuccess = user => {
        console.log('User has been updated')
        console.log(`Age - ${ user.age }`)
      }
      
      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.update(user)
        .then(onUpdateSuccess)
        .catch(onError)
      

      The approach shown above requires knowing the objectId of the user object. This operation can be easily restricted only for the specific users/roles using Backendless Console:

      1. Login to Backendless Console, select your app and click the Data icon.
      2. The Users table is selected by default. Click the Table Schema and Permissions button in the upper right corner.
      3. Click Role Permissions to grant permission to update other user objects to a role.
      4. In our app example, we have the Administrator role. The screenshot below shows that we granted a permission to update user accounts to that role and denied it to all others: