Problem with Login API

I need the user to be able to do certain things only if they are logged in.

So i do this test and bring up the requested activity or login activity based on the result of the test

if (userToken!= null && !userToken.equals("")) {

                Intent intent = new Intent(getApplicationContext(), PostVehicleActivity.class);
                startActivity(intent);
            }
            else {
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
                finish();

            }

//This is my login routine in LoginActivity

Backendless.UserService.login(email, password, new AsyncCallback() {
public void handleResponse(BackendlessUser user) {
// user has been logged in

            Intent intent = new Intent(getApplicationContext(), PostVehicleActivity.class);
            startActivity(intent);

        }
    

    public void handleFault(BackendlessFault fault) {
        // login failed, to get the error code call fault.getCode()
                    }
    
});

But each time i do the test userToken is always null and i always get the login screen

Hello @Nkekere_Tommy_Minimann

Could you please tell me where you got this userToken from?

Regard, Viktor

String userToken =
UserTokenStorageFactory. instance().
getStorage (). get ();

Hi @Nkekere_Tommy_Minimann

According to the users login in AndroidSDK documentation (Login - Backendless SDK for Android/Java API Documentation)

To save the user token in the Storage you should pass the stayLoggedIn = true parameter to Backendless.UserService.login(...)

Async example:

Backendless.UserService.login( email, password, new AsyncCallback<BackendlessUser>() {
  @Override
  public void handleResponse( BackendlessUser response )
  {
    System.out.println( "user has been logged in" );
    String userToken = UserTokenStorageFactory.instance().getStorage().get();
    if( userToken != null && !userToken.equals( "" ) )
    {
      System.out.println( "userToken not null- " + userToken );
    }
    else
    {
      System.out.println( "userToken is null" );
    }
  }
  @Override
  public void handleFault( BackendlessFault fault ) {
    System.out.println( "login failed - " + fault.getCode() );
    System.out.println( "login failed - " + fault.getMessage() );
  }
}, true );

Sync example:

Backendless.UserService.login( email, password, true );
String userToken = UserTokenStorageFactory.instance().getStorage().get();
if( userToken != null && !userToken.equals( "" ) )
{
  System.out.println( "userToken not null - " + userToken );
}
else
{
  System.out.println( "userToken is null" );
}

How do I save the userToken in the async method call?

Hello @Nkekere_Tommy_Minimann,

As Denys said, the userToken is saved automatically when stayLoggedIn = true and then you can get it tis way:

String userToken = UserTokenStorageFactory.instance().getStorage().get();

In case stayLoggedIn = false you can get it this way:

Backendless.UserService.login( email, password, new AsyncCallback<BackendlessUser>() {
  @Override
  public void handleResponse( BackendlessUser response )
  {
    System.out.println( "user has been logged in" );
    String userToken = (String) backendlessUser.getProperty( HeadersManager.HeadersEnum.USER_TOKEN_KEY.getHeader() );

    // here you can save the userToken or use it without saving 

  }
  @Override
  public void handleFault( BackendlessFault fault ) {
    System.out.println( "login failed - " + fault.getCode() );
    System.out.println( "login failed - " + fault.getMessage() );
  }
}, false );

Regards,
Olha

The async method call doesn’t have a stayloggedin variable. What I see there is an asyncCallBack. Only the sync method call has stayloggedin Boolean variable. Am I missing something? Show me exactly how to passed in staylogged as true in the ASYNC method call.

According to this documentation, the last parameter is stayLoggedIn:

Regards,
Olha

Wow! I completely missed that. Thanks :blush:

1 Like