Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 55b1ab8

Browse files
Merge pull request #1145 from CaptainDingle/getCustomClaims
Added option to retrieve Firebase custom claims - resolves #1008
2 parents 3ee2b51 + 6bf6951 commit 55b1ab8

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

docs/AUTHENTICATION.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ To solve, you will want to pass in the appropriate iOS controller of the active
649649
If you want to authenticate your user from your backend server you can obtain
650650
a Firebase auth token for the currently logged in user.
651651

652+
You can choose to get just the token itself or the token plus any custom claims you may have previously set via the Firebase Admin SDK as outlined [here](https://firebase.google.com/docs/auth/admin/custom-claims):
653+
654+
Just token:
655+
652656
```js
653657
firebase.getAuthToken({
654658
// default false, not recommended to set to true by Firebase but exposed for {N} devs nonetheless :)
@@ -663,6 +667,29 @@ a Firebase auth token for the currently logged in user.
663667
);
664668
```
665669

670+
Token with custom claims:
671+
672+
```js
673+
firebase.getAuthToken({
674+
// default false, not recommended to set to true by Firebase but exposed for {N} devs nonetheless :)
675+
forceRefresh: false,
676+
// set to true to also retrieve your custom claims
677+
withClaims: true
678+
}).then(
679+
function (result) {
680+
// for both platforms
681+
console.log("Auth token retrieved: " + result.token);
682+
// Android only
683+
console.log("Specific custom claim retrieved: " + result.claims.get('yourClaimKey'));
684+
// iOS only
685+
console.log("Specific custom claim retrieved: " + result.claims.objectForKey('yourClaimKey'));
686+
},
687+
function (errorMessage) {
688+
console.log("Auth result retrieval error: " + errorMessage);
689+
}
690+
);
691+
```
692+
666693
### logout
667694
Shouldn't be more complicated than:
668695

src/firebase.android.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,18 @@ firebase.getAuthToken = arg => {
704704
if (user !== null) {
705705
const onSuccessListener = new gmsTasks.OnSuccessListener({
706706
onSuccess: getTokenResult => {
707-
resolve(getTokenResult.getToken());
707+
if (arg.withClaims) {
708+
/* get token and custom claims previously set via the Firebase Admin SDK. */
709+
resolve(
710+
{
711+
token: getTokenResult.getToken(),
712+
claims: getTokenResult.getClaims()
713+
}
714+
);
715+
} else {
716+
/* get just token without custom claims */
717+
resolve(getTokenResult.getToken());
718+
}
708719
}
709720
});
710721

src/firebase.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export interface GetAuthTokenOptions {
202202
* Default false.
203203
*/
204204
forceRefresh?: boolean;
205+
/* Set this to true if you want any custom claims returned that you previously set via the Firebase Admin SDK. */
206+
withClaims?: boolean;
205207
}
206208

207209
export interface Provider {
@@ -873,7 +875,7 @@ export function reauthenticate(options: ReauthenticateOptions): Promise<any>;
873875

874876
export function reloadUser(): Promise<void>;
875877

876-
export function getAuthToken(option: GetAuthTokenOptions): Promise<string>;
878+
export function getAuthToken(option: GetAuthTokenOptions): Promise<any>;
877879

878880
export function logout(): Promise<any>;
879881

src/firebase.ios.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,13 @@ firebase.getAuthToken = arg => {
680680
resolve(token);
681681
}
682682
};
683-
user.getIDTokenForcingRefreshCompletion(arg.forceRefresh, onCompletion);
683+
/* get token and custom claims previously set via the Firebase Admin SDK. */
684+
if(arg.withClaims) {
685+
user.getIDTokenResultForcingRefreshCompletion(arg.forceRefresh, onCompletion);
686+
} else {
687+
/* get just token without custom claims */
688+
user.getIDTokenForcingRefreshCompletion(arg.forceRefresh, onCompletion);
689+
}
684690
} else {
685691
reject("Log in first");
686692
}

0 commit comments

Comments
 (0)