Start a new topic

Android login to Kinvey account questions

I’m able to login to my Kinvey account using the Android APIs, but I had some questions on how the login process works. The Android guide says the “library automatically cache’s the user’s name and a current authorization token in a saved credential store.” The first question I have is how long is the user information cached? I would prefer to not have to save the user’s password, but I don’t want them to have to login frequently.


The second question I have is what is the best way to check if a user is logged in? I didn’t see any API calls for login check.


The last question I have is do I need to make the user login again after they register?


Thanks for the time!



Here is what I posted before. I had the same problem with IsUserLoggedIn() when the app starts up and it was because the build() function wasn't finished. You need a callback and check IsUserLoggedIn() in the build() callback. I used KinveyUserCallback.


1 person likes this

Sorry for the delay. Here is the code I use in my global class. I only use getInitialClient at app startup to check if the display should go to the login view or the home screen.


  

public void getInitialClient(Context mainContext, int displayview) {

        mainActivityContext = mainContext;
        displayviewInt = displayview;

        client = new Client.Builder(this.getApplicationContext()).setRetrieveUserCallback(new KinveyUserCallback() {      
            @Override
            public void onSuccess(User result) {

                Log.d(TAG, "Kinvey load complete. Load client user is: " + result.getUsername());       

                if (result.isUserLoggedIn()) {
                    Log.d(TAG, "Kinvey user logged in - display home");

                    //code to display home screen

                } else {
                    Log.d(TAG, "Kinvey user not logged in - display settings");

                    //code to display login screen
                }
            }

            @Override
            public void onFailure(Throwable error) {
                Log.d(TAG, "Kinvey Build failure");

            }
        }).build();

    }   

  

Is there a way to make it longer? Making the user log in every two weeks seems inconvenient to the user.

Thanks for your answer Revillo, I took your advice about "Global class" you have half helped, how i can minimise and reopen an app and ".user().isUserLoggedIn()" returns true each time.

Success 1/2.


(Relevant) global application code looks like...:


package some.package.app;

import android.app.Application;
import com.kinvey.android.Client;

public class AppApplication extends Application {

    private Client mKinveyClient;
    private static String APP_ID = "kid_ARandOm1D";
    private static String APP_KEY = "xxxxxxxxxxxxxxxxxx";

    @Override
    public void onCreate() {
        super.onCreate();
        mKinveyClient = new Client.Builder(APP_ID,APP_KEY
                , this.getApplicationContext()).build();
    }


    public Client getClient(){
        return  mKinveyClient;
    }
}

   

Main Activity's oncreate looks like: 

        mKinvey = ((TechCheckApplication) getApplication()).getClient();

 

I have spend several hours and tried several ways to implement a call back onBuild() but am completely lost and only get compiler errors. are you able to provide a small snippet and or advise if the call back should be in the global class or the main activities variable which retrieves from the global class?


Kind Regards

I believe that is not configurable at this moment as the value is common across the board.


Another way to achieve this is to implement "Remember Me" checkbox option with login so that the user wouldn't need to enter the credentials again at the next login. This would mean saving the user's password locally using encryption.



Regards,

Wani

Hi Oliver,


As the android guide says, username and session is stored. This means that user's password is not stored but only that particular session information is stored.


And hence, you will need to call login method in only the following cases:

  • When the application launches and a current user is not established in the credential store (fresh install, first launch)
  • When the application is reinstalled on the same or a new device for a user who already has an account established on the back-end (uninstall and reinstall, first launch)
  • The user has logged out and the application needs to make more calls to Kinvey

To answer the second question, the active user can be obtained by initializing an instance of the Kinvey client, and then calling mKinveyClient.user().

To specifically check the "logged in" status, you can use mKinveyClient.user().isUserLoggedIn().


And lastly, after successful registration, the user will be in logged in status for that application session and you will be able to check that by the same call - mKinveyClient.user().isUserLoggedIn().


Above information and relevant code snippets can be found at http://devcenter.kinvey.com/android/guides/users.


Let me know if you need anything else.


Regards,

Wani


Is the forum having issues? I posted an answer last night, but it's not showing up.

Sagar,


