HELP - userEmail Filter Not Working Correctly

Hi pros, I need help with a problem i’m facing
Let me briefly explain how my setup is.


I have a member-only page (not dynamic page) with a Button that will link to a user specific google drive which i arrange for them manually via a database. The button is connected to the dataset as follow without any filter. I use a code to filter the current user email with the Email field in the “Delivery” database.

I use the code below to filter the user Email for their own respective Google Link.

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

$w.onReady(function () {

 // ...

 let user = wixUsers.currentUser;

 let userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
 let isLoggedIn = user.loggedIn; // true
 let userRole = user.role; // "Member"

  user.getEmail()
    .then((email) => {
 let userEmail = email; // "user@something.com"
      console.log(userEmail);
      console.log(userId);

      $w("#deliverydata").setFilter(wixData.filter()
        .eq("email", userEmail)
      );
    });

});


I have no problem filtering the data. Each member is correctly linked to their own respective google drive links when clicked.

My problem occurs when a New user is signed in (who have not had their email added into the database) when they click on the button it will bring them to the last google drive link added to the database. For example, if the last google drive link was added to member A, when the new member which have not had their email added to the database clicks on the button, it will bring them to member’s A google drive, which is not I want.


[Conclusion]
This happens when the user’s email is not in the database.
Possible solutions I can think of is that :

  1. Code to hide the button when the user’s email is not added to the database yet. (not preferable and don’t know how)
  2. Or automatically add the user’s email into the database, (I tried adding only the email without the google drive link, the button is leads to nowhere, which is good cause it won’t give them access to other people’s GoogleDrive.) (Need code for this solution)

If there’s a better solution please let me know.
I hope what I’m asking is not too confusing to understand, sorry for my bad english.

Thank you in advance.
Best Regards
Ryan

1 Like

You should just auto add their email to the database when they signup, something like this…

import wixData from “wix-data”;

