How to create a list of users?

I’m a coding novice and am getting lost in the wealth of information available. I’m hoping someone can set me in the right direction, or at least confirm that what I’m trying to do is doable.
I want to control what pages my users can see based on what programs they are enrolled in.
I have created a data collection keyed to a user that contains yes/no fields for the various programs we offer. (I’d like to use the same id that wix-users has as the key to this collection, but I could also match on name or email.) I know I’ll be able to add code to each page that can check that a user has permission to see a page before loading the page.

What I’m having difficulty figuring out is how to connect a user to this collection.

I’d like to create a list of existing members, then I could select a member and go to a page to update the programs that they have access to. But I can’t figure out how to create a list from wix-users; is there a way to do this?
Or maybe I could go in the reverse direction, create a list of users from my collection and then if I add a new user to the collection add a user record to wix-users? Is that even possible?

One other option I explored was using the category groups associated with a user subscription to reflect the programs we have. I can create group names for each program. But I could not find any references anywhere that would tell me how to check which categories a logged in user has access to. Is that possible?

One important thing to understand is that our organization would set up a member and then send them a link to create their login password. Access to this site would be a part of their program. We do not invite the public to join this site.

Hey

  1. Make manual approval on site signups to approve them manually.

  2. Create a new collection which will hold all data about a member except their actual user-object from wix-users. Connect it by users id which you can find using the _id field name.

  3. When they login get the current user, get the id and then show the correct data.

  4. To create a list of users you will have to make your own collection or use the one in my point 2 and connect that to a table, you can’t reach the wix-users as a collection (would be nice).

  5. Create the different programs / options in the collection you have in number 2.

Does this make any sense at all? (If you want a video tutorial on membership workflow signup and ask for it at wixshow.com)

Thanks for your quick response. It’s looking like I’m not going to be able to do what I want, but I may be missing something.
I want to preload what programs a user has access to so that the first time a user logs in he will have access to the correct program pages. My workflow would be as follows:

  1. manual approval is set
  2. On the search contacts page, ADD a new contact
  3. connect my collection record to the new contact
  4. select which programs the new contact has access to (update the collection record)
  5. approve the new contact and send an email telling them to sign in

I’m trying to find a way to do step 3. The only way I can find to see wix-user data is to find the current logged in user. If I’m working as an admin how can I fill in the _id field on my collection record ? Can I search the users for a matching name or email?

You can get the user by email instead, use the getEmail method which you can read about in the API docs. That would probably be the only way but there is no way to list users for now. I will write feature request of that.

Hello!
I cannot get current user email by getEmail method. What did i do wrong?

import wixUsers from ‘wix-users’;
$w.onReady(function () {
let user = wixUsers.currentUser;
let greet = $w(“#Greeting”); // #Greeting is a text box on web page
let userEmail = “empty”;
user.getEmail()
.then( (email) => {
userEmail = email;
} );
greet.text = userEmail;
greet.show();
});

#Greeting textbox shows “empty”. But “member login button” component at the page header shows user email correctly like this “Hi, email@domain.com

If I change penultimate line to greet.text = user.id; then #Greeting textbox shows string like this “db1e11c2-3439-48c6-995d-01df40035280” which must be correct

Does it mean currentUser object stores empty email info, why?
I can see all registered users of my site in WixContacts and they all have email.

Are you testing on preview mode or on a published site? Preview could not be used very well to try it out I think.

I am testing on published site, not on preview mode.

Okey, try this. Move the email variable out of the functions to see if it can’t be reached by inner functions maybe.

import wixUsers from 'wix-users';
var userEmail;
$w.onReady(function () {
	let user = wixUsers.currentUser;
	let greet = $w("#Greeting");  // #Greeting is a text box on web page
	user.getEmail()
	  .then( (email) => {
   	userEmail = email;    
 	} );
	greet.text = userEmail;
	greet.show();
});

still doesnt show email. May I somehow forward you a copy of my site please?

The problem is that the code is not executed
user.getEmail() .then( (email) => { userEmail = email; } );
If I change it to
userEmail = “empty”; user.getEmail() .then( (email) => { userEmail = "Not empty but equals " + email; } );
then #Greeting textbox will be still “empty”

Well you could invite me as a member somehow I guess to andreas@pilgrimsbo.se and I can look and then you can delete me.

So I have logged in on your site and fixed it. It seem you will have to put the userEmail value to the page control inside the then function and it will work. Outside it is undefined or falls back to the predefined value. I have fixed it anyway so it works and now you can make the code look good :slight_smile:

Thank you for big help. It seems that the code executes not consequentially.

  1. user.getEmail()
  2. .then( (email) => {
  3. userEmail = email;
  4.  greet.text = 'Hi ' + userEmail; 
    
  5.  } ); 
    
  6. greet.text = 'logged in with ’ + userEmail;

Line 6) executes before lines 3) и 4) when userEmail is not yet estimated.
Do you know is it bug or feature of WixCode?
What is “.then =>” syntaxis?

.then is a way to wait for execution in javascript when you get a so called promise back. This is a way of handling and if you want all to work ok you will have to wait for then to execute and do stuff within the then promise not outside. The outside will execute before it waits for then. It’s like sync and async.

1 Like