If it's returning false when the application is killed it seems like the event thread is stuck for some reason (thread.sleep or otherwise).   Can you refactor your code in a way that will be free of synchronous calls that hang the event thread?


Thanks,

The auth token expiry is set at 14 days.



Regards,

Wani

Thanks Wani. Does this mean the auth token Kinvey uses for login doesn't expire?
Hi, I have tried the following. first run of the application IsUserLoggedIn() returns false (expected). I login and immediately call IsUserLoggedIn() returns true (expected). I return to the home screen and inside the onStop() event and IsUserLoggedIn() returns true (expected). I return to the app and call IsUserLoggedIn() inside the onStart() event and it returns true as expected. I return to the home screen for the second time and call IsUserLoggedIn() inside onStop() and it returns true (as expected). I return to the app (for the second time) and call IsUserLoggedIn() inside onStart() and it returns false (unexpected). I have repeated the above a several dozen times with the same outcome What is the cause of this? Furthermore I have tried the same as above but instead of returning to the home screen I terminate the app each time and the exact same happens. I have also tried to call the retrieve() function at each step and it fails each and every time, even immediately after login(). Can you shed any light on these, I.e is this expected? What am I doing wrong. At this rate the user will have to log back in every other time the app is opened, regardless of whether the app is launched or returned to... I would appreciate any input on you can provide. Kind Regards (Formatting of my previous post wasn't preserved and made it unreadable)

When you call thread.sleep() the thread will miss the reply if it comes in during those 5 seconds.  Is there some reason why you're not using standard asynchronous programming paradigms that take advantage of threads and futures?


Thanks,

Hi Damien,


I have tried to perform the IsUserLoggedIn() check in the onSuccess() event of the Build function, the result is the same, once the application is killed it returns false.

I had the same issue at app startups and it was because the build() command wasn't finished before I called IsUserLoggedIn().  It was a pain because I had build() in the global class like the examples, but I needed to check user login in my main activity. However, you can use KinveyUserCallback for build() and check IsUserLoggedIn() in the callback function.


Hope this helps.

School boy error, the callback is a void method hence why it cant be assigned to a variable. but this has only solved half my problem, as long as the application is alive i.e. not terminated, the user remains logged in. but as soon as the application is terminated, the user is no longer logged in.


My Application code looks like:


   

package some.package.app;

import android.app.Application;
import android.content.Intent;
import com.kinvey.android.Client;
import com.kinvey.android.callback.KinveyClientBuilderCallback;

public class AppApplication extends Application {

    public Client mKinveyClient = null;
    private static String APP_ID = "kid_xxxxxxxxxx";
    private static String APP_KEY = "xxxxxxxxxxxxxxxxxxxxxxxx";

    @Override
    public void onCreate() {
        super.onCreate();

        new Client.Builder(APP_ID,APP_KEY, this.getApplicationContext()).build(new KinveyClientBuilderCallback() {
            @Override
            public void onSuccess(Client client) {

                mKinveyClient = client;

            }

            @Override
            public void onFailure(Throwable throwable) {

            }
        });
    }

    public Client getClient(){

        return  mKinveyClient;

    }
}

 

You will see in the above code that the mKinveyClient variable is initialised as "null" and I use the following Async to check if the the Client is initialized seems extreme but needed to be sure.

    

Client mKinvey;

...

private class WaitForBuilder extends AsyncTask{

        @Override
        protected void onPostExecute(Object o) {

            super.onPostExecute(o);

                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

			mKinvey = ((TechCheckApplication) getApplication()).getClient();


            if (mKinvey.user().isUserLoggedIn()){

                Toast.makeText(getApplicationContext(), "LOGGED IN!", Toast.LENGTH_LONG).show();

            }

            else {

                Toast.makeText(getApplicationContext(), "LOGGED OUT!", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected Object doInBackground(Object[] params) {
            
            while (((TechCheckApplication) getApplication()).getClient() == null)
            {
            // Keep waiting...
            }
            return null;
        }
    }

 I wait another 5 seconds after the Application class has assigned a value to the mKinveyClient variable to make sure it has initialised and yet IsUserLoggedIn() retruns false...


any help would be great on this one   


Login or Signup to post a comment