skip to Main Content

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("[email protected]");

    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

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

Firebase Github

2

Answers


  1. Looking at that doc, it seems you can set claims by specifying a Map with the new values (ie. no need to specify values you are not modifying).

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search