Created
February 11, 2025 04:08
-
-
Save samuelchanx/bcb99c5c0f29425f2c99207a57727ab5 to your computer and use it in GitHub Desktop.
Flutter retry mechanism for getting user token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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