Skip to content

Add tokenExpirationRefreshBufferMs as a prop for fetchAuthSession #14584

@ChristopherGabba

Description

@ChristopherGabba

Is this related to a new or existing framework?

React Native

Is this related to a new or existing API?

Authentication

Is this related to another service?

No response

Describe the feature you'd like to request

Currently when you call fetchAuthSession, it checks the local cache (AsyncStorage in the case of react native) and checks if the tokens are expired, and if they are it refreshes them.

The fault I have with this is that there is a chance that when a user opens the app, I call fetchAuthSession and the tokens expire in 10 seconds, but they don't get refreshed like they should because technically the pass. Now the user has the app open for longer than 10 seconds and they are operating with expired tokens.

This is problematic because then there is a chance when I call uploadData or getUrl or any other amplify API like client.models.User.get() that the token is expired (at least this is how I understand it?)

Describe the solution you'd like

The solution I would like is when you call fetchAuthSession on app mount, you have a prop that you can basically make the function call refresh the tokens if they are within a certain amount of time from expiring. Example API:

await fetchAuthSession({
   tokenExpirationRefreshBufferMs: 10 * 60 * 1000 // 10 Minutes - go ahead and refresh the tokens if we are within 10 minutes from expiring.
})

Now if the idToken or accessToken is within 10 minutes from expiring, it runs a refresh.

Describe alternatives you've considered

I do not want to have to call:

   await fetchAuthSession({
       forceRefresh: true
    })

every time the app mounts because its actually a very expensive function call in react-native.

I've also considered every time that I call uploadData or any other API, I call fetchAuthSession() first, which is also very expensive (adds a half second or more to all these function calls -- refer to below).

Additional context

I did some testing with fetchAuthSession on a brand new iPhone 17 Pro Max (best of the best) and calling the function without forceRefresh takes about 400-500 ms (half a second just to check the cache!!) and calling it with forceRefresh takes 800 - 900 ms (almost a full second).

This goes back to a feature request that I proposed previously. AsyncStorage is the slowest key-value storage in React Native currently. Amplify should allow the dev to use any alternative storage and I could make this fetchAuthSession 30 times faster:

#14533

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions