Monday, 14 March 2016

CloudBoost Android: CloudUser Demo

NB: you can now read this post on my new site 

In this post

We are going to look at how to take advantage of CloudBoost's CloudUser APIs to manage one of the most important parts of your app: User management.
Normally, when you are building an application with your own backend, you'd have to create a database for your users, create APIs for signup, login and logout. This can be quite cumbersome especially if you are new to user management. Additionally, you would have to create an engine for managing permissions of different categories of users to different parts of the app or a certain kind of data.


I haven't even talked about a case where the user loses their password and you have to create a password reset API that authenticates the user through their email address and gives them a link to create a new password.

Fortunately, for a developer building an app backed by CloudBoost, CloudUser is a powerful API that offers all the above functionalities without you having to write any backend code, but rather to call existing APIs to perform what ever functionality you need.

More good news is that the CloudBoost team recently added resetPassword and changePassword APIs to CloudUser. This, coupled with the already existing APIs, Access Control Lists and CloudRole API, gives your app a complete package to manage the most important part of any developers mission: Users.

Summary

In a nut shell, the demo is going to:
  1. Sign up a user
  2. Login an existing User
  3. Log out a logged in User
  4. Reset a forgotten password
  5. Change password for a logged in User

Assumptions:

  1. You have basic experience with CloudBoost
  2. You have build a Hello world Android app before

SignupActivity

The magic of sign up occurs in this AsyncTask:



CloudUser user=new CloudUser();
user.setUserName(params[0]);
user.setPassword(params[1]);
user.setEmail(params[2]);
user.signUp(new CloudUserCallback() {
 @Override
         public void done(CloudUser user, CloudException e) throws CloudException {
  if(e!=null)
   throw e;
  else{
   runOnUiThread(new Runnable() {
    public void run() {
    Toast.makeText(
SignupActivity.this, "New user "+params[0]+" created",                                                          
Toast.LENGTH_SHORT).show();      
     }
    });
   App.CURRENT_USER=user;
   Intent i=new Intent(SignupActivity.this, DashboardActivity.class);
     startActivity(i);
    }
  }
});

FortunatelyThe CloudUser.signUp API needs the user's username, password and email  represented by params[0], params[1] 

Login User

We have a simple enough API to login our User as below. But note that the mere process of Signing up a User sets them already as the currrent logged in user.

CloudUser user=new CloudUser();
user.setUserName(params[0]);
user.setPassword(params[1]);
user.logIn(new CloudUserCallback() { 
 @Override
 public void done(CloudUser user, CloudException e) throws CloudException {
  if(e!=null)
   throw e;
  else{
   App.CURRENT_USER=user;
   Intent i=new Intent(LoginActivity.this, DashboardActivity.class);
   startActivity(i);
   }
 }
});
As you have seen, the login API only requires username and password



Logging out User


Here is the code for logging out a logged in User, still very simple and self explanatory:

CloudUser user = CloudUser.getcurrentUser();
user.logOut(new CloudUserCallback() {
     @Override
      public void done(CloudUser user, CloudException e)
 throws CloudException {
      if(e!=null)
   throw e;
     else Toast.makeText(DashboardActivity.this, "Logged out",Toast.LENGTH_SHORT).show();
     }
    });

Reset Password

 Usually on the login page, we assume that a user may have lost their password or forgotten it especially if they havent loged in for a long time. So such a functionality requires that the user does not have a running session on the server, otherwise an error will be thrown advising them to use ChangePassword API instead.
Since we don't need the user to be logged in, this API is actually called statically:

CloudUser.resetPassword(params[0], new CloudStringCallback() {
 @Override
 public void done(final String x, CloudException e) throws CloudException {
      if(e!=null)
            throw e;
     else{
         runOnUiThread(new Runnable() {
   public void run() {
  Toast.makeText(ResetActivity.this, x, Toast.LENGTH_SHORT).show();
      }
   });
   Intent i=new Intent(ResetActivity.this, LoginActivity.class);
   startActivity(i);
      }
      
     }
    });


Change Password

At times a User may decide that their password needs to be changed. May be because its too complicated or too simple. This, you should allow for in your application too. Here is the code for password change:

CloudUser user = App.CURRENT_USER;
user.changePassword(params[0], params[1],
new CloudUserCallback() {
@Override
public void done(CloudUser user,final CloudException e) throws CloudException {
 if (e != null)
     runOnUiThread(new Runnable() {
   public void run() {
   Toast.makeText(
            ChangePasswordActivity.this,
      e.getMessage(),
       Toast.LENGTH_SHORT).show();
        }
    });
 else {
  runOnUiThread(new Runnable() {
  public void run() {
   Toast.makeText(
             ChangePasswordActivity.this,
    "Password changed successfully",
     Toast.LENGTH_SHORT).show();
       }
    });
  CloudUser.setCurrentUser(user);
  App.CURRENT_USER = user;
   Intent i = new Intent(
   ChangePasswordActivity.this,
    DashboardActivity.class);
      startActivity(i);
    }
  }
});

The changePassword API requires that a User is logged in.
After clicking change Password button, the user will still remain logged in.

This is just a tip of the Ice berg when it comes to how much you can do with CloudUser APIs. In a later post, we shall look at permission management. 

Please find the full source code on GITHUB

No comments:

Post a Comment