Registration Email Flow

Hello,
I have created a verification registration for new members email flow. I have followed the step by step listed in this article https://www.wix.com/code/reference/wix-users-backend.html#approveByToken
I am working with: Register a user sending an email for confirmation
I have created the codes for the following pages:
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.

I have a couple of questions. Hopefully someone will be able to provide recommendations?

For the page where the new member will verify registration I have created a lighting box with a button.

Will this button automatically provide access to that members profile page. based on the code below? there is no mention of a button in the code. Do I even need a button. I am a little confused on this process. the results of the code is suppose to " log the user in.

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

The way I understood this code:
user register > confirmation email sent with verification link > User clicks on link that will lead to verification page > user has access to members page. Is this correct understanding?

Yes, useful! The email flow is really something I didn’t know how to do https://escortinantalya.info, but thanks to you I learned! Thank!

As it states on the page:
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.

So firstly, you will use the page code for the page where users register (which in your case is your lightbox):

/*********************************
 * client-side registration code *
 *********************************/
import wixUsers 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.");
    } );
}

Secondly, this will then take the necessary and push it through the backend register jsw:

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

export function doRegistration(email, password, firstName, lastName) {
  // register the user
  return wixUsers.register(email, password, {
    "contactInfo": {
      "firstName": firstName,
      "lastName": lastName
    }
  } )
  .then( (results) => {
    // user is now registered and pending approval
    // send a registration verification email
    wixUsers.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 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};
    } );
}

With this line of code then directing it to the confirmnation page:

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

Then your user will automatically be directed and be on your confirmation page:

/**************************************
 * client-side post-registration code *
 **************************************/
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!");
      }
    } );
} );

From here the user is logged into your site, however you will need to add your own links from the page which the user can navigate from.

You can either just leave your page as it is and simply let the now logged in user go and use your header with your menu on it.

Or you could setup a couple of buttons or something that is set in the code to only be shown when the user is logged in and if not approved then the buttons are still hidden.

This exactly what I needed it hear. I set it up correctly YAY! I was unsure about what the user does after they see my confirmation page. Thanks for the clarification.

@givemeawhisky It appears that the code as described here doesn’t work, as wixUsers.emailUser only works for users who are already logged in, as described here and here . I worked around this by using third party email (there’s a mailgun.js module readily availabe)
But otherwise, the flow works good, thanks a lot!

If you are having issues with the ‘Register a user sending an email for confirmation’ code, then you need to be posting your own forum post and if you think that the code supplied by Wix in the register api info is wrong, then you need to be contacting Wix and talking to the Wix Corvid Forum’s Admin and Mods themselves.

As for the code above, then they are already registered and technically logged in so that Wix code can get the email address of the user, that is how they can send the verification email to them so that the user can then validate it by clicking on the link on the email and then the user is allowed full access as a logged in member, after the session token is approved.

Finally, yes I know that there are node packages already installed in Wix Package Manager that you can use, the likes of SendGrid are in there too as all described in triggered emails links below and you can also use emailJS too.

You are mixing up the above register code option for when you are purely just using the emailUser function to send a triggered email yourself.

https://support.wix.com/en/article/creating-a-triggered-email
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-contacts
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-members
https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission

" As for the code above, then they are already registered and technically logged in "
As far as I was able to determine, they were not yet logged in. It doesn’t really make sense they should be able to log in if we require verification… (If we do not require user verification, verifying email is of very limited use).
You seem to be implying there are 2 levels of “logged in”. Is that so?