Skip to content

Commit

Permalink
chore: allow password to be mutated by hooks (#7537)
Browse files Browse the repository at this point in the history
Fixes #7531

Allows passwords to be updated in hooks.
  • Loading branch information
JarrodMFlesch authored Aug 9, 2024
1 parent 7f39afa commit 2c2ffe4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/payload/src/collections/operations/updateByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
}

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
Expand Down Expand Up @@ -256,7 +256,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
// /////////////////////////////////////

const dataToUpdate: Record<string, unknown> = { ...result }

const { password } = dataToUpdate
if (shouldSavePassword && typeof password === 'string') {
const { hash, salt } = await generatePasswordSaltHash({ password })
dataToUpdate.salt = salt
Expand Down
9 changes: 9 additions & 0 deletions test/_community/collections/Users/index.ts
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 2 additions & 0 deletions test/_community/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c2ffe4

Please sign in to comment.