How to get external ID

When I consume API directly, I can access user’s external authentication ID without any problem:

GET /admin/users/{id}.json

However, when I try using the Javascript API, I cannot get the same data for the current user. In fact, the currentUser doesn’t return any data for external IDs:

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer("1.5.0", api => {
  const currentUser = api.getCurrentUser();
  if (currentUser) {
    const userOAuthID = currentUser?.external_ids?.oauth2_basic;
    console.log("userOAuthID:", userOAuthID); // null
  }
});

How could I access currentUser’s external ID via javascript?

The Context

I’m trying to push current user’s external ID to window.dataLayer.

It looks like you are trying to get the user’s user_associated_account.provider_uid. From looking at the code, there is a hidden site setting that you need to enable to cause the associated_account_ids to be returned from the CurrentUserSerializer:

The include_associated_account_ids setting is hidden: discourse/config/site_settings.yml at main · discourse/discourse · GitHub. To enable it, enter your site’s Rails console and run:

SiteSetting.include_associated_account_ids=true

3 Likes

Thanks @simon.
Though, it seems like it cannot be enabled on a hosted instance by myself. I find no option at /admin/site_settings

Likely solved! : )

If your site is hosted by Discourse, contact their support team. They should be able to set it for you.

4 Likes

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