Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Conditional verification email

default discord avatar
thefrontend2 years ago
5

Is there a way to conditionally send verification emails upon account creation?


I'm currently creating a plugin for Google Authentication and I'd like to not send verification emails if a user signs up with Google. As the default Email / Password auth is still active, verification emails should be send here.



Also, it looks like I can not create a new already verified account. It takes two steps: create an account and then update _verified: true.



As I can see I can only pass 'true' or 'generateEmailHTML' for ´´´verify´´´ option in a collection config.

  • default discord avatar
    notchr2 years ago
    @1041438496285532170

    You cant alter the _verified prop once they auth with Google?

  • default discord avatar
    thefrontend2 years ago

    I can, but not before the email is sent.



     const registered = await payload.create({
              collection: 'users',
              showHiddenFields: true,
              data: {
                email: user.email,
                name: user?.name,
                password: pw,
                oauth: pw,
                loginMethod: 'google',
              },
            })


    Now the email is sent, before I can update the _verified. I can login directly but the email is still sent.



    await payload.update({
                  id: registered.id,
                  collection: 'users',
                  data: {
                      _verified: true,
                  },
              });
  • default discord avatar
    notchr2 years ago
    @1041438496285532170

    Unless there is a better way..



    How about disabling email



    And then manually calling

    payload.sendEmail(message)


    on creation of a user with the type you want

  • discord user avatar
    denolfe
    2 years ago

    We've done something similar in the past by using a

    beforeOperation

    hook that contains the conditional logic for setting

    disableVerificationEmail

    . Let me post a snippet



    This used a previous Payload version, but still should be good.



    import type { CollectionBeforeOperationHook } from 'payload/types';
    
    const skipVerificationEmail: CollectionBeforeOperationHook = ({ args, operation }) => {
      if (operation === 'create' && args.req.query && typeof args.req.query.checkout !== 'undefined') {
        return {
          ...args,
          disableVerificationEmail: true,
        };
      }
    
      return args;
    };


    Just change up the conditional. You

    should

    have access to the user off of req

  • default discord avatar
    thefrontend2 years ago

    Works perfectly, thanks!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.