Aws-sdk-android: No public method to know if a user is logged in

Created on 1 Mar 2017  Â·  19Comments  Â·  Source: aws-amplify/aws-sdk-android

  • What service are you using?
    Cognito

  • In what version of SDK are you facing the problem?
    2.3.8

My issue : I haven't found a way to check if a user is currently logged in via CognitoUser class, ie if cached sessions tokens are present on the device.
The signOut method allows to clear cached tokens but there doesn't seem to be a public method to know if they are present.
getCachedSession would be perfect for this, but it has a protected visibility.

This would be really useful to decide whether to display a Login/Signup action or a Signout action for example.

Cognito Feature Request Needs Info from Requester

Most helpful comment

We (@bvirlet and I) don't use Cognito anymore but thanks for the offer.

All 19 comments

Note that such a method has been added in the iOS SDK: https://github.com/aws/aws-sdk-ios/issues/370#issuecomment-220691786

Is there any workaround for this? I can see that iOS SDK has the property signedIn with getter as isSignedIn

Here is a workaround I've implemented:

public boolean isLoggedIn() {
    CognitoUser currentUser = getUserPool().getCurrentUser();
    SharedPreferences prefs = context.getSharedPreferences("CognitoIdentityProviderCache", 0);
    String csiIdTokenKey = "CognitoIdentityProvider." + CognitoIdentityUserPoolAppClientId + "." + currentUser.getUserId() + ".idToken";
    return prefs.contains(csiIdTokenKey);
}

Do not forget it's just workaround!!!

Will this be added to the android sdk any soon? the iOS SDK already contains a public method as mentioned above. Is there any timeline on this?

I'm facing the same problem with version 2.6.18 of the SDK. Specifically, I'm trying to detect if the user is logged in while the app is offline (e.g. due to lack of network signal).

My test steps are:

  1. Run app and sign up/log in
  2. Close app
  3. Put device into Flight mode
  4. Repeatedly: Launch app / wait for app to load / close app

Expected behaviour is that the app will always bypass the sign up/log in UI and go straight to the content.

I tried the workaround from @guillaume-tgl but it only works first time I run the app: after that, CognitoCachingCredentialsProvider.clearCredentials() has been called and the SharedPreferences key mentioned in the workaround doesn't exist.

Is there a better solution available?

@guillaume-tgl getCachedSession check Threshold your workaround not

the fix can be just make "getCachedSession" pubblic!! why not? 1 year and half for what?

the best workaround is:

final StringBuilder sb = new StringBuilder();
getCognitoUserPool().getCurrentUser().getSession(new AuthenticationHandler() {
    @Override public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
        sb.append(userSession.getIdToken().getJWTToken());
    }

    @Override public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
    }

    @Override public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
    }

    @Override public void authenticationChallenge(ChallengeContinuation continuation) {
    }

    @Override public void onFailure(Exception e) {
        e.printStackTrace();
    }
});
if (sb.length() != 0)
    return sb.toString();
else
    return null;

getSession is sync so callbacks i ever called before going on

PS remember to call this in a asnyc thread

Hi,

We are looking into adding isSignedIn method to the CognitoUser.

@guillaume-tgl @bvirlet @keshavkaul @kulik @roshanrajaratnam @markwhitaker @xanscale
I work for the AWS Mobile team and we're working on a comprehensive solution to Auth with Cognito in our SDK that should handle this use case. If you'd like to be part of the testing in the next couple weeks please email me richthr_AT_amazon.com (replace the _AT_ with @).

We (@bvirlet and I) don't use Cognito anymore but thanks for the offer.

@undefobj just to know: why not make getCachedSession public?

EDIT: i will send you an email

@xanscale Thank you for your workaround. Saved my day!

Hi @guillaume-tgl @bvirlet @keshavkaul @kulik @roshanrajaratnam @markwhitaker @xanscale @akashnigam010 ,

We have launched a new AWSMobileClient in v2.8.+ , which will work with Cognito Userpools and allow you to access methods like isSignedIn().

A full details available in the Release notes and Documentation

Thank you.
Great work!
Will be testing soon.

Akash.

On Wed, 14 Nov 2018, 2:40 am minbi, notifications@github.com wrote:

Hi @guillaume-tgl https://github.com/guillaume-tgl @bvirlet
https://github.com/bvirlet @keshavkaul https://github.com/keshavkaul
@kulik https://github.com/kulik @roshanrajaratnam
https://github.com/roshanrajaratnam @markwhitaker
https://github.com/markwhitaker @xanscale https://github.com/xanscale
@akashnigam010 https://github.com/akashnigam010 ,

We have launched a new AWSMobileClient in v2.8.+ , which will work with
Cognito Userpools and allow you to access methods like isSignedIn().

A full details available in the Release notes
https://github.com/aws-amplify/aws-sdk-android/releases/tag/release_v2.8.0
and Documentation
https://aws-amplify.github.io/docs/android/authentication

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aws-amplify/aws-sdk-android/issues/260#issuecomment-438437796,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AXuo6_UPLfWamWmOj4N50RYkU2J32JtNks5uuzVdgaJpZM4MPq5p
.

Hi,

We are closing this issue because there has been no activity. Please feel free to open a new issue if the problem persists. We ask this because closed issues are not actively monitored.

Thanks

@minbi i updated to new 2.8.5 but i have same problem. (not sure)

how i can get token synchronously to put http call inside something like interceptor ?

actually i use this workaround and works but seams over complicated
https://github.com/aws-amplify/aws-sdk-android/issues/260#issuecomment-410338918

why not make getCachedSession public?

We have launched a new AWSMobileClient in v2.8.+ , which will work with Cognito Userpools
Please read the documentation linked below for full usage.

try {
    Tokens tokens = AWSMobileClient.getInstance().getTokens();
} catch (Exception e) {
    e.printStackTrace();
}

A full details available in the Release notes and Documentation

Was this page helpful?
0 / 5 - 0 ratings