How can I link to a user profile by their user ID?

AI-generated summary

A user, wilson29thid, wants to link to a Discourse user’s profile using their discourse_user_id without storing the username. wilson29thid is hesitant to make API requests to fetch usernames.

pfaffman suggests using an external ID in the user record and the /by-external/:external_id route or disabling username changes. However, wilson29thid is unsure how to update the external ID field without enabling SSO.

pfaffman recommends using email addresses as a common identifier across systems. Alternatively, pfaffman suggests using the active.json endpoint with email filtering to retrieve user data.

Ultimately, wilson29thid creates a plugin, discourse-user-by-id, to enable linking to user profiles by ID, redirecting from /user-by-id/123/summary to /u/janedoe/summary.

I have a members table in my application’s database, where I store application-specific information about a subset of members. One of the columns is discourse_user_id. I’d like to render a link to the member’s discourse profile. Is it possible to do that with just their user id?

I am not storing discourse_user_name because the user can change that, and then I’d have to worry about keeping them in sync.

The only way I can think of is to make an API request to /admin/users/<user-id>.json to get their username, but that would be a large number of requests to do on an index page, where I’m listing members with links to their profile.

The only other thing that comes to mind is to build a plugin to add a route, though that seems like a lot of effort for something that I’d expect I’m not the first person to ask about, so I wanted to check whether there’s another solution already?

One idea is to put an external ID in the user record and then you could use /by-external/:external_id. The other is to use a webhook to notify your external database if they change their username. Another is to just disable changing usernames. From a quick look at discourse/config/routes.db I don’t see any route that uses the user_id.

Thanks @pfaffman, that’s very helpful. I think using an external id would be ideal, as it also solves another problem—linking in the reverse direction.

Poking around the API docs and a REST client, though, I don’t see how to update that field. Can I use that without enabling SSO? (And, even if I enabled SSO, would I be able to update it programmatically?)

Sorry, I don’t know offhand. I’d have to poke in the source.

There is no reason to update the external key, keys aren’t supposed to change.

The other thing you might do is have both systems count on having the email address be the same.

My guess is that sso is what you’ll want to do

You can try this:

https://SERVER/community/admin/users/list/active.json?filter=email%40hostname.com&show_emails=true

You’ll need to pass the API key in headers as documented elsehwere.

I have created a plugin to enable this:

https://github.com/29th/discourse-user-by-id

/user-by-id/123/summary redirects to /u/janedoe/summary etc.

I’m still surprised I had to do this, but at least it works!