Working on preview mode but not on live mode

@russian-dima Well i tried it and it is sooo close! The registration code works perfectly in preview mode ( it is generating an ID for each member) but it is not working on live mode (it does the registration but it does not insert ID to the members collection). On the login side, i changed the code for a simple one and it is loging in but to the last ID generated, not to the related one.

I know that i am bothering too much, but a little more help and i think this will be solved! :grin:

Login Code:

$w.onReady(function(){ 
 $w('#loginNow').onClick(function (){ 
 let email = $w('#email').value; 
 let password = $w('#password').value;

wixUsers. login(email,password) 

 .then( () => {
const milliseconds = 5 * 1000; 
 setTimeout( function(){
 wixLocation. to("/inicio");
 } , milliseconds); // 
 $w("#loginNow").hide();
 $w("#boxLog").expand();
 $w("#text38").hide();
 })

 .catch( (err) => {
 console. log(err);
 $w("#text38").show();
 })

 })
})


@cryptoirt
I had similar problem. It worked in Preview but not in live. The thing is that while you are in Preview mode, the current user is Admin and do not have any restrictions.

In the doRegistration function or any function that is dealing with contacts and members’ info, you need to use options. Options are declared as following.

const options = {
 "suppressAuth": true,
 "suppressHooks": true
 };

Depending on the functions that you use, you may put a code similar to this:

 // Get the contact's last revision number
 const myContact = await contacts.getContact(parContactId, options);
 const contactIdentifiers = {
 contactId: parContactId,
 revision: myContact.revision
 };

Further information you may find in https://www.wix.com/velo/reference/wix-crm-backend/contacts-obj/getcontact.

Hope it helps.

PS Kudos to wixsupport that guided me to find the solution that works. The trick was that any function that is related to Contacts and Members needs to have Options to bypass the default (collection) permissions. Needless to say, you need to use these functions in your backend.

@russian-dima Hi!! I am trying to use the RESULTS that you created, but in the login code. I think that it could work generating an ID for the members (and not have restrictions for the live mode).

Code:

$w.onReady(function(){ 
 $w('#loginNow').onClick(async function (){ 
 let email = $w('#email').value; 
 let password = $w('#password').value;

 if ($w('#password').valid) {

 let RESULTS = await wixUsers.login(email,password) 
 console.log("RESULTS: ", RESULTS)
 let userID = await RESULTS.user. id; console.log("User-ID: ", userID)

 const toInsert = {
 "_id": userID,
 "email": email,
 "password": password,
 };
 // add the item to the collection
 wixData.insert("Members", toInsert)

 .then( () => {
const milliseconds = 5 * 1000; 
 setTimeout( function(){
 wixLocation. to("/inicio");
 } , milliseconds); // this is to put a gif when login run and to show a message if error. 
 $w("#loginNow").hide();
 $w("#boxLog").expand();
 $w("#text38").hide();
 })

 .catch( (err) => {
 console.log(err);
 $w("#text38").show();
 })

 } 
 })
})

The problem is that this code give the error “Cannot read property ‘user’ of undefined”. Could you help me to solve this, pleaseee?

@venkog Thanks for your response! I am trying to undestand where do i have to put those lines. Should it go in a new backend module or with the DoRegistration function in the register backend module?

Register Module:

import wixUsers from 'wix-users-backend';
import wixData from 'wix-data';

export function doRegistration(email, password, firstName, lastName) {
 
 // register the user
 
 return wixUsers. register(email, password, {
 "contactInfo": {
 "firstName": firstName,
 "lastName": lastName
 }
 })
 .then(async(RESULTS)=> {
 await wixUsers. emailUser('verifyRegistration', RESULTS.user. id, {variables: 
 {approvalToken: RESULTS.approvalToken}})
 return RESULTS

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


Do i have to put something in the register front-end code?

$w.onReady(function(){ 
 $w('#RegButton').onClick(async function(){

let firstName = $w("#nombre").value 
let lastName = $w("#apellido").value
let email = $w("#email").value
let password = $w("#password").value
let confirmPassword = $w("#confirmPassword").value

 if ($w('#password').value === $w('#confirmPassword').value) {

 let RESULTS = await doRegistration(email, password, firstName, lastName)
 console.log("RESULTS: ", RESULTS)
 let userID = RESULTS.user. id; console. log("User-ID: ", userID)

const toInsert = {
 "_id": userID,
 "email": email,
 "nombre": firstName,
 "apellido": lastName,
 }
 wixData.insert("Members", toInsert)
 }
else {
 $w("#text40").show()
}
})
}) 

@cryptoirt
I am not completely sure what you want to achieve - I assume you would need approval process for new members by email.

Here is an example https://www.wix.com/velo/reference/wix-users-backend/approvebytoken. The 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.

Step by step explanation you may find here - https://www.wix.com/velo/forum/main/comment/5c6065bfde840e00abde1db3.

In order this to work, you need to create triggered email.

Velo Tutorial: Sending a Triggered Email to Contacts | Help Center | Wix.com.

so that the function for sending emails will know which email template to use.

 wixUsers.emailUser('verifyRegistration', contactId, {
 "variables": {
 "firstName": firstName,
 "lastName": lastName,
 "company" : contactInfo.company,
 "verifyLink": `https://www.yourdomain.com/post-registration?token=${results.approvalToken}`
 }
 } );

In the example above, please note the text verifyRegistration - it is the email ID in the triggered email that you need to create upfront.

Hope it helps. My advise is to stick to one example and go to the end - do not mix example because it could generate unpredictable results.

@russian-dima This worked perfectly and all i have to do then was change the settings permissions on my collection database. PROBLEM SOLVED!

@cryptoirt EXACTLY !
Or using → Suppress-Options.

const options={"suppressAuth":true,"suppressHooks":true};

This is what → @venko gligorov wanted to tell you.