Hello!
Please, help! I'm trying to make my custom registration lightbox with email confirmation, and it works, but not in part, where new registered user recieve confirmation letter. I also noticed, that in my members database new user have no userid (maybe it's not relevant).
So, thats my code:
import wixUsers from 'wix-users'; import wixLocation from 'wix-location'; $w.onReady(function () { $w('#button3').onClick(function () { let email = $w('#email').value; let password = $w('#pass').value; let phone = $w('#phone').value; let firstName = $w('#name').value; wixUsers.register(email, password, { contactInfo: { "firstName": firstName, "phones": [phone] } }) .then((results) => { wixUsers.emailUser('verifyRegistration', results.user.id, { "variables": { "name": firstName, "verifyLink": `http://yourdomain.com/post-register?token=${results.approvalToken}` } }); wixLocation.to('https://dagoltsov.wixsite.com/pfie/account/my-account'); wixUsers.login(email, password); }) }) })
@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!
I apologize for the multiple posts in the multiple threads (comes from the whiskey I guess), I'm trying to help others avoid the lengthy process I went through.
Thanks for your tip! But it still doesnt work )
I repeated code from reference word for word, and it registering new user (not loggin in), but not sending approval email (and user can login without any approval).
Firstly the ID in a dataset is hidden from view automatically when you create any datasets. To make it visible for yourself, then simply open up the required dataset from your dashboard and click on visible fields and make sure that ID is ticked to be shown. It will now be shown in your dataset, however it may have a grey background to indicate that it is normally hidden from view.
You can easily do what you are trying to achieve through already listed code shown here in the Wix Api (you have to scroll down a little bit to find the code, before you get on to the next section of approve by token):
Wix Users Backend - Approve By Email
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 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}; } ); } /********************************* * 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."); } ); } /************************************** * 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!"); } } ); } );