yeah, here is the website URL: https://rzgancubing.wixsite.com/member
I’m trying to make it work on the free version first, then i’ll implement it in my main website!
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
import { continueRegister, continueLogIn, validateEmailToken } from 'backend/signIn.jsw';
$w.onReady(function () {
let emailToken = wixLocation.query.tokenRegister;
register(emailToken);
})
async function register(emailToken) {
if (await validateEmailToken(emailToken)) {
$w('#valid').expand();
$w('#notValid').collapse();
continueRegister(emailToken)
.then(async (result) => {
if (result.approved) {
await wixUsers.applySessionToken(result.sessionToken);
wixLocation.to("/home");
} else {
console.log("Not approved!");
}
}).catch(error => {
console.log("error register");
})
} else {
$w('#valid').collapse();
$w('#notValid').expand();
}
}
import wixData from 'wix-data';
import { addFile, getAllMembers } from 'backend/data.jsw';
let memberSelectedEmail = "";
$w.onReady(async function () {
$w('#membersTable').rows = await getAllMembers()
$w('#membersTable').onRowSelect(event => {
memberSelectedEmail = event.rowData.loginEmail;
$w("#uploadButton").enable();
})
$w('#uploadButton').onChange(async event => {
try {
const uploadedFile = await $w("#uploadButton").startUpload();
const obj = {
'file': uploadedFile.url,
'loginEmail': memberSelectedEmail,
}
const result = await addFile(obj);
if (result.insert) {
$w('#successText').expand();
} else {
console.log('Failed to upload');
}
} catch (err) {
console.log("err" + err);
}
})
});
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;
}
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);
}
}
import wixCrm from 'wix-crm-backend';
const verificationEmailId = 'verify';
export function sendEmailVerification(contactId, approvalToken){
const obj ={
'url': 'https://rzgancubing.wixsite.com/member/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())
}
}
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;
}
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' };
}
}
And this is how my tokens collection looks:
only the contact id field has the exclamation mark.
And this is the triggered email, i’ve setted up:
Please let me know, if you want me to send anything else, Thanks in advance!