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: {
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!");
}
} );
} );
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).
@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.
In order it should not allow login without approval you need to set it so in the dashboard login settings. Regarding the approval, please see my other comment about third party versus wix email for this case
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.
@givemeawhisky seemed to have missed your reply back then, sorry and a belated thanks.
Actually, if I recall correctly, the user does actually not get logged in before the verification, and therefore wix cannot in fact get the email (no currentUser). I wouldn’t be surprised if this is something which changed after the original solution was proposed. Does it still work in practice for you?
About your comment " you need to be contacting Wix and talking to the Wix Corvid Forum’s Admin and Mods themselves", how is that done? How should I (successfully) reach out to the Admins and mods? Contacting support@wix is useles…