firebase / quickstart-java

Quickstart samples for Firebase Java Admin SDK
Apache License 2.0
233 stars 143 forks source link

Adding Java samples for listUsersAsync() and setCustomUserClaimsAsync() #15

Closed hiranya911 closed 6 years ago

hiranya911 commented 6 years ago

Related to https://github.com/firebase/firebase-admin-java/pull/92

Samples to appear on https://firebase.google.com/docs/auth/admin/custom-claims and https://firebase.google.com/docs/auth/admin/manage-users

samtstern commented 6 years ago

SGTM!

On Tue, Nov 28, 2017, 6:33 PM Hiranya Jayathilaka notifications@github.com wrote:

@hiranya911 commented on this pull request.

In auth/src/main/java/com/google/firebase/quickstart/AuthSnippets.java https://github.com/firebase/quickstart-java/pull/15#discussion_r153664804 :

  • 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 https://github.com/firebase/quickstart-java/pull/15#discussion_r153664804, or mute the thread https://github.com/notifications/unsubscribe-auth/AIEw6mJV64cRcBn2PbX93g9HB_6mgepRks5s7KZPgaJpZM4QuJak .