payload icon indicating copy to clipboard operation
payload copied to clipboard

Resend for /api/users/forgot-password over REST in Vercel

Open peterjunsworth opened this issue 1 year ago • 12 comments

Resend does not receive email request on Vercel. Locally no issues.

Version:

"@payloadcms/email-resend": "3.0.0-beta.25"

payload.config.ts

import { resendAdapter } from '@payloadcms/email-resend'

export default buildConfig({
    email: resendAdapter({
        defaultFromAddress: '****@****.****.com',
        defaultFromName: 'Payload CMS',
        apiKey: '**********',
    }),
    ....

REST Call

await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/users/forgot-password`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      email: '****@****.****.com',
    }),
  });

Users: CollectionConfig

...
  auth: {
    forgotPassword: {
      generateEmailSubject: () => 'Reset your password',
      generateEmailHTML: async ({ req, token, user }: any) => {
        const resetPasswordURL = `${process.env.NEXT_PUBLIC_BASE_URL}/verify?token=${token}`
        return `
          <!doctype html>
          <html>
            <body>
              <h1>Here is my custom email template!</h1>
              <p>Hello, ${user.email}!</p>
              <p>Click below to reset your password.</p>
              <p>
                <a href="${resetPasswordURL}">${resetPasswordURL}</a>
              </p>
            </body>
          </html>
        `
      },
    },
  },

peterjunsworth avatar May 07 '24 16:05 peterjunsworth

Note: Same issue exists without using forgotPassword.generateEmailHTML within User collection > auth

peterjunsworth avatar May 07 '24 18:05 peterjunsworth

Is this just over the REST API? What response are you getting from that fetch if so? It doesn't look like you're sending any creds.

Does the Forgot password? link on the login page send the email to you as expected?

All of the normal functionality appears to work for me. image

denolfe avatar May 08 '24 03:05 denolfe

When you say "It doesn't look like you're sending any creds.", am I incorrect in thinking that the correct way of providing creds is through the payload config for the resendAdapter. Outside of that, forgot password should only require an email no?

This is related to REST. I will follow up with OOTB forgot password.

Thanks for your prompt reply BTW

peterjunsworth avatar May 08 '24 10:05 peterjunsworth

Further testing for me, neither REST nor /admin forgot password link works on Vercel. Can you see an issue with my implementation in the config?

"@payloadcms/email-resend": "3.0.0-beta.25",

import { resendAdapter } from '@payloadcms/email-resend'

export default buildConfig({
    email: resendAdapter({
        defaultFromAddress: '****@****.****.com',
        defaultFromName: 'Payload CMS',
        apiKey: '**********',
    }),
    ....

peterjunsworth avatar May 08 '24 11:05 peterjunsworth

Is this just over the REST API? What response are you getting from that fetch if so? It doesn't look like you're sending any creds.

Does the Forgot password? link on the login page send the email to you as expected?

All of the normal functionality appears to work for me. image

Interesting reset hyperlinked text though.... undefined

JarrodMFlesch avatar May 08 '24 12:05 JarrodMFlesch

Tried with @payloadcms/email-nodemailer just now. Unfortunately same result. Locally works perfect, Vercel, nothing gets through. Tried on Vercel edge and without. Followed the docs for transportOptions here:

https://www.npmjs.com/package/@payloadcms/email-nodemailer

peterjunsworth avatar May 08 '24 12:05 peterjunsworth

@JarrodMFlesch I just simply didn't set the variable from the template provided in the example.

@peterjunsworth

What response are you getting from that fetch if so? It doesn't look like you're sending any creds.

I would expect to see some sort of Payload auth credentials being sent in the REST request in order to be able to invoke this route. Where are you actually calling this function from? Your front-end? A local script? Different server?

Does the Forgot password? link on the login page send the email to you as expected?

Just want to verify that this still works for you. I know this is not the same as your REST request, but it just helps troubleshooting.

denolfe avatar May 08 '24 12:05 denolfe

I am invoking forgotPassword from the NextJS (app) custom login page:

const forgotPassword = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/users/forgot-password`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      email: normailizedEmail,
    }),
  });

 const fp = await forgotPassword.json();
 console.log(fp);

Response: { message: 'Success' }

I am unclear about 'Payload auth credentials' for forgot password endpoint, isn't this anonymous?

Does the Forgot password? link on the login page send the email to you as expected?

Only locally

peterjunsworth avatar May 08 '24 13:05 peterjunsworth

@peterjunsworth Can you provide a minimal reproduction repo? The forgot password link not working for you is most concerning to me, as this works just fine in my testing.

denolfe avatar May 08 '24 13:05 denolfe

@denolfe - just invited you to a fork of the repo. Added a couple of notes to top of readme

Everything fine and works as expected locally. Just vercel deployment no emails for me

peterjunsworth avatar May 08 '24 13:05 peterjunsworth

@denolfe - no worries if you haven't looked at this yet but I did see you forked the repo so thought I would check-in to see if you were able to replicate with that repo?

peterjunsworth avatar May 09 '24 15:05 peterjunsworth

@peterjunsworth The repo was a bit large, would appreciate a smaller reproduction that I can simply load and click something to reproduce.

denolfe avatar May 13 '24 17:05 denolfe

@peterjunsworth Could you simplify your reproduction?

denolfe avatar May 28 '24 16:05 denolfe

@denolfe - ye - I will do but under the gun right now so haven't had the opportunity yet

peterjunsworth avatar May 28 '24 18:05 peterjunsworth

ok @denolfe - finally got round to it. Just added you to the repo (let me know if didn't receive invite). I forked this repo from: https://github.com/payloadcms/payload-3.0-demo

You will see the resend dependencies in the package.json file. Locally I receive email, deployed to Vercel 'nothing', just a success message in network. Same even using the admin forgot password functionality: https://payloadtestingemail.vercel.app/admin/forgot

Very simple setup using Mongo Atlas and @payloadcms/email-resend": "^3.0.0-beta.40" Nothing much else configured apart from what was in the original repo.

Hopefully I am just missing something minor.

Cheers

peterjunsworth avatar Jun 07 '24 16:06 peterjunsworth

Screenshot

No error messages in Vercel or browser No logs at all in resend - double checked env vars

peterjunsworth avatar Jun 07 '24 17:06 peterjunsworth

Will have a fix going out in the next beta for this 👍

denolfe avatar Jun 12 '24 18:06 denolfe

awesome - thanks. glad it wasn't just me being daft

peterjunsworth avatar Jun 12 '24 18:06 peterjunsworth

@peterjunsworth These email calls should now be awaited. Try it out on beta.46 and let me know 👍

https://github.com/payloadcms/payload/releases/tag/v3.0.0-beta.46

denolfe avatar Jun 12 '24 20:06 denolfe

Fantastic - thanks @denolfe . I will definitely give it a try. Thank you for addressing

peterjunsworth avatar Jun 13 '24 13:06 peterjunsworth

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

github-actions[bot] avatar Sep 07 '24 02:09 github-actions[bot]