Skip to content

Login got struck if users collection have any hook in <= 3.71.0 version #15568

@rjay-hb

Description

@rjay-hb

Describe the Bug

Login is getting struck if we have beforeLogin hook inside the Users collection after upgrading the Payload version to 3.71.0. In v3.70.0, it's working fine. Here is the example users collection:

import type { CollectionConfig } from 'payload'

export const Users: CollectionConfig = {
  slug: 'users',
  admin: {
    useAsTitle: 'email',
  },
  auth: true,
  fields: [
    {
      name: 'lastLogin',
      type: 'text',
      admin: {
        readOnly: true,
        position: 'sidebar',
      },
    },
    {
      name: 'lastLogout',
      type: 'text',
      admin: {
        readOnly: true,
        position: 'sidebar',
      },
    },
  ],
  hooks: {
    beforeLogin: [
      async ({ req, user }) => {
        try {
          const payload = req.payload
          await payload.update({
            collection: 'users',
            id: user.id,
            data: {
              lastLogin: new Date().toISOString(),
            },
          })
        } catch (err) {
          console.error('Error updating lastLogin:', err)
        }
        return user
      },
    ],
    afterLogout: [
      async ({ req }) => {
        const user = req.user

        if (user && typeof user.id === 'string') {
          try {
            await req.payload.update({
              collection: 'users',
              id: user.id,
              data: {
                lastLogout: new Date().toISOString(),
              },
              req,
            })
          } catch (err) {
            console.error('Error updating lastLogout:', err)
          }
        }
      },
    ],
  },
}

Link to the code that reproduces this issue

https://github.com/rjay-hb/payload-v3.71

Reproduction Steps

  1. Create a blank payload website.
  2. Create a hook inside the Users collection as shared above.
  3. Run yarn dev
  4. Try to login, and hit login button after entering the detail.

Which area(s) are affected?

area: core

Environment Info

Binaries:
  Node: 22.21.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.71.0
  next: 15.4.11
  @payloadcms/db-postgres: 3.71.0
  @payloadcms/drizzle: 3.71.0
  @payloadcms/graphql: 3.71.0
  @payloadcms/next/utilities: 3.71.0
  @payloadcms/richtext-lexical: 3.71.0
  @payloadcms/translations: 3.71.0
  @payloadcms/ui/shared: 3.71.0
  react: 19.2.1
  react-dom: 19.2.1
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Enterprise
  Available memory (MB): 32717
  Available CPU cores: 8

Metadata

Metadata

Assignees

Labels

area: coreCore Payload functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions