How to reliably get the current (logged-in) user into a plugin context?

Re-asking an unanswered question from community: asked by @Siegfried Nolte:
https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-get-the-current-logged-in-user-into-a-plugin-context/qaq-p/556239

I need the username of the plugin “user” to use TrustedRequest#addTrustedTokenAuthentication.

To get this user during runtime, however, this suggested idiom does not reliably work:

   this.authContext = ComponentAccessor.getJiraAuthenticationContext();
   this.currentUser = this.authContext.getLoggedInUser();

The call to getLoggedInUser returns null at random, seemingly independent of the calling thread (ie. on an event listener thread or on a SchedulerService job thread).

What is the best way to reliably get the name of the “logged in user” in the context of the plugin thread?

1 Like

I’ve run into this issue as well, calls to getLoggedInUser() are not reliable as they will periodically return null.

hi , i had the same issue , any solution please?

This is how I am using it and it works without a problem for me:

Constructor of my SomeServiceClass:

    @Inject
    public someServiceImpl(@ComponentImport JiraAuthenticationContext jiraAuthenticationContext) {
        this.jiraAuthenticationContext = jiraAuthenticationContext;
    }

The method in question:

    @Override
    public ApplicationUser getCurrentLoggedInUser() {
        return jiraAuthenticationContext.getLoggedInUser();
    }

I on purpose gave a very specific use case. It of course does not have to be like this. I thought maybe it is more helpful this way.