Unable to get the Username and Roles in Kibana 7.9 through the Legacy and New Platforms

Hey @BitBite,

You're very close! If you pass the security plugin's setup contract through to your route handler, then you can access the current user. Something like the following should work:

interface RouteDeps {
  security: SecurityPluginSetup
}

export function defineRoutes(router: IRouter, { security }: RouteDeps) {
  router.get(
    {
      path: '/api/my_plugin_name/example',
      options: { authRequired: 'required' },
      validate: false,
    },
    async (context, request, response) => {
      const currentUser = security.authc.getCurrentUser(request);

      return response.ok({
        body: {
          time:  new Date().toISOString(),
        },
      });
    }
  );
}
1 Like