Email Verification in payload CMS

If you’d like to require email verification before a user can successfully log in, you can enable it by passing true or an options object to auth.verify. The following options are available:

generateEmailHTML

The function that accepts one argument, containing { req, token, user }, allows for overriding the HTML within emails sent to users indicating how to validate their account. The function should return a string that supports HTML, which can optionally be a full HTML email.

Example:

import { CollectionConfig } from 'payload/types'
export const Customers: CollectionConfig = {
 slug: 'customers',
 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}`
      },
    },
  },
}

generateEmailSubject

Similarly to the above generateEmailHTML, you can customize the email’s subject. The function arguments are the same but you can only return a string – not HTML.

Example:

{
  slug: 'customers',
      auth: {
      verify: {
      generateEmailSubject: ({ req, user }) => {
      return `Hey ${user.email}, reset your password!`;
      }
    }
  }
}

Leave a comment

Your email address will not be published. Required fields are marked *