payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
25.73k stars 1.64k forks source link

req.i18n.language not respecting accept-language header in forgotPassword generateEmailHTML function #8642

Closed sriechersrc closed 1 month ago

sriechersrc commented 1 month ago

Link to reproduction

No response

Environment Info

Binaries: Node: 22.8.0 npm: 10.8.2 Yarn: N/A pnpm: 8.12.1 Relevant Packages: payload: 3.0.0-beta.97 next: 15.0.0-canary.104 @payloadcms/db-mongodb: 3.0.0-beta.97 @payloadcms/db-postgres: 3.0.0-beta.97 @payloadcms/email-nodemailer: 3.0.0-beta.97 @payloadcms/graphql: 3.0.0-beta.97 @payloadcms/live-preview: 3.0.0-beta.97 @payloadcms/live-preview-react: 3.0.0-beta.97 @payloadcms/next/utilities: 3.0.0-beta.97 @payloadcms/plugin-cloud: 3.0.0-beta.97 @payloadcms/plugin-cloud-storage: 3.0.0-beta.97 @payloadcms/plugin-nested-docs: 3.0.0-beta.97 @payloadcms/plugin-search: 3.0.0-beta.97 @payloadcms/plugin-seo: 3.0.0-beta.97 @payloadcms/richtext-lexical: 3.0.0-beta.97 @payloadcms/storage-vercel-blob: 3.0.0-beta.97 @payloadcms/translations: 3.0.0-beta.29 @payloadcms/ui/shared: 3.0.0-beta.97 react: 19.0.0-rc-06d0b89e-20240801 react-dom: 19.0.0-rc-06d0b89e-20240801 Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.2.0: Wed Nov 15 21:54:51 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6030 Available memory (MB): 18432 Available CPU cores: 11

Describe the Bug

When trying to access the language from the req object, the language is always set to english. The docs (https://payloadcms.com/docs/beta/configuration/i18n#node) state that "You can make HTTP requests with the accept-language header and Payload will use that language." However that does not work.

import localeParser from 'accept-language-parser'

const Customers: CollectionConfig = {
  slug: 'customers',
  auth: {
    useAPIKey: false,
    forgotPassword: {
      async generateEmailHTML(args) {
        if (args?.token) {
         // locale should be available under args.req.i18n.language but it is always set to "en"

        // workaround that works and also verifies that the header is actually set
          const acceptLanguage = headers().get('accept-language')
          const locale = (
            acceptLanguage
              ? localeParser.pick(LOCALES as unknown as string[], acceptLanguage) || DEFAULT_LOCALE
              : DEFAULT_LOCALE
          ) as Locale
          return await renderForgotPasswordEmail({ token: args.token }, { locale })
        }
        return ''
      },
    },
  },
}

Reproduction Steps

see above

Adapters and Plugins

No response

JarrodMFlesch commented 1 month ago

I am unable to reproduce this issue. I am going to close it for now. If you can provide a reproduction then we can re-open it. Thanks!

https://github.com/user-attachments/assets/2aef3fc7-7046-4328-b118-acb6048351d8

github-actions[bot] commented 1 month ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.