Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
peterjunsworth opened this issue May 7, 2024 · 21 comments
Closed

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

peterjunsworth opened this issue May 7, 2024 · 21 comments
Assignees

Comments

@peterjunsworth
Copy link

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
Copy link
Author

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

@denolfe denolfe self-assigned this May 7, 2024
@denolfe
Copy link
Member

denolfe commented May 8, 2024

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

@peterjunsworth
Copy link
Author

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
Copy link
Author

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: '**********',
    }),
    ....

@JarrodMFlesch
Copy link
Contributor

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

@peterjunsworth
Copy link
Author

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

@denolfe
Copy link
Member

denolfe commented May 8, 2024

@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.

@peterjunsworth
Copy link
Author

peterjunsworth commented May 8, 2024

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

@denolfe
Copy link
Member

denolfe commented May 8, 2024

@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.

@peterjunsworth
Copy link
Author

peterjunsworth commented May 8, 2024

@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
Copy link
Author

@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?

@denolfe
Copy link
Member

denolfe commented May 13, 2024

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

@denolfe denolfe transferred this issue from payloadcms/payload-3.0-demo May 22, 2024
@github-actions github-actions bot removed the status: cant-reproduce If an issue cannot be reproduced label May 28, 2024
@denolfe
Copy link
Member

denolfe commented May 28, 2024

@peterjunsworth Could you simplify your reproduction?

@peterjunsworth
Copy link
Author

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

@peterjunsworth
Copy link
Author

peterjunsworth commented Jun 7, 2024

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
Copy link
Author

peterjunsworth commented Jun 7, 2024

Screenshot

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

denolfe added a commit that referenced this issue Jun 12, 2024
Await email sending, serverless may end before send

Fixes #6457
@denolfe
Copy link
Member

denolfe commented Jun 12, 2024

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

@peterjunsworth
Copy link
Author

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

@denolfe
Copy link
Member

denolfe commented Jun 12, 2024

@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 denolfe closed this as completed Jun 12, 2024
@peterjunsworth
Copy link
Author

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

Copy link
Contributor

github-actions bot commented Sep 7, 2024

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants