-
Notifications
You must be signed in to change notification settings - Fork 145
Adding Java samples for listUsersAsync() and setCustomUserClaimsAsync() #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
// [START verify_custom_claims] | ||
// Verify the ID token first. | ||
FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get(); | ||
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean.TRUE.equals
seems a bit strange. Why not just if(decoded.getClaims().get("admin"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getClaims()
returns a Map<String, Object>
. So when querying the map we need to deal with possible null values (if the key does not exist), and the returned value not being boolean. Boolean.TRUE.equals()
provides a one-liner that deals with both cases.
.getUserByEmailAsync("[email protected]").get(); | ||
// Add incremental custom claim without overwriting the existing claims. | ||
Map<String, Object> currentClaims = user.getCustomClaims(); | ||
if (Boolean.TRUE.equals(currentClaims.get("admin"))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but I left two comments.
SGTM!
…On Tue, Nov 28, 2017, 6:33 PM Hiranya Jayathilaka ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In auth/src/main/java/com/google/firebase/quickstart/AuthSnippets.java
<#15 (comment)>
:
> + public static void setCustomUserClaims(
+ String uid) throws InterruptedException, ExecutionException {
+ // [START set_custom_user_claims]
+ // Set admin privilege on the user corresponding to uid.
+ Map<String, Object> claims = new HashMap<>();
+ claims.put("admin", true);
+ FirebaseAuth.getInstance().setCustomClaimsAsync(uid, claims).get();
+ // The new custom claims will propagate to the user's ID token the
+ // next time a new one is issued.
+ // [END set_custom_user_claims]
+
+ String idToken = "id_token";
+ // [START verify_custom_claims]
+ // Verify the ID token first.
+ FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get();
+ if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
getClaims() returns a Map<String, Object>. So when querying the map we
need to deal with possible null values (if the key does not exist), and the
returned value not being boolean. Boolean.TRUE.equals() provides a
one-liner that deals with both cases.
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#15 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIEw6mJV64cRcBn2PbX93g9HB_6mgepRks5s7KZPgaJpZM4QuJak>
.
|
@@ -155,6 +232,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc | |||
getUserByEmail("[email protected]"); | |||
getUserByPhoneNumber("+11234567890"); | |||
updateUser("some-uid"); | |||
//setCustomUserClaims("some-uid"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this commented out on purpose ?
Related to firebase/firebase-admin-java#92
Samples to appear on https://firebase.google.com/docs/auth/admin/custom-claims and https://firebase.google.com/docs/auth/admin/manage-users