react-keycloak / react-native-keycloak

React Native components for Keycloak
MIT License
171 stars 48 forks source link

keycloak.updateToken(50) The token is not refreshing the first time. #93

Open RaviDhakadDoodle opened 2 years ago

RaviDhakadDoodle commented 2 years ago

keycloak.updateToken(50)

I want to refresh the token every minute. So I have added code :- keycloak.onTokenExpired = () => { keycloak.updateToken(50) .then((refreshed) => { if (refreshed && keycloak.token) { console.log('Token refreshed'); } }) .catch(() => { Alert. alert('Failed to refresh the token, or the session has expired.'); });

But it's not working as expected. For example, I have a token expiration time of 1 minute so it will call after 1 * 2 = 2 minutes only for the first time after that it works every minute.

To Reproduce

Expected behavior

Screenshots

Smartphone

Additional context

letiendungdn commented 2 years ago

use in redux const refreshToken = (state: any) => { console.log('refresh token') keycloak.init({ refreshToken: state.refreshToken, token: state.token, idToken: state.idToken, redirectUri: 'demo-app://demo', }) keycloak.onTokenExpired = () => { keycloak .updateToken(30) .then(() => { state.refreshToken = keycloak.refreshToken state.token = keycloak.token state.idToken = keycloak.idToken console.log('successfully get a new token') }) .catch(err => { console.log(err) }) } }