Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to modify a users firebase authentication token claims?

How can I update a users claims if the map returned is an immutable map?

This is example code from Firebase Auth docs on how to update claims:

    UserRecord user = FirebaseAuth.getInstance()
        .getUserByEmail("user@admin.example.com");

    Map<String, Object> currentClaims = user.getCustomClaims(); //This returns an immutable map
    if (Boolean.TRUE.equals(currentClaims.get("admin"))) {
      currentClaims.put("level", 10); //This will throw an exception

      FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), currentClaims);
    }

Firebase doc src

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Exception thrown: UnsupportedOperationException: null
at com.google.common.collect.ImmutableMap.put(ImmutableMap.java:468)

Firebase Github

>Solution :

You can simply make a copy of the map to make modifications to it using the HashMap copy constructor.

Map<String, Object> immutableCustomClaims = user.getCustomClaims();
Map<String, Object> mutableCustomClaims = new HashMap<>(immutableCustomClaims)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading