fetchToken static method
Returns a token for remote access.
decrypter
When the old tokens are read from local storage, they are
decrypted. This parameter allows you to provide your own decryption
method. This will be used in addition to the default decryption method.
This will be performed after the default decryption method.
If the token is not found, returns null
.
May throw ExpiredAccessTokenException if the token is expired. If this happens, refresh the token with refreshRemoteToken.
Implementation
static Future<String?> fetchToken({
String Function(String ciphertext)? decrypter,
}) async {
Map<String, dynamic>? storedTokenData =
await fetchTokenData(decrypter: decrypter);
if (storedTokenData == null) return null;
String? token = storedTokenData[ApiFields.accessToken];
String? expirationDateStr = storedTokenData[ApiFields.expirationDate];
if (token == null || expirationDateStr == null) return null;
DateTime? expirationDate = DateTime.tryParse(expirationDateStr);
if (expirationDate == null) return null;
// If the token is expired, throw an exception.
if (DateTime.now().isAfter(expirationDate)) {
throw const ExpiredAccessTokenException();
}
return token;
}