$w.onReady( function () {

$w('#signUpButton1').onClick((event) => { 

let toInsert = {

//change your collection field keys email, firstName, lastName as necessary. Also chaneg the input box ids as necessary

“email”: $w(“#input1”).value,
“firstName”: $w(“#input2”).value,
“lastName”: $w(“#input3”).value
};

//change collection name
wixData.insert(“CollectionName”, toInsert);

}) 

})

Hi Mike, thanks for replying again.
I can’t seem to find the sign-up page to insert the code above, since I’m using wix’s member sign up page, where should i put this code? Do I have to build a custom signup form in order to use it ?

I would prefer to use the native sign-up page by wix-member, anyway to do this ?

thanks

A simple option would be if the user is signing up and is landing on the HP (or any other page), you could place the above code in the page so that it first checks if the email exists in your ‘deliverydata’ database and if not, then does the insert. I have done this using the member page native code.

Hi Pl993

Thanks for your reply.
I don’t quite understand. Can you elaborate ?
Do you mean I need to change the landing page after sign-up (to a custom page with a code)? Can we do that? I can’t find any settings that allow me to change the redirect after signup.

Should I use a code like this to insert the current user email to the database? I tried using this code but it keeps writing a new row each time when its loaded. I need it to happen only once during Sign-up, but I am still confused where to put the code.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
if (wixUsers.currentUser.loggedIn) {
let user = wixUsers.currentUser;
console.log(user);
user.getEmail()
.then((email) => {
let userEmail = email;
console.log(userEmail);
//submit user email to collection
let toInsert = {
"Email": (userEmail) //email is the collection field id , change as necessary
};
wixData.insert("Delivery", toInsert); //change the collection name as necessary
})
}
})

Thanks in advance.

Hi Ryan,

if I assume that the user starts at your home page and decides they want to sign-up. The wix sign-up page will redirect the user back to the original page you signed-up from, generally the home page. So, if this is the main page that you are looking to ensure you can differentiate the new user from an existing user, you can just place the code on your home page.

So, on my website that I am working on, I use the native WIx sign-up via the member page setup. When a user signs-up, they are at the home page, but my home page is nothing more than just some basic content. I have the user go to the “My Account” page. Here, I actually do the check to see if they exist or not and then set them up in my profile database.

If you’re not using a custom member profile database, but another database that is linked to a particular user, just do a query to see if they exist. You can user the user.id query to see if there is a record in the database or their email address. If the result is results.items.length === 0, then there is no entry and you can to an insert of their info to create a record for this new user…

I have a profile database that I’ve created to house just basic user info. When a new user sigs up, i do something like this:
wixData.query(“profile”)
.eq(“email”, userEmail)
.find()
.then((results) => {
if (results.items.length === 0) {
const toInsert = {
“_id”: user.id,
“email”: userEmail,
“other”: other
}
wixData.insert(“profile”, toInsert)

The basic idea is that you can easily check whatever source you are using to maintain your user base. If no record exists then create one.

Hi pl993, thanks for your time writing this.
I have little to no experience in coding.

If I would want to add a code in the “member download” page, will something like this work?

  1. OnReady when the page loads, I want to do a query check if the current user’s email is in the database, if not, add the current user email into the “delivery” database’s FieldID “Email”. If it already existed, then do nothing.

  2. So when the page is done loading, there will definitely be an email field loaded into the dataset with the current user’s email. Then another code for the download button to check if the user’s email is in the database, if yes then it will go to the google link I added backend, if not then the button is dead.

(How can i code for point 1 and point 2 above)

I think I have all the codes needed to do this (provided by Mike and your code), I just can’t figure out how to mix and match to make it work.

Thanks

Anyone can help me ?

@thecandymachinefreel
When you use wix’s default member signup page the member contact details are stored on WIX CRM. Wix CRM is limited at the moment in so far as you can access very little information from it email, userId, member role. Hopefully this will change next year.

To get around the limitations of WIX CRM people generally make another copy off all the member details in a database, I know it’s stupid having to store your member details in 2 locations…

Once you have your member details in a separate database you can read such things as their first and last name, etc.

Building a custom signup page is very easy.

Hi Ryan,

Here’s what you could do:

First get user email
then check against the deliverydata dataset,
if not found, insert into dataset
show download button
if exists, show download button.

So, the real issue is if the user email is not in the system, i.e. a new user.

here’s what this code could look like:
$w.onReady( function () {

$w(“#downloadbutton).hide(); //initially hide the download button
let user = wixUsers.currentUser;
let userId = user.id
user.getEmail() .then((email) => {
let userEmail = email; //get user email
wixData.query(“deliverydata”) //search dataset for entry
.eq(“email”, userEmail)
.find()
.then((results) => {
if (results.items.length === 0) { //if no entry, insert into DB
const toInsert = {
“_id”: userId,
“email”: userEmail,
“other”: other
}
wixData.insert(“profile”, toInsert)
.catch((error) =>{
let errMsg = error;
console.log(“Error”,errMsg);
$w(”#downlaodbutton").show(); //show download button
});
} else { //do nothing since email exists and show download button
$w("#downloadbutton).show();
}
… //rest of your code

Enjoy :slight_smile:

the problem with the above is someone can potentially gain access to someone else’s google drive link simply by signing up as a member and typing in someone else email address during the signup process. This is because Wix CRM does not prevent multiple member accounts being registered under the same email address.

Hey Mike, I agree if Wix does not vet email duplication, then the above code could enable someone else to use the same email address to gain access to the download link.

Seems odd that the native Wix Sign-up function does not verify that only one email can be had in the system? Or, maybe it does???

Wix’s default signup page CRM has no restrictions on the number of accounts that can be registered using the same email address.

Another interesting point is that there is no way for a user to delete/close their account other than contacting the website owner and asking them to delete their account from the CRM.

Hi Pl993, Thanks for spending your time putting together the code for me. I really appreciate it.My problem was solved, although it took me a few hours just to get the code working, I was dumb I thought I had to put the dataset name for the query, but instead it should be the DB collection’s name. The second problem I had is that once the email was inserted, the download button is still hidden, because the page is not refreshed. So I inserted a wix-location in between the codes to refresh the page right after the email insert. Not sure if it’s the correct way to do it, but it worked. The page refreshes almost instantly right after the email insert, (doesnt even look like it was a refresh, more like just it done loading). If there’s a better way to do it, please let me knw. Thank you so much again.

@mikemoynihan99 Hi Mike, I don’t quite understand, I tried signing up again with a same email address as an exisitng user, the system denies the signup and says that the email is already in use. Isn’t it working as it should?
Thanks.

@thecandymachinefreel
if it is now preventing you from doing that then yes. Is it also preventing you from registering with say capital letters ?

I have checked it and yes the default Wix signup is preventing same email registration to the CRM. However if you create a custom signup page using wix.register the CRM will allow multiple accounts under the same email address.

so…I guess, the simple answer to ensure greater security is to leverage the default wix sign-up…Cool :slight_smile:

It has it some draw backs like…

allows weak 4 digit passwords susceptible to brute for attacks

does not ask the member to type their email or password twice for validation

not possible to place age restrictions on the people who become member of your site

obtains very little info about the member (email), does not get their Name, Age, etc.

on the other hand it is easy to use and has the big advantage of having Google & Facebook signup built in which are some of the most fast & secure ways for a user to register for a website.