What are the allowed data types for firebase custom claims?

Viewed 351

I looked through the firebase documentation and found :

The custom claims object should not contain any OIDC reserved key names or Firebase reserved names. Custom claims payload must not exceed 1000 bytes.

Here custom claims are referred to as "custom claims object" but no where could I find an example of setting a value that is not a primitive data type (int,float,string etc) as custom claim ( both in documentation and other tutorials ). Now I am confused is that object referring to the whole set of custom claims or a single custom claim.

To put it simply is this valid :

admin.auth().setCustomUserClaims(uid, {endDate: new Date()});

Or a general case

admin.auth().setCustomUserClaims(uid, {
    key1: {
        sub_key1: value1,
        sub_key2: value2      
    },
    key2: {
        sub_key1: vlaue1
    }
});

If this is not valid is there any way to set a timestamp as custom claim so that I can use it in security rules to allow access based on it.

1 Answers

Custom claims accepts anything that is valid JSON. That means you can only use JSON types, which are string, number, boolean, array, object. Date is not valid. Instead, you should store a numeric representation of a date. It's common to use millisconds since epoch, such as you get from Date.now().

{ endDate: Date.now() }
Related