onAccessTokenRefreshed
🧠Overview
The onAccessTokenRefreshed
event is triggered by the REL-ID SDK when a new JWT access token is issued. This ensures that session continuity is maintained without requiring the user to re-authenticate.
The event listener provides a base64-encoded JWT token which the application must extract and use for subsequent authenticated requests.
🎯 Purpose
To securely refresh a user's session without reauthentication, ensuring uninterrupted user experience across app usage.
📱 UI Guidance
The app does not need to present any UI to the user when this event occurs. This is a background process. However, developers must ensure that all subsequent SDK/API calls use the refreshed token.
📩 Event Payload
Sample payload from the event:
{
"accessTokenInfo": "{\"access_token\":\"<token>\",\"token_type\":\"bearer\",\"expires_in\":66399,\"scope\":\"all\",\"jti\":\"<JWT ID>\"}",
"status": {
"statusCode": 100,
"statusMessage": "Success"
},
"error": {
"longErrorCode": 0,
"shortErrorCode": 0,
"errorString": "Success"
}
}
📦 Payload Fields
Field | Description |
---|---|
access_token | The new JWT token string |
token_type | Usually bearer |
expires_in | Time in seconds before token expires |
scope | Access scope, e.g., "all" |
jti | JWT ID, unique identifier of token |
💻 Sample Code (Platform-Specific)
React Native
let subscription = rdnaEventRegistery.addListener('onAccessTokenRefreshed', this.onAccessTokenRefreshed.bind(this));
onAccessTokenRefreshed(response) {
const tokenInfo = JSON.parse(response.accessTokenInfo);
console.log("Access Token:", tokenInfo.access_token);
}
Flutter
rdnaClient.on(RdnaClient.onAccessTokenRefreshed, (String response) {
var tokenData = jsonDecode(response);
print("Access Token: ${tokenData['access_token']}");
});
Cordova
document.addEventListener('onAccessTokenRefreshed', function(event) {
const tokenData = JSON.parse(event.accessTokenInfo);
console.log("Access Token:", tokenData.access_token);
}, false);
Native Android
void onAccessTokenRefreshed(String accessTokenInfo, RDNARequestStatus requestStatus, RDNAError error) {
JSONObject tokenData = new JSONObject(accessTokenInfo);
String token = tokenData.getString("access_token");
}
Native iOS
- (void)onAccessTokenRefreshed:(NSString *)accessToken status:(RDNARequestStatus *)status error:(RDNAError *)error {
NSDictionary *tokenData = [NSJSONSerialization JSONObjectWithData:[accessToken dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSLog(@"Access Token: %@", tokenData[@"access_token"]);
}
Success
- Cache and use the new access token in API calls.
Failure
- Show a message and consider retrying or logging the user out based on the app’s logic.
📊 Event to API Mapping
There is no API to be called in response to this event. Instead, the new access token must be saved and used automatically in subsequent API requests.
Updated 2 months ago