payload icon indicating copy to clipboard operation
payload copied to clipboard

I can't get forgot password to work, it throws 500 error

Open artmelkon opened this issue 2 years ago • 4 comments

Link to reproduction

No response

Describe the Bug

when I try to fetch the token I get 500 error. I have posted this issue on Discord and Dan suggested that it could be a bug. This is my front end code: `import { NextApiRequest, NextApiResponse } from "next"; // import { getServerSession } from "next-auth/next";

// import { authOptions } from "./[...nextauth]";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {

if (req.method === 'POST') { try { console.log('email ', req.body.email) const result = await fetch(${process.env.CMS_URI}/api/users/forgot-password, { method: 'POST', credentials: 'include', headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: req.body.email }) });

  const data = await result.json();
  console.log(data)
} catch (err) {
  console.log(err)
}

} }`

This is the returned data { errors: [ { message: 'Something went wrong.' } ] }

And this error was thrown from the server ERROR (payload): TypeError: Cannot read properties of undefined (reading 'email') at Object.generateEmailHTML (/Users/arthurmelkonyan/webdev/ethnicast-cms/payloadcms/src/collections/Users/index.ts:24:32) at forgotPassword (/Users/arthurmelkonyan/webdev/ethnicast-cms/payloadcms/node_modules/payload/src/auth/operations/forgotPassword.ts:112:59) at processTicksAndRejections (node:internal/process/task_queues:95:5) at forgotPasswordHandler (/Users/arthurmelkonyan/webdev/ethnicast-cms/payloadcms/node_modules/payload/src/auth/requestHandlers/forgotPassword.ts:15:5)

To Reproduce

I have created the form on the front end to capture the email and pass to API Route handler

Payload Version

v2

Adapters and Plugins

No response

artmelkon avatar Jan 24 '24 06:01 artmelkon

Hey @artmelkon, Have you configured the mail service like smtp in the payload init?

await payload.init({
    secret: process.env.PAYLOAD_SECRET,
    express: app,
    email: {
      transportOptions: transportOptions,
      fromName: process.env.MAIL_FROM_NAME,
      fromAddress: process.env.MAIL_FROM_ADDRESS,
    },
  });

faizur11786 avatar Jan 24 '24 14:01 faizur11786

Hi faizur11786. Yes, I have done it and I don't think it has to do with that. That configuration didn't work for me, throwing error saying that email wasn't configured right, basically it was a mail server issue. So, I'm using "nodemailer-sendgrid-transport module ", tested and it works perfectly fine. I don't think that it's related to SMPT configuration as the error thrown for the server tells that " undefined (reading 'email')"

artmelkon avatar Jan 24 '24 15:01 artmelkon

I fixed it and it works.

artmelkon avatar Jan 26 '24 01:01 artmelkon

Hey @artmelkon - are you still experiencing this issue or has it been resolved?

PatrikKozak avatar Apr 16 '24 14:04 PatrikKozak

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]