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: