callRefreshTokenApi method
dynamic
callRefreshTokenApi()
Implementation
callRefreshTokenApi() async {
try {
final response = await dio.post(
'${ApiConstant.keycloakUrl}realms/${ApiConstant.realm}/protocol/openid-connect/token',
options: Options(
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
),
data: {
"client_id": ApiConstant.clientId,
"client_secret": ApiConstant.clientSecret,
"grant_type": "refresh_token",
"refresh_token": SecureStorageService.readSecureData(
SecureStorageService.refreshToken,
),
},
);
if (response.statusCode == 200) {
final responseBody = response.data;
final accessToken = responseBody['access_token'];
ConditionalLogs().customLog('accessToken: $accessToken');
final refreshToken = responseBody['refresh_token'];
ConditionalLogs().customLog('refreshToken: $refreshToken');
// final idToken = responseBody['id_token'];
// ConditionalLogs().customLog('idToken: $idToken');
if (accessToken != null) {
await SecureStorageService.writeSecureData(
SecureStorageService.accessToken,
accessToken ?? "",
);
// storage.write("ID_TOKEN", idToken);
await SecureStorageService.writeSecureData(
SecureStorageService.refreshToken,
refreshToken ?? "",
);
startTimer();
// await callTokenApi();
} else {
ConditionalLogs()
.customLog('Token is null. Status code: ${response.statusCode}');
clearData();
}
} else {
ConditionalLogs().customLog(
'Failed to exchange code for token. Status code: ${response.statusCode}');
ConditionalLogs().customLog('Response body: ${response.data}');
// callRefreshTokenApi();
clearData();
}
} catch (e) {
ConditionalLogs()
.customLog('An error occurred while exchanging code for token: $e');
// if (tokenRecieved) {
// await callTokenApi();
// } else {
// MainController mainController = Get.put(MainController());
// mainController.showErrorPopup();
// }
clearData();
}
}