问题
In legacy Firebase API, I was using "mFirebase.getAuth().getExpires()" to validate the user session. But in new Firebase 9.0.0 API, I couldn't find such validation.
Right now I'm checking the availability of Auth session as below,
public static boolean hasValidAuthToken() {
return FirebaseAuth.getInstance().getCurrentUser() != null ? true : false;
}
How can I handle this, if the token got expired?
Update: Actually I'm doing custom Authentication. Having Firebase server sdk(java) at server side. By default, auth token will get expire after 1 hour.
Getting following error after token expired,
D/ConnectionRetryHelper: Scheduling retry in 398ms
D/PersistentConnection: pc_0 - Trying to fetch auth token
D/PersistentConnection: pc_0 - Error fetching token: An internal error has occured. [ Internal error. ]
D/PersistentConnection: pc_0 - Scheduling connection attempt
How can I handle this scenario?
Thanks in advance!
回答1:
Firebase latest version has an error to keep the token "fresh".
In this discussion a Google developer shared a guide to solve this problem.
Guide: https://drive.google.com/file/d/0B94LePkXiqa6SXVFd3N1NzJHX1E/view
It worked for me.
回答2:
The check you are doing should be sufficient for interacting with Firebase Database or Storage, tokens are automatically refreshed by the Authentication SDK.
回答3:
FirebaseAuth.getInstance().getCurrentUser() will return null if the is not authenticated with Firebase. This will happen if the user has signed out, or was never signed in.
The JWT generated server-side for a custom auth has a max lifespan of 60 minutes. If you haven't used it to authenticate the user to Firebase before then, I expect you will see an error like you have mentioned.
Try submitting the token to Firebase before it expires (ie less than 1hr after you generate it). If you haven't auth'd the user against Firebase before then, you will need to get your server to custom generate another token.
See https://firebase.google.com/docs/auth/android/custom-auth for how to submit the custom token to Firebase and authenticate the user.
来源:https://stackoverflow.com/questions/37413195/in-firebase-9-0-0-api-how-to-check-the-user-has-valid-auth-session-or-not