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

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.

1 Like

I have created a plugin to enable this:

/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!

1 Like