HOW TO SETUP LINK TO CONFIRM REGISTRATION INTO EMAILTRIGGER ?

I want to send triggered email with link to confirm registration of a member on my site.

Email trigger it’s send ok, received by the user with the link.

But link contained into mail doesn’t work. This because variable " approvalToken " is not set correctly : ( ?token= )
I think I haven’t set path correctly into the email trigger template.

Could you please give me help about link set into the mail trigger ?
You use URL path +AddVariable ? How to setup it correctly ?

I put below my code that I use into my application :

"/*******************************
* backend code - register.jsw *
*******************************/

import wixUsersBackend from 'wix-users-backend';
export function doRegistration(email, password, firstName, lastName) {
// register the user
return wixUsersBackend.register(email, password, {
"contactInfo": {
"firstName": firstName,
"lastName": lastName
}
} )
.then( (results) => {
// user is now registered and pending approval
// send a registration verification email
wixUsersBackend.emailUser('verifyRegistration', results.user.id, {
"variables": {
"name": firstName,
"verifyLink": `https://mywebsite.com/verification?token=${results.approvalToken}`

}
} );
} );
}

export function doApproval(token) {
// approve the user
return wixUsersBackend.approveByToken(token)
// user is now active, but not logged in
// return the session token to log in the user client-side
.then( (sessionToken) => {
return {sessionToken, "approved": true};
} )
.catch( (error) => {
return {"approved": false, "reason": error};
} );
}

/*********************************
 * client-side registration code *
 *********************************/

import wixUsersBackend from 'wix-users';
import {doRegistration} from 'backend/register';

export function button7_click_1(event) {

let firstName = $w("#input12").value     // My input values
let lastName = $w("#input11").value
let email = $w("#input10").value
let password = $w("#input9").value

doRegistration(email, password)
.then( () => {
console.log("Confirmation email sent.");
} );
}
/**************************************
 * client-side post-registration code *
 **************************************/

import wixLocation from 'wix-location';
import wixUsersBackend from 'wix-users';
import {doApproval} from 'backend/register';

$w.onReady( () => {
// get the token from the URL
let token = wixLocation.query.token;
doApproval(token)
.then( (result) => {
if (result.approved){
// log the user in
wixUsersBackend.applySessionToken(result.sessionToken);
console.log("Approved");
}
else {
console.log("Not approved!");
}
} );
} );

And this is how i’ve set up link into email trigger :

Thanks a lot for the help ! :blush:
Mattia

Why my first post about this question is now closed ???

Assuming you are talking about this post here.
https://www.wix.com/corvid/forum/community-discussion/send-email-trigger-with-confirmation-link-about-member-registration?origin=member_posts_page

Well it wasn’t closed when I looked at it before doing this reply.

However, as it is just a complete copy of this post above, I will close it myself as it is a duplicate post.

1 Like

https://www.wix.com/corvid/reference/wix-users-backend.html#approveByToken

Register a user sending an email for confirmation
This example demonstrates a common email verification flow. A user is initially registered but not yet approved. At registration, a verification email is sent with a link to a verification page. When a user goes to the verification page, the approval is granted and the user is logged into the site.
The code is split between three locations:

  • A backend web module named register.jsw.

  • The page code for the page where users register.

  • The page code for the page where users confirm their registration.

/*******************************
 * backend code - register.jsw *
 *******************************/
import wixUsersBackend from 'wix-users-backend';

export function doRegistration(email, password, firstName, lastName) {
  // register the user
  return wixUsersBackend.register(email, password, {
    "contactInfo": {
      "firstName": firstName,
      "lastName": lastName
    }
  } )
  .then( (results) => {
    // user is now registered and pending approval
    // send a registration verification email
    wixUsersBackend.emailUser('verifyRegistration', results.user.id, {
      "variables": {
        "name": firstName,
        "verifyLink": `http://yourdomain.com/post-register?token=${results.approvalToken}`
      }
    } );
  } );
}

export function doApproval(token) {
  // approve the user
  return wixUsersBackend.approveByToken(token)
  // user is now active, but not logged in
  // return the session token to log in the user client-side
    .then( (sessionToken) => {
      return {sessionToken, "approved": true};
    } )
    .catch( (error) => {
      return {"approved": false, "reason": error};
    } );
}

/*********************************
 * client-side registration code *
 *********************************/
import wixUsersBackend from 'wix-users';
import {doRegistration} from 'backend/register';

export function button_click(event) {
  let email = // the user's email address
  let password = // the user's password
  let firstName = // the user's first name
  let lastName = // the user's last name

  doRegistration(email, password, firstName, lastName)
    .then( () => {
      console.log("Confirmation email sent.");
    } );
}

