Add user/me endpoint to Users Permissions Plugin
G
Gabe
There is no updateMe (user/me) endpoint in the Users Permissions Plugin. This would allow users to update their own data after registration (aka: email, login, address, etc..)
Following is the code I've used. Create a file called strapi-server.js in the folder: extensions>users-permissions.
module.exports = (plugin) => {
plugin.controllers.user.updateMe = async (ctx) => {
if (!ctx.state.user || !ctx.state.user.id) {
return ctx.response.status = 401;
}
// Extract only the fields that need to be updated from the request body
const updatedUserData = {
// ...ctx.request.body,
firstname: (ctx.state.user.firstname || ctx.request.body.firstname),
lastname: ctx.state.user.lastname || ctx.request.body.lastname,
username: ctx.state.user.username || ctx.request.body.username,
email: ctx.state.user.email || ctx.request.body.email,
// change the above code to reflect the fields you want the user to be able to update.
};
try {
// Update the user data in the database
await strapi.query('plugin::users-permissions.user').update({
where: { id: ctx.state.user.id },
data: updatedUserData,
});
ctx.response.status = 200;
} catch (error) {
console.error('Error updating user data:', error);
ctx.response.status = 500;
}
};
plugin.routes['content-api'].routes.push(
{
method: "PUT",
path: "/user/me",
handler: "user.updateMe",
config: {
prefix: "",
policies: []
}
});
return plugin;
}