Amplify-android: How can I refresh id token manually?

Created on 21 Oct 2020  路  7Comments  路  Source: aws-amplify/amplify-android

I want to update the user attribute and get a new token with updated params. I need it updated since our server takes some params from it.

How can I refresh the token so it will contain updated params? Also, I'm not sure should this token be refreshed automatically after parameter update?

Found similar issue in the AWS SDK: https://github.com/aws-amplify/aws-sdk-android/issues/696. It has a SharedPreferences workaround. Not sure if it will work with Amplify? What exact prefs do I need to clear? Thanks.

Amplify Auth version: 1.4.1

Auth Feature Request

Most helpful comment

Sorry guys, you're correct - that does not achieve what you're looking for and I see this is still a pending feature request for AWSMobileClient which Amplify Auth is currently based on. You can add your +1s there as well to help get it prioritized and reopened this so others can add +1s to this.

All 7 comments

Hey @AliakseiIvanou - currently the way to achieve this use case is to get an instance of the AWSMobileClient escape hatch and call the refresh() method on it. The token cannot be refreshed automatically by us since we have no way of knowing when you update the params on the server side.

I used the refresh() method and now i am getting "The security token included in the request is invalid" message with AmazonServiceException. It only happens when tokens have to be refreshed.

@TrekSoft your provided answer does not really work.
(Amplify.Auth.getPlugin("awsCognitoAuthPlugin").escapeHatch as AWSMobileClient).refresh()
returns same token values. I did try launching it on separate threads with delays, etc. I still get same results.

Sorry guys, you're correct - that does not achieve what you're looking for and I see this is still a pending feature request for AWSMobileClient which Amplify Auth is currently based on. You can add your +1s there as well to help get it prioritized and reopened this so others can add +1s to this.

Meanwhile waiting for the official way to do this, is there a walkaround you are aware of? Cuz i have my app published and im struggling with this problem.

I noticed that when Amplify is initialized every time the app starts, it refreshes the tokens or does something that helps with the problem. So when it happen that tokens expire, relaunching the app fixes it, but this fix is far from ideal.

Sorry no I'm not aware of a workaround other than signing out and back in which obviously probably doesn't work for your situation.

@baltekgajda there is a workaround, but it will require you using lambdas. We shoot a request to our lambda with active identity token and get a custom challenge answer and session in the response.
Then we use RespondToAuthChallengeRequest from the AWSMobileClient, provide session, challenge answer there and call it on Cognito user. After that the next call to Amplify.Auth.fetchAuthSession will return new tokens.

Was this page helpful?
0 / 5 - 0 ratings