Reputation: 649
I am currently using Firebase custom authentication and recently I have started to notice that an auth/user-token-expired
error is being received with a message
The user's credential is no longer valid. The user must sign in again.
on the client without any significant change to the user account. What can I do to find the reason for this issue and possible ways to rectify it, so that sign-outs like these don't happen at some awkward state while the user is using the application?
Upvotes: 1
Views: 953
Reputation: 24094
The client code can call User.getIdToken(forceRefresh)
to force a token refresh.
getIdToken ( forceRefresh ? : boolean ) : Promise < string >
Returns a JSON Web Token (JWT) used to identify the user to a Firebase service.
Returns the current token if it has not expired. Otherwise, this will refresh the token and return a new one.
Parameters
- Optional forceRefresh: boolean
Force refresh regardless of token expiration.
Retrieve ID tokens on clients
When a user or device successfully signs in, Firebase creates a corresponding ID token that uniquely identifies them and grants them access to several resources, such as Firebase Realtime Database and Cloud Storage. You can re-use that ID token to identify the user or device on your custom backend server. To retrieve the ID token from the client, make sure the user is signed in and then get the ID token from the signed-in user:
firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) { // Send token to your backend via HTTPS // ... }).catch(function(error) { // Handle error });
Upvotes: 1