Accessing the current user (AppUser, AppRoles) on the server side

if I need to check the current users roles for a server-side activity (say in the app behavior), what’s the right way to get the current user and their roles since I don’t have access to the authService?

can do it this way:

debug('options.request.appUser.id', options.request.appUser.id);
const appuserId = options.request.appUser.id;
const userProfileResponse = await UserProfile.findById

(appuserId, {}, options);
debug(‘userProfileResponse %j’, userProfileResponse);

If you ever need to confirm that the user has a specific role name, you can use this utility function from the Loopback base library:

const isAdmin = await hasRole('Administrator',options)
1 Like

what’s the dependency needed to allow that server-side hasRole call?
i’m getting that it’s not found…

There is a utility function called hasRole in the Loopback Base Library. Just add hasRole as a dependency.

1 Like

should there be a module name defined as part of the dependency for hasRole?
I have it defined as a dependency (in a mixin) and it’s causing an app error saying:
2025-06-03T15:27:49.648Z MeUsKirk:LastModified mixin repository loaded
stderr Cannot start the application. Error: Cannot find module ‘hasRole’