System Information
-
Strapi Version: 4.3.8
-
Operating System: macOS Monterey
-
Database: MYSQL
-
Node Version: 14.19.0
-
NPM Version: 6.14.16
-
Yarn Version: NA
Hello everyone,
Trying to get the logged in user details from Admin like id, username and email to populate the created by and updated by collection fields as I am creating the collection using API these fields are coming blank.
I am trying to populate the created by and updated by details automatically how its happens when we create the content manually.
Solutions tried :
- Created file in /extensions/users-permissions/server/services/user.js and added code
'use strict';
/**
* User.js service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
*/
module.exports = ({ strapi }) => ({
/**
* Promise to fetch authenticated user.
* @return {Promise}
*/
fetchAuthenticatedUser(id) {
return strapi
.query('plugin::users-permissions.user')
.findOne({ where: { id }, populate: ['role'] });
},
});
and strapi-server.js in /extensions/users-permissions/strapi-server.js
module.exports = plugin => {
const sanitizeOutput = (user) => {
const {
password, resetPasswordToken, confirmationToken, ...sanitizedUser
} = user; // be careful, you need to omit other private attributes yourself
return sanitizedUser;
};
plugin.controllers.user.me = async (ctx) => {
if (!ctx.state.user) {
return ctx.unauthorized();
}
const user = await strapi.entityService.findOne(
'plugin::users-permissions.user',
ctx.state.user.id,
{ populate: ['role'] }
);
ctx.body = sanitizeOutput(user);
};
plugin.controllers.user.find = async (ctx) => {
const users = await strapi.entityService.findMany(
'plugin::users-permissions.user',
{ ...ctx.params, populate: ['role'] }
);
ctx.body = users.map(user => sanitizeOutput(user));
};
return plugin;
};
Reference : strapi/user.js at main · strapi/strapi · GitHub
But no luck with this.
I am trying to access this API http://localhost:1337/admin/users/me but it gives Unauthorized user as it requires JWT token.
Can someone please help me with this solution.
Thanks
@Eventyret any suggestions on this OR anyone who can help me with this ?
Please stop tag people that has nothing to do with the conversation. If people know the answer people will give an answer. @Pushpanjali 2nd time I have been tagged in your conversations now.
@Eventyret the intension here is to get an answer to the issue that I am facing in Strapi on which i am not getting any solutions nor in document as well as through forums which gives me a bad review about the CMS that I am using which doesn’t have much solutions or articles about it. Also I have tagged you in this conversation with a good intension to just get a suggestion if u would have any answers to it as its urgent for me. If you don’t have any answers to it you can just ignore it or tag any relevant person who can help me in this. As simple as that.
There is no reason to get offended if someone has tagged you in an issue the intension is to get some answers or solution to it.
I’m not offended, but I don’t see the point of tagging people to get help,.
If people know something, people will mostly answer.
I understand your issue might be urgent for you, what about the other 15 people that have posted too? 
So my suggestion is, to stop just tagging people unless you know they can help, and or already have helped you in something similar.
To answer your question, mainly for this topic.
I don’t know hence why I never answered this topic in the first place when it was originally created.

I would guess you can get the user state in the controller as it contains the user there.
@Pushpanjali This will be your last warning about tagging people. If you require an answer to your question within a certain SLA I would suggest you look at our enterprise plans that include response SLAs. This is a community platform where people like @Eventyret donate their time to help community members. No one is entitled to an answer.
Do not ping people that are not already part of your conversation. There are hundreds of thousands of Strapi users, if all them do what you are doing we would be getting millions of pings.
This question has been answered many times over, a simple search of either the forum, our Discord, or even on google would yield the result.
2 Likes
@DMehaffy FYI this was the first time I had tagged any person. I searched the entire forum and even googled it but didn’t get the answer if I would have got one then wouldn’t have posted the question here. Strapi don’t have much information in docs plus the forums are also not very helpful plus people here are rude.