[SOLVED] Problem with email verification using cloud functions

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?

I think that this is all you need to do:
const emailVerification = await Moralis.User.requestEmailVerification(userEmail);
without any other cloud function.
and this email will be sent automatically by Moralis when a new user gets registered with an email, or if you add an email to a user entry from the dashboard.

1 Like

Ok thanks a lot @cryptokid, I understand now! I did that and it worked now :smiley:

1 Like

this is so frustratingā€¦ i have been 4 hours trying to send ONLY one email and not only with the verification link but with the userā€™s name as follow ā€œHello USERNAMEā€ but I canā€™t find how to. Also there is no documentation whatsoever for this function Moralis.User.requestEmailVerification(email);

what were you able to do? were you able to change the static text in the template?

found this on google in case that helps you:
https://parseplatform.org/Parse-SDK-JS/api/master/Parse.User.html#.requestEmailVerification

I was able to use this syntax in a template: {{{ link }}} {{{ email }}}

wow, what is that link? i thought requestemailverification was a moralis function. is not?

i just want to be able to send confimration email by myself and not to be sent autmatically by the server each time a user registers, if i set sendgrid template id in the server, then it sends the email by itself!!! 'and i am not being able to say ā€œhello USERā€ with the userā€™s name.

this two works but with out a pair of { I think you have them wrong: {{ link }}} {{{ email }}} but {{user}} or {{username}} or {{first_name}} as I have them in my database, DONT WORK

Moralis server uses parse server and some functionality is directly from parse server.

I donā€™t know if you can get the username value in that template. You could add a request on roadmap.moralis.io

i had to make my own function and build the confirmation link by myself. Not sure if this is the best apporach but it is wokring, here is the cloud function iā€™ve put together if someone needs it, you are wellcome.

you need to remove the template id from the serverā€™s settings for verification, this way it doesnā€™t sent an autmate email each time a user is created in the db, and also you can say ā€œhi nicknameā€ to your users (something that was not possible with the autmated email)

Moralis.Cloud.define("sendWelcomeEmail", (request) => {

  const logger = Moralis.Cloud.getLogger();
    

    const server_url ="https://yourserverurl:2053/server";
    const appid ="your_app_id_here";

    const algo = JSON.stringify(request.user);
	
  
  	let substring = algo.substring( algo.search("_email_verify_token"), algo.search("_email_verify_token") + 48);
  	let sub = substring.split(":");
    const verify_token = sub[1].replaceAll('"', '');
	logger.info("formed link: " + server_url + "/apps/" + appid + "/verify_email?token=" +  verify_token +"&username=" + request.params.email);
    
    Moralis.Cloud.sendEmail({
      to: request.params.email,
      templateId: "d-YOUR_TEMPLATE_ID_HERE",
      dynamic_template_data:{name: request.params.name, link: server_url + "/apps/" + appid + "/verify_email?token=" + verify_token +"&username=" + request.params.email},
      })
  });

Moralis.Cloud.afterSave(Moralis.User, async (request) => {
  // code here
  return "user updated";
});
1 Like