Is there a recommended way to get the current username from a post?

I was wondering if it’s possible to create a post that references the username of whoever is logged in? Sort of like the /my route but for the frontend.

My use case would be to customize a URL pointing outside of Discourse so when a user clicks it, it will have the query ?username=MY_USERNAME appended to it. The same approach could be used to embed an iframe with that URL, for example.

If this is not possible out of the box, I assume the other alternative would be to write a custom plugin.

Yes. The post record has the username in it (see https://meta.discourse.org/t/is-there-a-recommended-way-to-get-the-current-username-from-a-post/178288.json).

You can create a theme component that would add your link as a plugin outlet. See Developer’s guide to Discourse Themes for some hints.

2 Likes

Thanks, Jay. I definitely need to investigate that option, but I think I didn’t explain myself properly. Here’s the reworded version:

How to create a post that contains a URL that updates based on who clicks on it?

Is there a way I can query the currently logged in user (and its username)? Sort of like the /my route but for the frontend.

Oh, well, the particulars are different but the answer is the same. There should be examples in the developer’s guide, so you can see how to access current_user.

1 Like