[ad_1]
I’m creating a react-native utility which can use firebase authentication, and I’ve some doubts about how refresh tokens ought to work.
I’m utilizing the react native package deal https://rnfirebase.io/ "@react-native-firebase/auth": "^14.9.0" and thus far all the pieces works nice (login/account-creation, password reset circulate, and many others)
Nevertheless, I’ve doubts about how the idToken needs to be refreshed.
From the documentation, and several other posts (post1, post2 with comparable subject, post3, and many others), my understanding is that the react native package deal makes use of the android & ios SDK which needs to be managing token refreshes for me and dispatching the onIdTokenChanged occasion when the idToken expires and is refreshed. I learn that the idToken expires in 1h, so I’d anticipate the token to be refreshed each hour.
In follow nonetheless I do not see this habits.
Here’s a minimal reprod of what I arrange in app with logging when the onIdTokenChanged occasion is dispatched, and when the idToken really expires
const [ user, setUser ] = useState(null)
// Listens to onIdTokenChanged
useEffect(() => {
auth().onIdTokenChanged((_user) => {
console.log('auth//check//onIdTokenChanged')
setUser(_user)
})
}, [])
// Units a timer on idToken expiry
useEffect(() => {
if (!consumer) {
return
}
(async operate () {
const idTokenResult = await consumer.getIdTokenResult()
const expirationDate = new Date(idTokenResult.expirationTime)
const expiresIn = expirationDate.getTime() - Date.now()
console.log(`auth//check//tokenWillExpireIn:${expiresIn / 60000}min`)
setTimeout(() => {
console.log('auth//check//tokenExpired')
}, expiresIn)
})()
}, [ user ])
And listed below are the ensuing logs after login:
2022-05-13T09:47:05.156Z: auth//check//onIdTokenChanged
2022-05-13T09:47:05.238Z: auth//check//onIdTokenChanged
2022-05-13T09:47:05.274Z: auth//check//tokenWillExpireIn:59.09545min
2022-05-13T09:47:05.358Z: auth//check//tokenWillExpireIn:59.094033333333336min
2022-05-13T10:46:11.015Z: auth//check//tokenExpired
2022-05-13T10:46:11.018Z: auth//check//tokenExpired
END
Result’s: onIdTokenChanged is dispatched on app begin twice, then an hour later the token expires, however why is the onIdTokenChanged is rarely dispatched ?
Am I establishing one thing incorrectly/did I misunderstand the documentation?
Thanks prematurely on your assist!
[ad_2]
