User logins_count per organization

Hi,
the Users endpoint of the management-api returns the logins_count and last_login of a user. I would like to know if this data is also available per organization a user is in. So for a user I would like the logins_count on organization a and logins_count on organization b.

Hi @Montijn,

Welcome back to the Auth0 Community!

While I totally understand the need of your use case, it is already documented that there no out-of-the-box solution to retrieve the logins count per application such as by using a Management API endpoint and there isn’t one either for logins_count in case of organizations either.

To achieve the desired tracking, the recommended approach is to implement a custom solution using a Post Login Action.

Here is a sample action code that you could try, which will insert into the user’s app metadata the logins count for each organization and the last login timestamp.

exports.onExecutePostLogin = async (event, api) => {
  // Only track if the login is contextually within an organization
  if (event.organization) {
    const orgId = event.organization.id;
    const stats = event.user.app_metadata.org_stats || {};

    // Initialize the specific org object if it doesn't exist
    if (!stats[orgId]) {
      stats[orgId] = { logins_count: 0, last_login: null };
    }

    // Increment count and update timestamp
    stats[orgId].logins_count += 1;
    stats[orgId].last_login = new Date().toISOString();

    // Persist to app_metadata
    api.user.setAppMetadata("org_stats", stats);
  }
};

I hope this helps and if you have further questions please let me know!
Best regards,
Remus