Adding loginCounts to GET /userinfo response

Hi,
I created the next rule:

function addAttributes(user, context, callback) {
  context.idToken['https://example.com/login_counts'] = context.stats.loginsCount || 0;
  context.idToken.login_counts = context.stats.loginsCount || 0;

  callback(null, user, context);
}

GET userinfo response BEFORE using the above rule was:

{
  "sub": "auth|012345"
  "nickname": "test" ,
  "name": "test@example.com",
  "picture": "..."
  "updated_at": "2022-02-21T11:49:20.539Z",
  "email": "test@example.com",
  "email_verified": false
}

AFTER using the rule:

{
  "sub": "auth|012345"
  "nickname": "test" ,
  "name": "test@example.com",
  "picture": "..."
  "updated_at": "2022-02-21T11:49:20.539Z",
  "email": "test@example.com",
  "email_verified": false,
  "https://example.com/login_counts": 208
}

EXPECTED

{
  ...
  "https://example.com/login_counts": 208
  "login_counts": 208
}

Any ideas???

Thanks.

Hi @roy2,

Welcome to the Auth0 Community!

I understand you are looking to add the users loginCount to the ID token.

After looking carefully at your Rule and response, everything looks correct. Because the loginCount is not a part of the standard claims, you will need to append them as custom claims as you have already done using the Rule.

In this case, the result is to be expected with the custom claim appended to a namespace:

"https://example.com/login_counts" : 208

Our Create Namespaced Custom Claims documentation explains this in further detail.

Thus, to reiterate, you are appending namespaced custom claims to your ID token correctly.

Hoped this helps!

Please let me know if you have any further questions.

Thank you.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.