userToken questions

Hi dear Backendlesses,

trying to make the login of a user persistent.

in my onCreate I’ve put the initApp and then this code:

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

     if( userToken != null && !userToken.equals( "" ) ) {  // user login is available, skip the login activity/login form }
       //String first_name = (String) user.getProperty("first_name");
      String usernamestring = (String) user.getProperty("email");
      Toast.makeText(MainActivity.this, String.format("Welcome token " + usernamestring), Toast.LENGTH_LONG).show();

      Intent intent = new Intent(MainActivity.this, FeedActivity.class);
      startActivity(intent);
}

then in my onLogin I’ve put the following code:

public void onLogin(View view){
    Toast.makeText(MainActivity.this, String.format("signing in..."), Toast.LENGTH_LONG).show();
    textboxusername = (EditText) findViewById(R.id.editTextMail);
        textboxpassword = (EditText) findViewById(R.id.editTextPassword);
        final String usernamestring = textboxusername.getText().toString();
        String passwordstring = textboxpassword.getText().toString();

        Backendless.UserService.login( usernamestring, passwordstring, new AsyncCallback<BackendlessUser>()
        {
            public void handleResponse( BackendlessUser user )
            {
                // user has been logged in
                Toast.makeText(MainActivity.this, String.format("Welcome " + user.getProperty("email")), Toast.LENGTH_LONG).show();


                AsyncCallback<Boolean> isValidLoginCallback = new AsyncCallback<Boolean>()
                {
                    @Override
                    public void handleResponse( Boolean response )
                    {
                        System.out.println( "[ASYNC] Is login valid? - " + response );
                        Toast.makeText(MainActivity.this, String.format( "[ASYNC] Is login valid? - " + response ), Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void handleFault( BackendlessFault fault )
                    {
                        System.err.println( "Error - " + fault );
                        Toast.makeText(MainActivity.this, String.format( "Error - " + fault ), Toast.LENGTH_LONG).show();
                    }

                };

                Backendless.UserService.isValidLogin(isValidLoginCallback);

                Intent intent = new Intent(MainActivity.this, FeedActivity.class);
                startActivity(intent);

            }

            public void handleFault( BackendlessFault fault )
            {
                // login failed, to get the error code call fault.getCode()
                Toast.makeText(MainActivity.this, String.format(fault.getMessage()), Toast.LENGTH_LONG).show();
            }
        }, true);
    }

I thought that will work, but when I close my app it doesn’t save my current user login.
I do get a [ASYNC] Is login valid? - true response though.

what am I missing ?

thanks! love you guys (and Kate)!

Why do you invoke “Backendless.UserService.isValidLogin” in the response for the login call? That does not make any sense…

the documentation states to login and then use the isValidLogin:

Log in a user first. Make sure the stayLoggedIn argument is true. The value of true persists the information about the login for the use by subsequent starts/sessions of the application:
Backendless.UserService.login( "batman@backendless.com", “superm@n”, true );
Then, check whether the login is valid - see the example below:

where should I write it ? in the onCreate method ?
thanks Mark.

Please see this:

http://support.backendless.com/public/attachments/af86afc6b7137cf8d8f9a8f4663bdd9b.png</img>

I’ve put the code in my onCreate but still it doesn’t save the user.

in the onCreate I have the initApp, then the AsyncCallback<Boolean> isValidLoginCallback = new AsyncCallback<Boolean>(), then the if( userToken != null && !userToken.equals( “” ) ) and inside that I have the intent to leave the login screen to the next screen.

Sorry, i do not understand what checking if user is logged in has to do with saving the user…

I’m tring to login a user once and from then to skip the login screen.

when the user logins for the first time, the “stayLoggedIn” is set to “true”.
next time I run the app, the “AsyncCallback<Boolean> isValidLoginCallback = new AsyncCallback<Boolean>()” should re-login the same user, am I wrong ?

From the doc: isValidLogin checks if the login is still valid. Which means if the login is valid, you do not need to show the login form (yes, that also means that user does not need to re-login). At that point you can proceed to the rest of your app logic.

maybe my problem is with the BackendlessUser user = Backendless.UserService.CurrentUser();how should I define the user ?

define the user on the second launch of the app?

Dear Mark, thanks for all the help.

Maybe you could simply tell me the correct sequence of code?

What is defined Before the onCreate, in the onCreate and in my “userpressedlogin” and "userpressedregisterx methodes.

I love you documentation but I don’t understand from it, what comes where.

Thanks I really appreciate your help Mark.

also, do I need to define the user in each activity ?
“BackendlessUser user = Backendless.UserService.CurrentUser();”

got it working with this code:

Backendless.UserService.isValidLogin(new DefaultCallback&lt;Boolean&gt;(this) {
    @Override
    public void handleResponse(Boolean isValidLogin) {
        if (isValidLogin && Backendless.UserService.CurrentUser() == null) {
            String currentUserId = Backendless.UserService.loggedInUser();

            if (!currentUserId.equals("")) {
                Backendless.UserService.findById(currentUserId, new DefaultCallback&lt;BackendlessUser&gt;(MainActivity.this, "Logging in...") {
                    @Override
                    public void handleResponse(BackendlessUser currentUser) {
                        super.handleResponse(currentUser);
                        Backendless.UserService.setCurrentUser(currentUser);
                        startActivity(new Intent(getBaseContext(), FeedActivity.class));
                        finish();
                    }
                });
            }
        }

        super.handleResponse(isValidLogin);
    }
});

however, don’t see the similarity with the code example in the documentaries…