Get current logged in admin user

How do you get the currently logged in admin user, not end user, details on the server when building a plugin? I need to get the role of the admin user currently logged in to be able to set permissions within the plugin I’m making

ctx.state.user gives you the current auth user no matter the type

You can also use
ctx.state.auth.strategy to get more specific information like the name what is the verification strategy used

1 Like

This doesn’t work on the version of Strapi I’m using, v4.6.1.

ctx.state gives me the following, there is no user property populated:

{
_passport: {
instance: Authenticator {
_key: ‘passport’,
_strategies: [Object],
_serializers: [],
_deserializers: [],
_infoTransformers: [],
_framework: [Object],
_sm: [SessionManager],
Authenticator: [Function: Authenticator],
Passport: [Function: Authenticator],
Strategy: [Function],
strategies: [Object],
KoaPassport: [class KoaPassport extends Authenticator],
_userProperty: ‘user’
}
},
route: {
method: ‘POST’,
path: ‘/exporter/excel’,
handler: ‘exporter.excel’,
config: { policies: [Array], auth: false },
info: { pluginName: ‘loq-manager’, type: ‘admin’ }
}
}

That is correct since in your config you have auth: false what means ignore all authentication and let me trough

Of course, I’ve been using auth: false so that’s why I’m not getting any user state. I tried a different approach now by using the session data created client side and I’m using that data to get the currently logged in user role. It works great now so I will stick to that, even though I’m sure there is a better way.