From 2c2ffe406fbde01ac946119f805956b3b14bd13a Mon Sep 17 00:00:00 2001 From: Jarrod Flesch <30633324+JarrodMFlesch@users.noreply.github.com> Date: Fri, 9 Aug 2024 09:27:09 -0400 Subject: [PATCH] chore: allow password to be mutated by hooks (#7537) Fixes https://github.com/payloadcms/payload/issues/7531 Allows passwords to be updated in hooks. --- .../payload/src/collections/operations/updateByID.ts | 6 +++--- test/_community/collections/Users/index.ts | 9 +++++++++ test/_community/config.ts | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 test/_community/collections/Users/index.ts diff --git a/packages/payload/src/collections/operations/updateByID.ts b/packages/payload/src/collections/operations/updateByID.ts index f9ef418b54..9502098bca 100644 --- a/packages/payload/src/collections/operations/updateByID.ts +++ b/packages/payload/src/collections/operations/updateByID.ts @@ -91,9 +91,9 @@ async function updateByID( } let { data } = args - const { password } = data + const dataHasPassword = 'password' in data && data.password const shouldSaveDraft = Boolean(draftArg && collectionConfig.versions.drafts) - const shouldSavePassword = Boolean(password && collectionConfig.auth && !shouldSaveDraft) + const shouldSavePassword = Boolean(dataHasPassword && collectionConfig.auth && !shouldSaveDraft) // ///////////////////////////////////// // Access @@ -256,7 +256,7 @@ async function updateByID( // ///////////////////////////////////// const dataToUpdate: Record = { ...result } - + const { password } = dataToUpdate if (shouldSavePassword && typeof password === 'string') { const { hash, salt } = await generatePasswordSaltHash({ password }) dataToUpdate.salt = salt diff --git a/test/_community/collections/Users/index.ts b/test/_community/collections/Users/index.ts new file mode 100644 index 0000000000..daa0ab7a35 --- /dev/null +++ b/test/_community/collections/Users/index.ts @@ -0,0 +1,9 @@ +import type { CollectionConfig } from '../../../../packages/payload/src/collections/config/types' + +export const usersSlug = 'users' + +export const UsersCollection: CollectionConfig = { + fields: [], + auth: true, + slug: usersSlug, +} diff --git a/test/_community/config.ts b/test/_community/config.ts index aef47551ca..6ee10615e8 100644 --- a/test/_community/config.ts +++ b/test/_community/config.ts @@ -2,11 +2,13 @@ import { buildConfigWithDefaults } from '../buildConfigWithDefaults' import { devUser } from '../credentials' import { MediaCollection } from './collections/Media' import { PostsCollection, postsSlug } from './collections/Posts' +import { UsersCollection } from './collections/Users' import { MenuGlobal } from './globals/Menu' export default buildConfigWithDefaults({ // ...extend config here collections: [ + UsersCollection, PostsCollection, MediaCollection, // ...add more collections here