/**************************************
 * client-side post-registration code *
 **************************************/
import wixLocation from 'wix-location';
import wixUsersBackend from 'wix-users';
import {doApproval} from 'backend/register';

$w.onReady( () => {
  // get the token from the URL
  let token = wixLocation.query.token;

  doApproval(token)
    .then( (result) => {
      if (result.approved){
        // log the user in
        wixUsersBackend.applySessionToken(result.sessionToken);
          console.log("Approved");
      }
      else {
        console.log("Not approved!");
      }
    } );
} );

In your backend jsw file make sure that your actual website address is correct in this line here.

"verifyLink": `http://yourdomain.com/post-register?token=${results.approvalToken}`

So make sure that this line of yours is the correct website address before the ?

"verifyLink": `https://mywebsite.com/verification?token=${results.approvalToken}`

Also, the same with this line here.

    wixUsersBackend.emailUser('verifyRegistration', results.user.id, { 

Make sure that your triggered email is the same name as in your code.

wixUsersBackend.emailUser('verifyRegistration', results.user.id, {

Lastly for this section, make sure that the triggered email link is all of it including the variable at the end of ${approvalToken}

Client Side Registration Code.

let firstName = $w("#input12").value
let lastName = $w("#input11").value
let email = $w("#input10").value
let password = $w("#input9").value

Make sure that your lets have the ‘;’ at the end of them.

If you can’t get it to work then have a look at previous forum posts here.
https://www.wix.com/corvid/forum/community-discussion/email-approval-token-not-fully-working
https://www.wix.com/corvid/forum/community-discussion/tutorial-email-approval-token

I’ve now fixed the code and this is my actual source code.
Email received correctly with link, but when i click on link this not function well : user is blocked and process of approval member not executed. I must do approval member manually from the panel control.
Con you give me support to understand where i’m wrong in my code ?

/*******************************
* backend code - register.jsw *
*******************************/

import wixUsersBackend from 'wix-users-backend';
export function doRegistration(email, password, firstName, lastName) {
// register the user
return wixUsersBackend.register(email, password, {
"contactInfo": {
"firstName": firstName,
"lastName": lastName
}
} )
.then( (results) => {
// user is now registered and pending approval
// send a registration verification email
wixUsersBackend.emailUser('verifyRegistration', results.user.id, {

"variables": {
 "name": firstName,
 "approvalToken":results.approvalToken,

}

} );
} );
}



export function doApproval(token) {
// approve the user
return wixUsersBackend.approveByToken(token)
// user is now active, but not logged in
// return the session token to log in the user client-side
.then( (sessionToken) => {
return {sessionToken, "approved": true};
} )
.catch( (error) => {
return {"approved": false, "reason": error};
} );
}


/*********************************
 * client-side registration code *
 *********************************/


import wixUsersBackend from 'wix-users';
import {doRegistration} from 'backend/register';

export function button7_click_1(event) {

let firstName = $w("#input12").value     // My input values
let lastName = $w("#input11").value
let email = $w("#input10").value
let password = $w("#input9").value

doRegistration(email, password)
.then( () => {
console.log("Confirmation email sent.");
} );
}
/**************************************
 * client-side post-registration code *
 **************************************/

import wixLocation from 'wix-location';
import wixUsersBackend from 'wix-users';
import {doApproval} from 'backend/register';

$w.onReady( () => {
// get the token from the URL
let token = wixLocation.query.token;
doApproval(token)
.then( (result) => {
if (result.approved){
// log the user in
wixUsersBackend.applySessionToken(result.sessionToken);
console.log("Approved");
}
else {
console.log("Not approved!");
}
} );
} );

Thanks a lot for the support to resolve my problem…

Right guys, I think I wrote one of the first examples on how to do this a little over a year ago. ( https://www.wix.com/corvid/forum/community-discussion/tutorial-email-approval-token ) but as of late, it started malfunctioning. What I picked up is that it all works pretty well up until to point that one clicks on the link on the email. Should one copy the link and paste it into a browser url, it all still works, however when clicking directly on the link in an email, WiX seems to override whatever follows the “?”-mark with some of their own stuff, in effect removing the approval token from the link, causing things to break. So now the looooooooooooooooooong way round to do this would be to set up a SendGrid account and use this for the email, but if anyone has done this recently, they would know it is a real pain because as of late, one has to jump through about 72 hoops before they will activate the account.

@WiX - could you possibly offer some insight as to why WiX overrides the approval token?