Send Verification Email error, where is the problem?

265 Views Asked by At

I'm new to PayloadCMS and I'm trying to build a new webapp. I arrived at the step where I try to implement email verification, although I'm encountering a strange error I cannot understand... That's why I wanted to ask for your help! I'm using resend and the custom NodeMailer transport (https://payloadcms.com/docs/email/overview#use-a-custom-nodemailer-transport). Here is the following setup for email verification I have made :

I observe the following behaviour when the user is created (should I precise user is indeed created):

TypeError: req.get is not a function
    at sendVerificationEmail (C:\git\myapp\node_modules\payload\src\auth\sendVerificationEmail.ts:35:36)
    at create (C:\git\myapp\node_modules\payload\src\collections\operations\create.ts:269:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
 ⨯ unhandledRejection: TypeError: req.get is not a function
    at sendVerificationEmail (C:\git\myapp\node_modules\payload\src\auth\sendVerificationEmail.ts:35:36)
    at create (C:\git\myapp\node_modules\payload\src\collections\operations\create.ts:269:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
 ⨯ unhandledRejection: TypeError: req.get is not a function
    at sendVerificationEmail (C:\git\myapp\node_modules\payload\src\auth\sendVerificationEmail.ts:35:36)
    at create (C:\git\myapp\node_modules\payload\src\collections\operations\create.ts:269:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected behaviour: Email should be sent

Setup :

Transporter :

const transporter = nodemailer.createTransport({
  host: 'smtp.resend.com',
  secure: true,
  port: 465,
  auth: {
    user: 'resend',
    pass: process.env.RESEND_API_KEY,
  },
})

Payload init:

cached.promise = payload.init({
      email: {
        transport: transporter,
        fromAddress: '[email protected]',
        fromName: 'MyApp',
      },
      secret: process.env.PAYLOAD_SECRET,
      local: initOptions?.express ? false : true,
      ...(initOptions || {}),
    })

User model:

const Users: CollectionConfig = {
    slug: "users",
    auth: {
        verify:{
            generateEmailHTML: ({req, token, user}) => {
                // Use the token provided to allow your user to verify their account
                const url = `https://yourfrontend.com/verify?token=${token}`
                return `Hey ${user.email}, verify your email by clicking here: ${url}`
            }
        }
    },
    access: {
        read: () => true,
        create: () => true,
    },
    fields:[
        {
            name: "role",
            defaultValue: "user",
            required: true,

            type: "select",
            options: [
                {label: "Admin", value: "admin"},
                {label: "User", value: "user"}
            ]
        },
        {
            name: "username",
            required: true,
            type: "text"
        }
    ]
}
4

There are 4 best solutions below

1
On BEST ANSWER

in src/payload.config.ts, you have mis-typed env variable

export default buildConfig({
    // not PUBLICK
    serverURL: process.env.NEXT_PUBLICK_SERVER_URL || '',
    ....
})

in every else you are correctly using process.env.NEXT_PUBLIC_SERVER_URL. this bug might be causing the issue

1
On

Sounds like it could be a bug. I see the line that calls req.get, it attempts to generate the serverURL if none is provided.

You could set serverURL on your payload config to get around this for now. Opening a bug would be the best way to ensure it gets fixed though!

1
On

What I found from the code you have written and in the documentation is that you missed an await keyword before creating a nodemailer transport. await plays an important role here.

enter image description here

here is your code

enter image description here

0
On

Screenshot of JavaScript copy code of send verification file and paste it to chat gpt with the problem i am providing you the image it will work great