Created with <3 with dartpad.dev.
Created
August 5, 2023 04:03
-
-
Save Jerome-Jumah/4739713790d2e158d4ed04e6bcc9eb2c to your computer and use it in GitHub Desktop.
Refresh Token Logic
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
import 'dart:async'; | |
void main() { | |
const monitor = 3000; // 3s | |
Timer.periodic(Duration(seconds: monitor), (timer){ | |
print('Timer $timer'); | |
refreshTokenChk(monitor); | |
}); | |
} | |
void refreshTokenChk(int monitor) { | |
print('Refresh Monitor Checker'); | |
const refreshTokenExpiresIn = 1696390306055; // 90 days | |
const accessTokenExpiresIn = 1691209903; // 1 hour | |
var refreshTokenExp = DateTime.now().millisecond + refreshTokenExpiresIn; | |
var accTokenExp = DateTime.now().millisecond + accessTokenExpiresIn; | |
//Checks if refreshToken has expired | |
if(DateTime.now().millisecond > refreshTokenExp) { | |
print('RefreshToken Expired'); | |
//Need to log out the user to get new tokens | |
} | |
// Calculate the adjusted expiration time (2 minutes before actual expiration) | |
var accTokenExpAdjusted = accTokenExp - (2 * 60 * 1000); | |
if (DateTime.now().millisecondsSinceEpoch > accTokenExpAdjusted + monitor) { | |
print('Access Token about to expire'); | |
// Refresh Access Token before it expires | |
} | |
if(DateTime.now().millisecond > accTokenExp + monitor){ | |
print('Access Token expired'); | |
// Need to refresh the accesss token | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment