Email Approval token not fully working

Hello everyone,

I have followed every single detail on this tutorial for approval of new website members but it is not fully working. The user will receive the confirmation email with the link and by clicking on the link, she will be directed to the page but from there nothing happens. As admin, I receive a pending membership request and the user info is added to the contact list but user is still not a member and her info is not added to the “SiteMembers” collection.

It would be greatly appreciated if you could let me know what I am doing wrong.
Thanks,
RVN

1 Like

The tutorial you used was provide by Wix Coders . You should contact them for assistance.

Thanks @yisrael-wix . I thought it was posted in this forum so they can help me here.

@yisrael-wix , Does Wix have a tutorial about this topic that I can follow? I have seen other people having similar issues in many other discussions of this forum but none of them got a proper correct answer.

@yisrael-wix Nevermind. I found it here : (I had to scroll down a little bit)

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.

Copy 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,
        "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!");
      }
    } );
} );

Did you manage to solve this pal?
im in the same boat and cant figure it out

If you’re “in the same boat”, that must mean the you figured it out.

See the Best Answer above.

nope not figured it out, mine is the same as above, i cant seem to find the answer in there like RVN did

Hey @martynbell , I used the above code and it worked as Yisrael mentioned. One thing that was causing an issue for me was changing the name of the page that you send your user to for approval. If you change the page name, you need to update the SEO setting manually. If that’s not the case for you, please explain to what extent does the above code work.

@tomantransfers Just a daft thought, do you have your site to auto approve members or manual approval ?
i current ly have mine set to manual

@martynbell Manual

@tomantransfers i have no clue where im messing up here ,

This is my code

Verification page (where link takes you to)

import wixLocation from 'wix-location';
import wixUsers 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
wixUsers.applySessionToken(result.sessionToken);
console.log("Approved");
}
else {
console.log("Not approved!");
}
} );
} );

##Backend

import wixUsers from 'wix-users-backend';

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

export function doApproval(token) {
// approve the user
return wixUsers.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};
} );
}

##reg form

import wixUsers from 'wix-users';
import {doRegistration} from 'backend/register';
import wixLocation from 'wix-location';


export function registerNow_click(event) {
let email = $w('#email').value;
let companyName = $w('#companyName').value;  // the user's email
let password = $w('#password').value;  // the user's password
let firstName = $w('#firstName').value;  // the user's first name
let lastName = $w('#lastName').value; // the user's last name
let emailVerified = $w('#emailVerified').value; // Set email verfiy to false
doRegistration(email, password, firstName, lastName, companyName, emailVerified)
.then( () => {
console.log("Confirmation email sent.");
wixLocation.to('/exit'); 
} );
}

im a beginner when it comes to this so ignore my ignorance.

im able to register and the click the link but i dont get approved

I will take a look at your code. There are times that changes on your website does not take effect immediately which is really irritating. Have you tried clearing your browser history?! In my case, the issue was not the code.

@martynbell sorry, I couldn’t find any issue in your code. Looks OK to me. As I said, in my case the issue was not the code.

@martynbell

I would double check your code from the sample in the best answer or from the API Reference itself, as you have missed out the ‘Backend’ from the API itself,
https://www.wix.com/corvid/forum/community-discussion/tutorial-email-approval-token

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!");
      }
    } );
} );

If you are still struggling, then try this example in this previous post here.
https://www.wix.com/corvid/forum/community-discussion/tutorial-email-approval-token

This is also in the ‘Related Posts’ box on this very forum page too. :wink:

to solve my issue
Dashboard → marketing & SEO → Triggered emails

Create your triggered email and use the email ID in backend code

Hello guys!

I have the same problem, I’m trying to send to my users a confirmation email before they can see a page marked for members only, but it is not working. I have followed both tutorials
but nothing works for me, I think I am starting to despair. I put my code below, hope you can find my mistake.

Thank you guys, Any suggestion is really appreciated!!!

// Backend file called “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: //mydomain.com /
 post-registro?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};
    } );
}

***I have posted the image because I am having issues posting the code
//Custom sign up ( lightbox)

//verification page File called post registro (SEO post-registro)

Lastly, I deleted the browsing history and cache and I also did the following :

Dashboard → marketing & SEO → Triggered emails I Create my own triggered email and I use the email ID in backend code

Please help!