Skip to content

Instantly share code, notes, and snippets.

@samuelchanx
Created February 11, 2025 04:08
Show Gist options
  • Select an option

  • Save samuelchanx/bcb99c5c0f29425f2c99207a57727ab5 to your computer and use it in GitHub Desktop.

Select an option

Save samuelchanx/bcb99c5c0f29425f2c99207a57727ab5 to your computer and use it in GitHub Desktop.
Flutter retry mechanism for getting user token
Future<Map<String, String>?>? getHeader() async {
final authenticated = auth.user != null;
if (!authenticated) {
return null;
}
var currentToken = await auth.user!.getIdToken();
var decodedToken = JwtDecoder.decode(currentToken!);
var retryTimes = 0;
while (decodedToken['https://hasura.io/jwt/claims'] == null &&
retryTimes < 10) {
logger.info('force refresh token');
await Future.delayed(const Duration(milliseconds: 2500));
currentToken = await auth.user!.getIdToken(true);
decodedToken = JwtDecoder.decode(currentToken!);
retryTimes++;
}
return {
'Authorization': 'Bearer $currentToken',
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment