Start a new topic
Answered

Login behaviour

Hi,


I have a behaviour that I don't understand :

final Client kinveyClient = new Client.Builder(this).build();
kinveyClient.user().logout().execute();
kinveyClient.user().login(email, password, new KinveyUserCallback() {
    public void onSuccess(User user) {
        Toast.makeText(LoginActivity.this, "Username=" + kinveyClient.user().getUsername() + "/Logged in=" + kinveyClient.user().isUserLoggedIn(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onFailure(Throwable throwable) {}
});

displays Username=email@email.com/Logged in=true



If I use a new kinveyClient :

final Client kinveyClient = new Client.Builder(this).build();
kinveyClient.user().logout().execute();
kinveyClient.user().login(email, password, new KinveyUserCallback() {
    public void onSuccess(User user) {
        final Client kinveyClient2 = new Client.Builder(LoginActivity.this).build();
        Toast.makeText(LoginActivity.this, "Username=" + kinveyClient2.user().getUsername() + "/Logged in=" + kinveyClient2.user().isUserLoggedIn(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onFailure(Throwable throwable) {}
});

displays Username=null/Logged in=true

 

Could you explain me why the username is null while the user is logged in ?




Best Answer

Fabreax,


It seems as though I've mislead you!   You can actually have both logged in at the same time, what's happening here is actually a bit different.   You are being tricked.  Your login call with client2 has all the credentials (as the ID / Auth token are stored on disk) so the call seems to happen without network activity, but all the other information requires an update from the server, which is an asynchronous call.


You can use something like the following to help you out:


final Client kinveyClient2 = new Client.Builder(this).setRetrieveUserCallback(new KinveyUserCallback(){...}).build()


Hopefully this helps.  Sorry about the misleading first reply.


To my knowledge you can only have 1 session per logged in user, another call to login will assume that you want to logout the user from their existing session.


Let me double-check this functionality with Ed.


Thanks,

Thanks ! It's curious to have a user logged in without a username.

Answer

Fabreax,


It seems as though I've mislead you!   You can actually have both logged in at the same time, what's happening here is actually a bit different.   You are being tricked.  Your login call with client2 has all the credentials (as the ID / Auth token are stored on disk) so the call seems to happen without network activity, but all the other information requires an update from the server, which is an asynchronous call.


You can use something like the following to help you out:


final Client kinveyClient2 = new Client.Builder(this).setRetrieveUserCallback(new KinveyUserCallback(){...}).build()


Hopefully this helps.  Sorry about the misleading first reply.


1 person likes this

Thanks Damien !


The kinvey doc about the "Client" class is explicit :

It is recommended to maintain a single instance of a Client while developing with Kinvey, either in an Activity, a Service, or an Application.

I was creating a new client. Since I'm now using only one kinvey Client (using a Singleton), I have no issue.