Hey, i’ve been trying to use this feature: https://www.wix.com/corvid/example/members-area
I’ve been trying to use this feature for so long, still can’t figure out how. I think, I’ve done all essential changes, but still can’t figure out where i am doing it wrong.
Well, let me share all the codes first:
// data.jsw
import wixData from 'wix-data';
export async function addFile(obj) {
try {
const result = await wixData.query('members_tasks').eq('loginEmail', obj.loginEmail).find({ "suppressAuth": true });
if (result.items && result.items.length > 0) {
const itemToUpdate = result.items[0];
itemToUpdate.file = obj.file;
await wixData.update('members_tasks', itemToUpdate, { "suppressAuth": true })
} else {
await wixData.insert('members_tasks', obj, { "suppressAuth": true })
}
return { insert: true };
} catch (err) {
return { insert: false };
}
}
export async function getAllMembers() {
try {
let tableArray = [];
const result = await wixData.query('Members/PrivateMembersData').find({ "suppressAuth": true })
if (result.items && result.items.length > 0) {
result.items.forEach(obj => {
const { loginEmail, firstName, lastName, picture } = obj;
tableArray.push({ loginEmail, firstName, lastName, picture });
})
return tableArray;
} else {
throw new Error('No items in members collection')
}
} catch (err) {
console.log(err.message);
}
}
//sendEmail.js
import wixCrm from 'wix-crm-backend';
const verificationEmailId = 'verifyRegistration';
export function sendEmailVerification(contactId, approvalToken){
const obj ={
'url': `https://rzgancubing.wixsite.com/test/approve?tokenRegister=${approvalToken}`
}
sendEmail(verificationEmailId, contactId, obj);
}
function sendEmail(emailId, contactId, variables) {
try {
wixCrm.emailContact(emailId, contactId, {
"variables": variables
})
.then(() => {
console.log('email sent successfully');
})
.catch((err) => {
console.log('err in sendEmail is ', err.toString());
});
} catch (err) {
console.log("err", err.toString())
}
}
signIn.js
import wixCrm from 'wix-crm-backend';
import wixData from 'wix-data';
import { sendEmailVerification } from 'backend/sendEmail';
export async function invalidateEmailToken(emailToken) {
return await wixData.remove("tokens", emailToken, { "suppressAuth": true });
}
export async function approveBy3rdParty(contactInfo) {
const contactId = await wixCrm.createContact({ "emails": [`${contactInfo.email}`] });
const tokenForEmail = await createToken(contactInfo, contactId);
sendEmailVerification(contactId, tokenForEmail);
}
export async function createToken(contactInfo, contactId) {
let item;
const objToInsert = {
'email': contactInfo.email,
"firstName": contactInfo.firstName,
"lastName": contactInfo.lastName,
"phone": contactInfo.phone,
"password": contactInfo.password,
"contactID": contactId,
}
item = await wixData.insert('tokens', objToInsert, { "suppressAuth": true });
return item._id;
}
signIn.jsw
import wixUsersBackend from 'wix-users-backend';
import wixData from 'wix-data';
import { invalidateEmailToken, approveBy3rdParty } from 'backend/signIn.js';
export const options = { "suppressAuth": true };
export async function validateEmailToken(emailToken) {
let result = await wixData.query('tokens').eq('_id', emailToken).find(options);
return result.items.length > 0;
}
export async function doRegistration(contactInfo) {
try {
if (contactInfo.email && contactInfo.firstName && contactInfo.lastName && contactInfo.phone && contactInfo.password) {
await approveBy3rdParty(contactInfo);
return { 'approved': true };
} else {
throw new Error("invalid data")
}
} catch (err) {
return { 'approved': false, 'reason': err.message };
}
}
export async function continueRegister(emailToken) {
const item = await wixData.get("tokens", emailToken, options);
const result = await wixUsersBackend.register(item.email, item.password, {
"contactInfo": {
"firstName": item.firstName,
"lastName": item.lastName,
"phones": [item.phone],
}
});
const check = await invalidateEmailToken(emailToken);
return check ? { "sessionToken": result.sessionToken, "approved": true } : { "sessionToken": result.sessionToken, "approved": false };
}
export async function doLogIn(email, password) {
try {
const result = await wixData.query('Members/PrivateMembersData').eq('loginEmail', email).find(options);
if (result.items && result.items.length > 0) {
const sessionToken = await wixUsersBackend.login(email, password);
return { sessionToken, "approved": true };
} else {
return { "approved": false, "reason": "not member" };
}
} catch (err) {
return { "approved": false, "reason": 'err' };
}
}
Approve page code:
import wixUsersBackend from 'wix-users-backend';
import wixData from 'wix-data';
import { invalidateEmailToken, approveBy3rdParty } from 'backend/signIn.js';
export const options = { "suppressAuth": true };
export async function validateEmailToken(emailToken) {
let result = await wixData.query('tokens').eq('_id', emailToken).find(options);
return result.items.length > 0;
}
export async function doRegistration(contactInfo) {
try {
if (contactInfo.email && contactInfo.firstName && contactInfo.lastName && contactInfo.phone && contactInfo.password) {
await approveBy3rdParty(contactInfo);
return { 'approved': true };
} else {
throw new Error("invalid data")
}
} catch (err) {
return { 'approved': false, 'reason': err.message };
}
}
export async function continueRegister(emailToken) {
const item = await wixData.get("tokens", emailToken, options);
const result = await wixUsersBackend.register(item.email, item.password, {
"contactInfo": {
"firstName": item.firstName,
"lastName": item.lastName,
"phones": [item.phone],
}
});
const check = await invalidateEmailToken(emailToken);
return check ? { "sessionToken": result.sessionToken, "approved": true } : { "sessionToken": result.sessionToken, "approved": false };
}
export async function doLogIn(email, password) {
try {
const result = await wixData.query('Members/PrivateMembersData').eq('loginEmail', email).find(options);
if (result.items && result.items.length > 0) {
const sessionToken = await wixUsersBackend.login(email, password);
return { sessionToken, "approved": true };
} else {
return { "approved": false, "reason": "not member" };
}
} catch (err) {
return { "approved": false, "reason": 'err' };
}
}
register lightbox page code
import wixUsers from 'wix-users';
import wixCrm from 'wix-crm';
import wixWindow from 'wix-window';
import { doRegistration, doLogIn } from 'backend/signIn.jsw';
$w.onReady(function () {
wixUsers.currentUser.loggedIn ? wixWindow.lightbox.close() : init();
});
function init() {
onClickLogIn();
onClickSignIn();
onClickBack()
}
function onClickBack() {
$w('#backText').onClick(() => {
presentLogInForm();
})
}
function onClickLogIn() {
$w('#emailLogIn').onChange(() => {
$w('#loginButton').enable();
})
$w('#loginButton').onClick(async () => {
$w('#loginButton').enable();
const email = $w('#emailLogIn').value;
const password = $w('#passwordLogin').value;
const result = await doLogIn(email, password);
if (result.approved) {
await wixUsers.applySessionToken(result.sessionToken); //apply active
wixWindow.lightbox.close();
} else {
if (result.reason === "not member") {
presentSignInForm(email);
} else {
$w('#checkPassword').expand();
console.log("error logIn");
}
}
})
}
async function register() {
const contactInfo = {
'email': $w('#email').value,
'firstName': $w('#firstName').value,
'lastName': $w('#lastName').value,
'phone': $w('#mobilePhone').value,
'password': $w('#password').value,
}
const { approved } = await doRegistration(contactInfo);
if (approved) {
$w('#checkEmail').expand();
$w('#signInBottun').disable();
} else {
console.log("Registration error");
}
}
function presentSignInForm(email) {
$w("#email").value = email;
$w('#containerLogIn').collapse();
$w('#containerRegister').expand();
$w('#backText').show();
}
function presentLogInForm() {
$w('#containerLogIn').expand();
$w('#containerRegister').collapse();
$w('#backText').hide();
}
function onClickSignIn() {
$w('#signInBottun').onClick(async () => {
if (checkValidation()) await register();
else $w('#textValidation').expand();
})
}
function checkValidation() { // you can add more validations as you wish
return $w('#password').value === $w('#secondPassword').value;
}
So, i’ve done all the changes in the code accordingly. I also created the triggered email with a variable named ${approvalToken}. But the thing is the approval token is not interchanging with the approval code. I don’t know if i am even correct. Please help me resolve this issue, been trying so hard for days.
Thanks in advance.