firebase / firebase/firebase-js-sdk

FR: Document User.getIdToken() Promise rejections

Open
#4,358 1 comment 1 reaction 0 assignees View on GitHub
api: auth feature request
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### [REQUIRED] Describe your environment

* Operating System version: N/A
* Browser version: N/A
* Firebase SDK version: 8.2.3
* Firebase Product: auth

### [REQUIRED] Describe the problem

[firebase.User.getIdToken()'s docs](https://firebase.google.com/docs/reference/js/firebase.User#getidtoken) do not describe how the Promise it returns can reject. I'm guessing at least the following cases can exist:

- The user was deleted
- The user was disabled
- The admin SDK revoked their refresh token
- Any internal error

Any app that has non-Firebase backend APIs and needs to authenticate those API requests needs to call `getIdToken()` on the client. They may be able to get away without addressing when the Promise rejects for a while, at least until they need to deactivate spammers, revoke a user's refresh token due to suspicious activity, etc.

Reading through the source, it appears there could be more cases, and it's not clear how to identify these errors on the client (what are the `error.code` values?). I'm looking at [fireauth.AuthUser.prototype.getIdToken](https://github.com/firebase/firebase-js-sdk/blob/5380e0012e15d34c19889be9483e1f0ad2eadd8f/packages/auth/src/authuser.js#L1010), which calls into [fireauth.StsTokenManager.prototype.getToken](https://github.com/firebase/firebase-js-sdk/blob/5380e0012e15d34c19889be9483e1f0ad2eadd8f/packages/auth/src/token.js#L293). That method will reject with `fireauth.authenum.Error.TOKEN_EXPIRED` if the refresh token is missing. But it calls into [fireauth.RpcHandler.prototype.requestStsToken](https://github.com/firebase/firebase-js-sdk/blob/5380e0012e15d34c19889be9483e1f0ad2eadd8f/packages/auth/src/rpchandler.js#L782) which can throw `fireauth.authenum.Error.NETWORK_REQUEST_FAILED` as well as any error from [fireauth.RpcHandler.getDeveloperError_](https://github.com/firebase/firebase-js-sdk/blob/5380e0012e15d34c19889be9483e1f0ad2eadd8f/packages/auth/src/rpchandler.js#L2750), which is just a general error translation function and not really helpful for understanding how `requestStsToken` can fail.

Given that `getIdToken()`'s Promise rejecting doesn't automatically sign out a user, might also be good to include a note on how to best handle each error (i.e. signing out the user yourself or prompting them for their password).

It'd be helpful to update the sample code here https://firebase.google.com/docs/auth/admin/verify-id-tokens#retrieve_id_tokens_on_clients with the various situations as well.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.