firebase-admin-interop icon indicating copy to clipboard operation
firebase-admin-interop copied to clipboard

Cannot use UserRecord.customClaims

Open Levi-Lesches opened this issue 6 years ago • 1 comments

I tried writing a script to get custom claims and had some trouble reading the server's response. Some observations:

  • According to the Firebase docs, the custom claims are stored as a simple JavaScript Object.
  • According to this package's docs, UserRecord.customClaims is a dynamic.
  • UserRecord.customClaims.runtimeType returns JSObject. Note the capital S. This spelling doesn't match any other class I could find.
  • Trying to cast it to a JsObject from dart:js throws: CastError: Instance of 'PlainJavaScriptObject': type 'PlainJavaScriptObject' is not a subtype of type 'JsObject'. Note that here it refers to the same object as PlainJavaScriptObject whereas claims.runtimeType returned JSObject.
  • Modifying the code outputted by dart2js allows me to use it as a standard JS object.

The last one helped me pin down the issue -- the claims were never converted to a Dart object, so any attempt to use it as such will fail. To fix this I used dartify from the node_interop library:

final Map<String, dynamic> dartClaims = dartify(claims);

That works. I would've submitted a PR with this change, but I'm not quite sure where to put this function call. The UserRecord is a JavaScript binding with only external methods. Does this mean extending UserRecord and changing customClaims to a getter that converts it under the hood? I'm not quite sure what approach would fit in the pattern used in this package.

Levi-Lesches avatar Apr 21 '20 18:04 Levi-Lesches

I came here to open the exact same issue. thanks for the temporary solution

jimmyff avatar Oct 09 '20 16:10 jimmyff