Open optislav opened 7 months ago
We can use an external library (e.g., jwt-decode) to validate the Firebase token. So even though we can't get the token from the Firebase SDK synchronously, this barrier will get it asynchronously if the token is undefined.
const FIVE_MINUTES_MS = 5 * 60 * 1000 // Firebase default behavior to fetch new token if 5 minutes left
const isTokenAlmostExpired = (jwt: string | null) => {
if (!jwt) return true
const { exp } = jwtDecode(jwt)
if (!exp) return true
return Date.now() + FIVE_MINUTES_MS > exp * 1000
}
export const authBarrier = createBarrier({
active: combine($authToken, (token) => isTokenAlmostExpired(token)),
perform: [getTokenMutation],
})
In the docs, we have this example:
However, there is no way to check if the token is valid synchronously in Firebase, as getIdToken() always returns a promise. https://firebase.google.com/docs/reference/js/v8/firebase.User#getidtoken
Maybe active should also accept an effect that returns a boolean?