Blog

How to change a user’s password via API (if the user can login)

by on March 30, 2015

In another article, we wrote about how to change a user’s password using Backendless Console. There are also ways to change a user’s password using API. In this post, we will review the API that can be used to change the password if a user can log in. A different appraoch is needed when a user cannot log in for the reason that he/she forgot their password. That scenario, requiring a temporary password be created, is discussed here.


    Backendless.UserService.login( "spidey@backendless.com", "greeng0blin", new AsyncCallback<BackendlessUser>()
    {
        @Override
        public void handleResponse( BackendlessUser user )
        {
           user.setPassword( "MyNewPassword" );
           Backendless.Data.of( BackendlessUser.class ).save( user, new AsyncCallback<BackendlessUser>()
           {
               @Override
               public void handleResponse( BackendlessUser backendlessUser )
               {
                   System.out.println( "User password has been changed" );
               }
               @Override
               public void handleFault( BackendlessFault backendlessFault )
               {
                   System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
               }
           } );
        }
        @Override
        public void handleFault( BackendlessFault backendlessFault )
        {
            System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
        }
    } );

    val user = Backendless.UserService.login("spidey@backendless.com", "greeng0blin")
    user.password = "myNewPassword"
    Backendless.Data.of(BackendlessUser::class.java).save(user, object : AsyncCallback {
       override fun handleResponse(response: BackendlessUser?) {
           Log.i(TAG, "User password has been changed")
       }
    
       override fun handleFault(fault: BackendlessFault?) {
           Log.e(TAG, fault?.message)
       }
    })

    [Backendless.shared.userService loginWithIdentity:@"spidey@backendless.com" password:@"greeng0blin" responseHandler:^(BackendlessUser *loggedInUser) {
        loggedInUser.password = @"MyNewPassword";
        [[Backendless.shared.data of:[BackendlessUser class]] updateWithEntity:loggedInUser responseHandler:^(BackendlessUser *updatedUser) {
            NSLog(@"User password has been changed");
        } errorHandler:^(Fault *fault) {
            NSLog(@"Error: %@", fault.message);
        }];
    } errorHandler:^(Fault *fault) {
        NSLog(@"Error: %@", fault.message);
    }];

    Backendless.shared.userService.login(identity: "spidey@backendless.com", password: "greeng0blin", responseHandler: { loggedInUser in
        loggedInUser.password = "MyNewPassword"
        Backendless.shared.data.of(BackendlessUser.self).update(entity: loggedInUser, responseHandler: { updatedUser in
            print("User password has been changed")
        }, errorHandler: { fault in
            print("Error:\(fault.message ?? "")")
        })
    }, errorHandler: { fault in
        print("Error:\(fault.message ?? "")")
    })

    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 onLoginSuccess = user => {
      user.password = 'MyNewPassword'
    
      return Backendless.UserService.update(user)
    }
    
    const onPasswordUpdateSuccess = user => {
      console.log('User password has been changed')
    }
    
    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('spidey@backendless.com', 'greeng0blin')
      .then(onLoginSuccess)
      .then(onPasswordUpdateSuccess)
      .catch(onError)
    

    var user = await Backendless.UserService.login("spidey@backendless.com", "greeng0blin");
    user.password = "MyNewPassword";
    Backendless.Data.of("Users").save(user.toJson()).then((response) => print("User password has been changed"));

    Notice the sample shows user login first. This is required as the user identity must be established for the subsequent password update call to go through.