I have followed the article “How to create member profile pages with Wix Code”.
In the login-button onClick-handler, there is the following code:
// prompt the user to log in wixUsers.promptLogin( {“mode”: “login”} ) .then( (user) => { userId = user.id; return user.getEmail(); } ) .then( (email) => { // check if there is an item for the user in the collection userEmail = email; return wixData.query(“Members”) .eq(“_id”, userId) .find(); } ) .then( (results) => { // if an item for the user is not found if (results.items.length === 0) { // create an item const toInsert = { “_id”: userId, “email”: userEmail }; // add the item to the collection wixData.insert(“Members”, toInsert) .catch( (err) => { console.log(err); } ); }
So, if new users to the site are automatically approved, then in the " // add the item to the collection " section, a new user is added to the database.
If on the otherhand, new users require manual approval, then when the site admin approves them, that is presumably done on another computer, and we are no longer inside the onClick handler above. So how do I link the manual approval, by the admin, to executing some code to add the new user to the database?
All the Wix promptLogin stuff is hidden, so I can’t see what’s happening internally.
You asked “if new users to the site are automatically approved…”. In the example, new users are apparently automatically approved. However, it ain’t necessarily so.
Just add an approved field to the database collection, and when creating a new user record, save the user data, but set the approved field to “no”. The approved field is checked when the user logs in. If approved is “no”, then the user is denied access.
The approval can then be done by an admin somewhere else since all of the information that’s needed is in the database.
The tutorial you refer to is just a simple example. A more robust system can be built by adding email verification, approval can be yes/no/pending, different permissions can be assigned to users, etc.
No, we’re not having fun yet. In fact, we are tearing our hair out over this stuff. I have done PHP and Java stuff before, and some database development.
But doing all the code in Javascript alone, is a nightmare.
Add to that, the fact that Wix has things like promptLogin and its members system, which I have no idea what is happening INSIDE of, and it’s a huge mess for me to try and debug.
Let me try and be more specific, rather than just venting.
Okay, say I add an approved field to the database collection, then how do I connect the action of the admin in approving the member, to some code to update the approved field to have a value of ‘true’?
The admin will receive an email saying a member is waiting for approval. They click on a link in their email, which takes them to a wix “contacts” page, on which they can click “approve”.
When they click “approve” - that’s great, they have approved the member within the Wix member’s stuff, but how does my code know that?
First of all, I’m sorry to hear that you’re tearing your hair out…
Let me try to shed some light about this one…
First of all - this thread is discussing the whole members and database relationship.
But, as a TL;DR… here’s a summed up explanation…
I’ll try to put some sense into it by breaking it into three subject:
Wix Members - A Wix App
Wix Code
Collections and site database
Every Wix site can have the Site Members App (regardless of Wix Code).
This means, that when you add a Login/Signup button to your site, users can register, and somewhere in the Wix server there’s a database that hold that information.
The basics of Wix Code is having APIs to communicate with Wix features.
So, looking at the Wix Users feature, you now have the API listed in wixUsers .
Wix Code database allows you to create and manage your own data.
So, let’s say we want to save some EXTRA data regarding our users in the site (for example, their age).
The flow will be as follows:
User gets to signup page (using a button that we added) → a User entry is created in Wix Members (automatically, nothing we need to do) → We ask the user for his age (using a page we created with some sort of input) → we save the info to database (using wixUsers.getCurrentUser() and wixData.insert()).
So what we get actually, is a way for us to save extra info about a signed in users, in terms of database that saves submissions.
But, if an existing user does not submit data, then he won’t appear in our database, and, we can add rows to that database, but this doesn’t mean that a user is created for the site.
For example, I can do to my database and add a row:
USER AGE liran@someemail.com 32
But still, if the user ‘liran@someemail.com’ hasn’t registered to the site, he will have to go through the signup process when he arrives.
Hope this makes it a bit clearer.
Now, for your use case:
You need a way to know whether a user is approved or not in your database, So I think that you should alter the code in a way that adds a row to your database only when a user is logged in, using wixUsers.currentUser.loggedIn and his role is ‘Member’ .
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
//check if we approved him:
if (user.role === 'Member'){
userId = user.id;
return user.getEmail();
}
} )
.then(
//More code here
Thank you very much Liran. I am still working through your post, and assessing what I need to do. I think maybe I need to make big changes to what I have so far.
In the meantime, I have a question about your code above.
In the .then( ) section of your code, you do a check to see if the user.role === ‘Member’, but isn’t that superfluous, as our code only executes the .then( ) if promptLogin returns a promise that evaluates to a logged in user?
Hi Liran,
I too have the same questions as Mike - in how to code in a custom sign in page to handle manual approval through WIX CRM; where the admin is notified each time someone signs up. Then he can decide to approve access or deny.
When you mentioned that a user can be logged in as a Visitor not a member, does this mean he is in pending approval status and cannot access member permission sections of the website?
If so, how do we modify the code to implement this? Please kindly advise, I am quite lost trying to figure this out.
Thanks!