Open OrmEmbaar opened 1 week ago
Hello, @OrmEmbaar and thanks for creating this feature request. I'll review it with the team internally and follow up with any additional questions we have.
@cwomack I think I have encountered the same problem while using Tanstack Query with NextJs. After session tokens have expired and Tanstack Query is trying to refetch the data the server multiplies the cookies and tokens as presented below:
Minimum repository with reproduction: https://github.com/mkolbusz/nextjs-amplify-v6-issues
Steps to reproduce:
This problem does not allow to sign out properly.
It looks like the library already has tools needed to make this work. This deDupeAsyncFunction
function is used to wrap the refreshToken
method.
But that is only called when refreshing tokens, not when fetching credentials. To de-duplicate initial credential fetch, the getId
function would also need to be wrapped by deDupeAsyncFunction
:
It seems like an easy change. Can we make this happen?
@mkolbusz This appears to be a bug on the Amplify side unrelated to deduping due to setting different "path" values on refresh, we're working on a fix. In the mean time, would you mind creating a new ticket to track the issue?
@jimblanc issue created: https://github.com/aws-amplify/amplify-js/issues/13509
Is this related to a new or existing framework?
No response
Is this related to a new or existing API?
Authentication, PubSub
Is this related to another service?
Cognito
Describe the feature you'd like to request
The
fetchAuthSession
singleton should deduplicate requests to fetch credentials from the server.We are in the process of upgrading from aws-amplify v4 to v6. On initial page load, we fetch data from our API using signed Authorization headers and set-up subscriptions using the Amplify PubSub library. For requests to our own server we are calling
fetchAuthSession
manually to get the credentials to create the signature. For subscriptions,fetchAuthSession
is being called internally by the Amplify PubSub library.As this all happens concurrently on initial page load, the
fetchAuthSession
singleton has not yet populated its internal cache with any credentials. We are therefore hitting the Cognito server 20 to 30 times on page load, which is unnecessary network load and causes us to be rate limited by the Cognito server (this pattern was working fine in v4).We therefore would like to see the
fetchAuthSession
singleton de-duplicate concurrent in-flight requests to fetch credentials from the Cognito server.Describe the solution you'd like
The aws-amplify library should await any in-flight requests to the Cognito server instead of making duplicate concurrent requests.
Describe alternatives you've considered
Our present solution is to wrap
fetchAuthSession
inside auseQuery
hook from the ReactQuery library. ReactQuery will de-duplicate requests for us, so we can pass the returnedrefetch
method around our application without worrying about sending redundant, duplicate requests. A similar approach could possibly be achieved by wrapping it in a debounce.To make that work for the PubSub library, we have been forced to extend the
PubSub
class and inject a customendpoint
method that uses our de-duplicatedfetchAuthSession
method.This is all messy. It adds a lot of code and boilerplate which shouldn't be necessary.
Additional context
No response
Is this something that you'd be interested in working on?