[SOLVED] Problem with email verification using cloud functions

Hello,

Iā€™m trying to send email verifications using cloud functions when a user wants to register or log in and has not verified his email yet. No email verification is being sent(not in the spam folder either) and Iā€™m also not getting any errors in the console.

Moralis.Cloud.define("sendVerification", async (request) => {
  await Moralis.User.requestEmailVerification(request.user.get("email"))
    .then(() => {
      console.log("Successfully sent verification email");
    })
    .catch((error) => {
      alert("Error: " + error.code + " " + error.message);
    });
});

Iā€™m calling the cloud function like this: await Moralis.Cloud.run("sendVerification", {});

What should I do to make it work? is there anything that Iā€™m doing wrong?
Thanks in advance!

Hey @Markus-55

Moralis.User.requestEmailVerification is available only on frontend

Take a look at Moralis is not sending email verification to users

Also it should send email verifications automatically if you set correctly your sendgrid account

Hey @Yomoo,

I have tried to put Moralis.User.requestEmailVerification in the frontend but Iā€™m still not getting any emails and I have verified my email ā€œfromā€ in SendGrid, also when I send emails with Moralis.Cloud.sendEmail({ to:... with cloud functions I correctly get an email.

Is there anything more I need set up in SendGrid to correctly get verification emails? Maybe I have missed something?

It should send verification automatically after you added email.
Could you confirm that it doesnā€™t work?
If it doesnā€™t - share your server subdomain and we will check :raised_hands:

1 Like

I think that you need two templates from sendgrid in your Moralis server config, for example: Sendgrid Verification Email Template ID

2 Likes

Ok I see :+1:
how can I confirm that it doesnā€™t work?

I tried to do that before, I put the id of the template there and I successfully got an email, but with no verification link. I think I need to add the link, but I donā€™t know how I can get it and where I need to add it.

Your template should have two curly brackets with variable link in it -
Hi {{name}} {{link}}

The values will get prepopulated accordingly.

1 Like

Hi Malik,

I have tried that and I only get the text I input i.e: "Hi ", I know that I need to define the name to get text there, but I canā€™t define the link. I cannot find any variables in the template, I tried to put the variables in the Design Editor ā€œcodeā€ and ā€œtextā€ and Code Editor.

Maybe there is something Iā€™m doing wrong?

How does your template look like now?

It looks like this

In the email I only get ā€œVerify your emailā€

Maybe Iā€™m doing it wrong, but I donā€™t understand how I can make the verification link work

I just checked my templates and I have this: Hello {{{ link }}} {{{ email }}}

1 Like

Ok I see, I changed it like yours: Verify your email {{{ link }}}
and in my cloud function it looks like this:

Moralis.Cloud.define("sendVerification", async (request) => {
  Moralis.Cloud.sendEmail({
    to: request.user.get("email"),
    templateId: "d-0c47c30df8d945c0bf6ae1db793d8702",
    dynamic_template_data: {
      link: Moralis.User.requestEmailVerification(request.user.get("email"))
    }
  });
});

I tried to set the link variable to requestEmailVerification() and it worked!
but I get 2 emails, one with the link and the other with map[]

If the email is already verified I only get an email with map[]
If I didnā€™t set the variable link to anything I would get an email with ā€œVerify your emailā€ like before

Capture

It also gets set to true in the dashboard

Capture

Any idea why I get two emails?

I think that it is enough to use Moralis.User.requestEmailVerification(request.user.get("email")) in order to send an email.

I changed it and now I only get one email! I defined the userEmail in the frontend and passed it to the cloud function call.

const currentUser = Moralis.User.current();
const userEmail = currentUser.attribute.email;
await Moralis.Cloud.run("sendVerification", {userEmail});

and in the cloud function I added userEmail and requestEmailVerification(userEmail)

Moralis.Cloud.define("sendVerification", async (request) => {
  Moralis.Cloud.sendEmail({
    to: request.user.get("email"),
    templateId: "d-0c47c30df8d945c0bf6ae1db793d8702",
    dynamic_template_data: {
      userEmail: request.user.get("email"),
      link: Moralis.User.requestEmailVerification(userEmail)
    }
  });
});

Thank you very much for the help guys! I really appreciate it :grin:

Oh ok, so you mean that I donā€™t need to set the variable link?

I think that you only have to call Moralis.User.requestEmailVerification(userEmail) from front end.

Ok thanks, now I added it to the frontend, saved it in a variable, and passed it to the cloud function call.

const emailVerification = await Moralis.User.requestEmailVerification(userEmail);
await Moralis.Cloud.run("sendVerification", {userEmail, emailVerification});

and I changed link: emailVerification, in the cloud function. It works but I get the email only one time, if I have not verified the email and then login again later I wonā€™t get any other verification email.
Is it supposed to be like that?