Impossible to update REST API responses via hooks #8906
-
Link to reproductionNo response Describe the BugHooks manipulate and return a hard object, impossible to manipulate and runs in strange behaviour. To ReproduceI do have a fresh install of Payload CMS 2.30, I have a couple of collections, among them {
"exp": 1729882853,
"message": "Auth Passed",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiY29sbGVjdGlvbiI6InVzZXJzIiwiZW1haWwiOiJlcmlrYS5tdXN0ZXJtYW5uQHN0YXJidWNrcy5kZSIsImlhdCI6MTcyOTg4MTk1MywiZXhwIjoxNzI5ODgyODUzfQ.SmM1wrsjhSWQSvlZbQfctmHzzgx55ESHiuzwJvehr6c",
"user": {
"id": 2,
"firstName": "Erika",
"lastName": "Mustermann",
"updatedAt": "2024-10-22T16:32:22.884Z",
"createdAt": "2024-07-26T19:54:11.004Z",
"email": "erika.mustermann@starbucks.de",
"loginAttempts": 0
}
} At this point, there is no way to configure whether the response of the afterLogin: [
async ({ user }) => {
console.log(user.organization); // will log the correct relationship
return user; // this will return a "short" or "simplified" version of the user, just like the json response posted above;
// if I do this:
user.whateverName = 'hello world';
return user; // will return the "short" or "simplified" object, appending the "whateverName" to the user object, but I can't figrue HOW to make Payload to show the `organization` within the `user` object.
// If I do:
user.organization = 'hello world'; // this won't work and it won't show up under the `user` object, because I do have the impression that Payload is filtering out the property names that matches with associated fields to the collection.
},
], Since I can't get to update the response of afterRead: [
async ({ doc }) => {
console.log('doc', doc);
},
], the console outputs this: // first time
doc {
id: 2,
firstName: 'Erika',
lastName: 'Mustermann',
roles: [ 'user' ],
organization: {
organization: {
id: 1,
name: 'Starbucks Deutschland GmbH',
logo: 'www.google.com',
updatedAt: '2024-10-02T19:15:21.088Z',
createdAt: '2024-07-26T19:53:31.064Z'
},
roles: [ 'admin' ]
},
updatedAt: '2024-10-22T16:32:22.884Z',
createdAt: '2024-07-26T19:54:11.004Z',
email: 'erika.mustermann@starbucks.de',
password: undefined,
loginAttempts: 0
}
// second time
doc {
id: 2,
firstName: 'Erika',
lastName: 'Mustermann',
updatedAt: '2024-10-22T16:32:22.884Z',
createdAt: '2024-07-26T19:54:11.004Z',
email: 'erika.mustermann@starbucks.de',
password: undefined,
loginAttempts: 0
} it runs TWICE, the first time with the full doc/collection in depth, and the second time with the "short" or "simplified" version, so then AGAIN, I can not update the response to the REST API as I want. Can a good soul explain to me why is that? Is this a bug? (it seems so to me). Is there any way I can fix this without much overhead? Any help would be very much appreciated! <3 Thank you so much! PS: To me, it doesn't make sense the criteria used to set up the API responses for the {
"message": "Token refresh successful",
"exp": 1729884498,
"refreshedToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiY29sbGVjdGlvbiI6InVzZXJzIiwiZW1haWwiOiJlcmlrYS5tdXN0ZXJtYW5uQHN0YXJidWNrcy5kZSIsImlhdCI6MTcyOTg4MzU5OCwiZXhwIjoxNzI5ODg0NDk4fQ.9zIN4TNhNxzZsmbp67sETTjcf1PON3cUyREXaJV8O6Q",
"strategy": "local-jwt",
"user": {
"id": 2,
"firstName": "Erika",
"lastName": "Mustermann",
"roles": [
"user"
],
"organization": {
"organization": {
"id": 1,
"name": "Starbucks Deutschland GmbH",
"logo": "www.google.com",
"updatedAt": "2024-10-02T19:15:21.088Z",
"createdAt": "2024-07-26T19:53:31.064Z"
},
"roles": [
"admin"
]
},
"updatedAt": "2024-10-22T16:32:22.884Z",
"createdAt": "2024-07-26T19:54:11.004Z",
"email": "erika.mustermann@starbucks.de",
"loginAttempts": 0
}
} The Thx! Payload Version2.30.0 Adapters and Pluginsdb-postgres, bundler-webpack |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @juanpablob I think there are three or four questions here and I am going to try to respond to one at a time. Depth in generalYou seem to be asking about the
Here, populations haven't taken place yet so you get the "simplified" version of your user. Populations take place in field
This operation returns a user with the population depth that you specify on
This is because the I can't get the response to return a non-populated (simplified) version when I do tests against the This all being said I am happy to continue to chime in here! What are you trying to do? Field hooksIf you want to consistently add data to or modify data from the returned |
Beta Was this translation helpful? Give feedback.
@jmikrut Hello :) I wonder if you have any idea about how to achieve that simple thing of extending the API response of
/users/login
or the fact that addingauth.depth
is not working at all? Thank you very much! :)