Skip to content

Commit

Permalink
Feature: Add support ios and android support for getClaims, which ret…
Browse files Browse the repository at this point in the history
…urns all claims
  • Loading branch information
Zoe committed Apr 1, 2022
1 parent 751ba5a commit b7d067e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ To help ensure this plugin is kept updated, new features are added and bugfixes
- [linkUserWithCredential](#linkuserwithcredential)
- [reauthenticateWithCredential](#reauthenticatewithcredential)
- [registerAuthStateChangeListener](#registerauthstatechangelistener)
- [useAuthEmulator](#useAuthEmulator)
- [getClaims](#getClaims)
- [Remote Config](#remote-config)
- [fetch](#fetch)
- [activateFetched](#activatefetched)
Expand Down Expand Up @@ -2937,6 +2939,32 @@ FirebasePlugin.useAuthEmulator('localhost', 9099, function() {
});
```

### getClaims
Returns the entire payload claims of the ID token including the standard reserved claims as well as the custom claims (set by developer via Admin SDK).


**Parameters**:
- {function} success - callback function to pass claims {object} to as an argument
- {function} error - callback function which will be passed a {string} error message as an argument

Example usage:

```javascript
FirebasePlugin.getClaims(function(claims) {
// reserved claims
console.log("email", claims.email);
console.log("email_verified", claims.email_verified);
console.log("name", claims.name);
console.log("user_id", claims.user_id);

//custom claims
console.log("exampleClaimA", claims.exampleClaimA);
console.log("exampleClaimB", claims.exampleClaimB);
}, function(error) {
console.error("Failed to enable the Firebase Authentication emulator", error);
});
```

## Remote Config

### fetch
Expand Down
32 changes: 32 additions & 0 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.deleteUser(callbackContext, args);
} else if (action.equals("useAuthEmulator")) {
this.useAuthEmulator(callbackContext, args);
} else if (action.equals("getClaims")) {
this.getClaims(callbackContext, args);
} else if (action.equals("startTrace")) {
this.startTrace(callbackContext, args.getString(0));
} else if (action.equals("incrementCounter")) {
Expand Down Expand Up @@ -1779,6 +1781,36 @@ public void run() {
});
}

public void getClaims(final CallbackContext callbackContext, final JSONArray args){
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

user.getIdToken(true).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
try {
Map<String, Object> claims = result.getClaims();
JSONObject returnResults = new JSONObject(claims);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, returnResults));
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
handleExceptionWithContext(e, callbackContext);
}
});
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
}
});
}

//
// Firebase Performace
//
Expand Down
1 change: 1 addition & 0 deletions src/ios/FirebasePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- (void)sendUserPasswordResetEmail:(CDVInvokedUrlCommand*)command;
- (void)deleteUser:(CDVInvokedUrlCommand*)command;
- (void)useAuthEmulator:(CDVInvokedUrlCommand*)command;
- (void)getClaims:(CDVInvokedUrlCommand*)command;

// Remote notifications
- (void)getId:(CDVInvokedUrlCommand*)command;
Expand Down
29 changes: 26 additions & 3 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ - (void)pluginInitialize {

// Check for permission and register for remote notifications if granted
[self _hasPermission:^(BOOL result) {}];


authCredentials = [[NSMutableDictionary alloc] init];
firestoreListeners = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -738,15 +738,15 @@ - (void)authenticateUserWithGoogle:(CDVInvokedUrlCommand*)command{
[GIDSignIn.sharedInstance signInWithConfiguration:googleSignInConfig presentingViewController:self.viewController callback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
__auto_type strongSelf = weakSelf;
if (strongSelf == nil) { return; }

@try{
CDVPluginResult* pluginResult;
if (error == nil) {
GIDAuthentication *authentication = user.authentication;
FIRAuthCredential *credential =
[FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
accessToken:authentication.accessToken];

NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
NSString *idToken = user.authentication.idToken;
NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -1175,6 +1175,29 @@ - (void)useAuthEmulator:(CDVInvokedUrlCommand *)command {
}
}

- (void)getClaims:(CDVInvokedUrlCommand *)command {
@try {
FIRUser* user = [FIRAuth auth].currentUser;

if(!user){
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No user is currently signed"] callbackId:command.callbackId];
return;
}

[user getIDTokenResultWithCompletion:^(FIRAuthTokenResult * _Nullable tokenResult, NSError * _Nullable error) {
if(error != nil){
[self sendPluginErrorWithError:error command:command];
return;
}

[self sendPluginDictionaryResult:tokenResult.claims command:command callbackId:command.callbackId];
}];

}@catch (NSException *exception) {
[self handlePluginExceptionWithContext:exception :command];
}
}

/*
* Analytics
*/
Expand Down
4 changes: 4 additions & 0 deletions www/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ exports.useAuthEmulator = function (host, port, success, error) {
exec(success, error, "FirebasePlugin", "useAuthEmulator", [host, port]);
};

exports.getClaims = function (success, error) {
exec(success, error, "FirebasePlugin", "getClaims", []);
};

// Firestore
exports.addDocumentToFirestoreCollection = function (document, collection, success, error) {
if(typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
Expand Down

0 comments on commit b7d067e

Please sign in to comment.