Support Topics Documentation Slack YouTube Blog

User Validity


(Saad Zahoor) #1

i am using User Validity to validate user information but i am getting my user object null right after i call the user validity code. I am successfully login but after that my user object becomes null… On most of the activity or those activity where i need to call the api, before calling i check weather user is null or not

so

BackendlessUser user;
 user = Backendless.UserService.CurrentUser();
        if (user ==  null){
            Toast.makeText(this, "User Is Not Login", Toast.LENGTH_SHORT).show();



        }else {  .... Api Calling Stuff } 

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 );

If i do not user the validity code and simply login then everything is working perfect. My user object is not null.


(Mark Piller) #2

If you have not done login in the current session, then the Backendless.UserService.CurrentUser() will not return a valid object. That method returns an object only if you have executed the login API.

What I believe you’re looking for is described here:
https://backendless.com/docs/android/users_login.html#validating-user-login

Regards,
Mark


(Saad Zahoor) #3

but the code of validity is showing me user profile that means he login successfully!
I mean i get the user name and other property from the server when i successful login using validity code Right and from main profile if i get to other activity then my user object null. is there is anything wrong with Api, if not then i have some problem in code


(Mark Piller) #4

I think you are confusing two different concepts. When you use the Login API, you can retrieve the BackendlessUser object with Backendless.UserService.CurrentUser(). If you run the app and didn’t use the login API, then the CurrentUser() function will not return a valid object. That remains true even if you use the stayLoggedIn argument and set it to true.

When you talk about “session validity”, what the SDK does is it checks whether the user token generated from the previous successful login is still valid, that’s all. It has nothing to do with retrieving user object.


(Saad Zahoor) #5

lets say if “session validity” checks that user token still valid… what does this indicate? do i need to call the LOGIN api if the token is valid ?


(Mark Piller) #6

If the session token (called user-token in our docs) is valid, it means all API calls made by your app will carry the identity of the user which the token represents. If the token is not valid, it means it has expired and your app would need to redirect the user to the login screen


(Saad Zahoor) #7

Thank you for your time. I will work into it and see what happens