-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for proactive token validation #110
Comments
Option 3: Trigger shouldRefresh both before & after request @felangel
Example: Fresh<OAuth2Token>(
tokenStorage: tokenStorage,
refreshToken: (token, client) async {
// Refresh token logic
},
shouldRefresh: (response, token) {
final now = DateTime.now().millisecondsSinceEpoch / 1000;
final issuedAt = await getIssuedAtFromStorage();
if(token.expiresIn != null && issuedAt != null) {
if(now >= (issuedAt + token.expiresIn)) return true;
}
// GraphQL or REST should refresh logic
return response.errors?.first.extensions?['code'] ==
'AUTH_NOT_AUTHENTICATED';
},
); |
I opened #112 to attempt to refresh expired tokens proactively. Let me know if that addresses your concerns and/or if you have any feedback on the API, thanks! 🙏 |
Thank you so much for this fast PR @felangel 🙂 Question how would people that want to persist the storage (for example : use |
Hello @felangel following back on this. I've been using a fork of your draft PR branch, in a production app with 30k monthly users for a few months now & it's been working great ! I think it could be merged as is. We could look into the improvement mentioned in the PR comments later but not necessary for the merge |
Description:
The
Fresh
package currently supports reactive token refresh via theshouldRefresh
callback, which is triggered after requests. While this approach works well, many use cases (e.g., OAuth2 withexpiresIn
) can benefit from proactive token validation, where tokens are validated and refreshed before requests are made.Proactively validating tokens:
This feature request proposes two potential solutions to enable proactive token validation in
Fresh
.Proposed Solutions:
Here’s the updated GitHub issue with Option 1 (developer-managed logic) and Option 2 (built-in expiration handling with
refreshIfExpired
and enhancedOAuth2Token
).Option 1: Developer-Managed Expiration Logic
Introduce a
validateTokenBeforeRequest
callback, giving developers complete control over token expiration handling.Example:
How It Works:
Fresh
callsvalidateTokenBeforeRequest
before every request.expiresIn
and a customissuedAt
field.Fresh
triggers therefreshToken
callback to refresh it.Pros:
OAuth2Token
orFresh
internals.Cons:
issuedAt
themselves.Option 2 Built-In Expiration Handling with Enhanced
OAuth2Token
Introduce a
refreshIfExpired
boolean and enhanceOAuth2Token
with an optionalissuedAt
field for built-in expiration logic.Example:
How It Works:
Fresh
usesexpiresIn
andissuedAt
(if provided) to calculate the token's expiration time:If the token is expired or nearing expiration,
Fresh
refreshes it before sending the request.If
issuedAt
is not provided, developers can handle expiration manually viashouldRefresh
.Pros:
refreshIfExpired
and letFresh
handle expiration logic.issuedAt
for precise expiration handling or rely on default behavior.Cons:
OAuth2Token
class to include an optionalissuedAt
field.Fresh
internals.The text was updated successfully, but these errors were encountered: