Replies: 1 comment 7 replies
-
The documentation linked is specifically for the SDK, so it is correct that it's undefined and not within UsersService 👍 The accountability object itself should already contain the user ID in directus/packages/shared/src/types/accountability.ts Lines 8 to 19 in 5e81373 However depending on extensions and the logic involved, and as the type above states, do note that user and/or role could be null (eg. invoked by public or system, hence there's no "user"), so do make sure to add additional guards before accessing You can then use the user ID in export default (router, { services, exceptions }) => {
const { UsersService } = services;
const { ForbiddenException, ServiceUnavailableException } = exceptions;
router.get('/', (req, res, next) => {
// this ensures public or unauthenticated calls will get forbidden error
if (!req.accountability?.user) return next(new ForbiddenException());
const usersService = new UsersService({ schema: req.schema, accountability: req.accountability });
usersService
.readOne(req.accountability.user)
.then((result) => {
res.json(result); // returns json containing the user detail
})
.catch((error) => {
return next(new ServiceUnavailableException(error.message));
});
});
} |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Maybe I am stupid or I found not the right piece of code.. I want to know in my extension of the API endpoint, which user I am authentificated with the token with.
In the documentation is a function mentioned
await directus.users.me.read();
By reading I would imagine that I could do something like this:
Also when using the ItemsService, I get an undefined error. I also could not found the functions in code. How is the recommended way to do that?
Beta Was this translation helpful? Give feedback.
All reactions