Skip to content

Validating User Login

Description

The login operation provides a way to persist the user-token on the client side so it can be used when the application is restarted. This helps in streamlining the user experience since the user of the application does not need to login again. However, when the application restarts, it needs to check if the underlying user token, and hence the user session is still valid. This can be accomplished with the API below:

Method

Non-Blocking API

public void Backendless.UserService.isValidLogin( AsyncCallback<Boolean> callback );

Blocking API

public boolean Backendless.UserService.isValidLogin();

If user token is valid, objectId of the logged in user can be retrieved with the following call:

string currentUserObjectId = UserIdStorageFactory.instance().getStorage().get()

Subsequently the BackendessUser instance can be obtained with the following API:

Non-blocking call:

Backendless.Data.of( BackendlessUser.class ).findById( 
                            currentUserObjectId, 
                            AsyncCallback<BackendlessUser> callback )

Blocking call:

Backendless.Data.of( BackendlessUser.class ).findById( currentUserObjectId )

Return Value

true if the current user session(user token) is valid. Otherwise, returns false.

Example

To validate the user session, the user must be logged in first, make sure to set the stayLoggedIn argument to true to persist the information about the login for the use by subsequent starts/sessions of the application. For more information refer to the Login topic.

The example below checks if the current session is valid.

Blocking API

boolean isValidLogin = Backendless.UserService.isValidLogin();
Log.i( "MYAPP", "[SYNC] Is login valid? - " + isValidLogin );

Non-Blocking API

AsyncCallback<Boolean> isValidLoginCallback = new AsyncCallback<Boolean>()
{
  @Override
  public void handleResponse( Boolean response )
  {
    Log.i( "MYAPP", "[ASYNC] Is login valid? - " + response );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.i( "MYAPP", "Error - " + fault );
  }

};

Backendless.UserService.isValidLogin( isValidLoginCallback );

Codeless Reference

user_service_codeless_is_valid_user_session

Returns true if the current user session(user token) is valid. Otherwise, returns false.

Consider the following record in the Users data table:
user_service_codeless_example_remove_user_3

The example below logs in as "alice@yourmail.com". The stay logged in parameter must be set to true to establish a persistent session. Then the session is validated by using the Is Valid User Session Codeless block.

user_service_codeless_example_is_valid_user_session