Velo Tutorial: Building Your Own Members Area

Visit the Velo by Wix website to onboard and continue learning.

Note: You can now add a members area to your site without using code. You can also create a page that displays items based on the currently logged-in member without using code.

In this article we demonstrate how to create a profile page for each of your site members. The data for the dynamic profile page is created when a site visitor first logs in to your site. Members can view and update their personal profiles at any time.  

Note: The functionality for logging members in and out of your site only works fully when viewing your published site. You will not be able to fully test your member profile pages in preview mode.

To create our member profile pages we build:

  • A members collection
  • A profile page
  • An update page
  • A login page

This article assumes you are familiar with the following concepts:

Members Collection

We begin by creating a collection named Members, which is where we store all of the member data. Each item in the collection is another member.

Because we want to restrict access to members so they can only view and update their own items, we set the permissions for the Members collection to Custom Use and choose the following:

  • Who can read content from this collection? - Site member author
  • Who can create content for this collection? - Site member
  • Who can update content from this collection? - Site member author
  • Who can delete content from this collection? - Admin

The collection can contain whatever member data you want. In this example, we use the following fields:

Field NameField IDType
TitletitleText
First NamefirstNameText
Last NamelastNameText
EmailemailText
PhonephoneText
AboutaboutText

Profile Page

Next, we create a page to display member profiles. It is a dynamic item page connected to the Members collection. Because we are creating a dynamic item page, we need to choose a field to add to the URL that uniquely identifies each member. Here we use each member's automatically generated IDs to uniquely identify them. 

The URL looks like this:

Because we're only using this page to display data, we set the dynamic dataset's mode to Read-only.

We also set the page's permissions to Members Only using the Permissions tab of the Page Settings so that only members who are logged in can reach the page.

The page itself is designed with elements for displaying the information in a member's profile. In our example, that means text elements that are connected through the page's dynamic dataset to the fields in the Members collection.

Finally, we add a button element. When we create the update page, we will connect the link of the button to navigate to the update page for the current member.

When members check their profile pages, they will see something like this:

Update Page

The update page is very similar to the profile page. It is also a dynamic item page connected to the Members collection. It also uses the members' IDs as the unique identifying field. However, because we can't have two pages with the same URL in our site, we add Update to the URL so it looks like this: 

Because we're displaying data on this page and allowing members to edit their data, we set the dynamic dataset's mode to Read & Write.

Once again we set the page's permissions to Members Only using the Permissions tab of the Page Settings so that only members who are logged in can reach the page.

This time the page is designed with elements for displaying and editing the information in a member's profile. In our example, we'll use a dropdown element, some text input elements, and a text box element. All these elements are connected through the page's dynamic dataset to the fields in the Members collection.

Finally, we add a button element for submitting edited data. We connect the submit button using the Connect to CMS button as follows:

FieldValue
Choose a datasetMembers Item
Click action connects toProfile Page

When members see their update pages, they will see something like this:

Now that we've created the update page, we can return to the profile page to link the update button.

We connect the update button using the Connect to CMS button as follows:

FieldValue
Choose a datasetMembers Item
Click action connects toUpdate Page

Login Page

In our example we have a dedicated page for members to use for logging into the site. However, you can set this page as your homepage, add the contents of this page to any page, or show them on all pages

Note: Because we have a dedicated login page, we'll be adding the code below to this page. However, if you're showing the elements on all pages, you need to add the code below to masterPage.js.

The design of our login page is very simple. It contains two buttons. The first button is used for either logging in or logging out, depending on whether the current member is already logged in or not. The second button is used to navigate to a member's profile page. It is only shown for members who are already logged in.

Code

The code on our login page consists of four parts:

  • Imports for the APIs used in the rest of the code
  • An onReady event handler for setting up the page when it loads
  • A login button onClick event handler for either:
    • logging in a member and possibly creating a new item in the Members collection if it is a first-time login
    • logging out a member who is already logged in
  • A profile button onClick event handler for navigating members to their profile pages

Let's take a look at the code piece by piece to understand what it's doing.

Import APIs and Declare Variables

Copy
1
import { currentMember, authentication } from 'wix-members-frontend';
2
import wixData from 'wix-data';
3
import wixLocationFrontend from 'wix-location-frontend';
4
5
let memberId, memberEmail;

Lines 1-3: First, we import all the APIs we need for our example:
wix-members-frontend: For getting information about the current member and for logging members in and out.
wix-data: For querying and inserting into the Members collection.
wix-location-frontend: For navigating to other pages
Line 5: We declare 2 global variables, memberId and memberEmail.

Check if the Current Member is Logged In

Copy
1
async function checkIfLoggedIn(){
2
const thisMember = await currentMember.getMember();
3
return thisMember ? true : false;
4
}

Lines 1-2: We add an async function to check if the current member is logged in, and await the current member.
Lines 3-4: We return the current member. If we can get the current member, thisMember is true, and if not, then thisMember is false.

Set Up the 'login/logout' Buttons

Copy
1
$w.onReady( () => {
2
if (wixWindowFrontend.rendering.env === "browser") {
3
checkIfLoggedIn().then(res => {
4
if (res === true) {
5
$w("#loginButton").label = "Logout";
6
$w("#profileButton").show();
7
} else {
8
$w("#loginButton").label = "Login";
9
$w("#profileButton").hide();
10
}
11
})

Lines 1-3: In the onReady() function, we set the window rendering environment to ‘browser’ to prevent the code in the onReady() function from running twice. We then call the checkIfLoggedIn() function to see if the current member is logged in.
Lines 4-6: If the current member is logged in, we label the button ‘Logout’, and show the profile button.
Lines 7-11: If the current member is not logged in, we label the button ‘Login', and hide the profile button.

Authenticate the Member on Log In

Copy
1
authentication.onLogin(async (member) => {
2
const loggedInMember = await member.getMember();
3
memberId = loggedInMember._id;
4
memberEmail = loggedInMember.loginEmail;
5
return wixData.query("Members")
6
.eq("_id", memberId)
7
.find()
8
.then((results) => {
9
if (results.Members.length === 0) {
10
const toInsert = {
11
"_id": memberId,
12
"email": memberEmail
13
};
14
wixData.insert("Members", toInsert)
15
.catch( (err) => {
16
console.log(err);
17
} );
18
}
19
})
20
.catch( (err) => {
21
console.log(err);
22
});
23
});
24
}
25
});

Lines 1-2: Still in the onReady() function, we add an async function authenticating the member when they log in, and await the current member data.
Lines 3-4: We set the current member ID and email, returned from the current member data to variables.
Lines 5-25: We search the ‘Members’ data collection for the current member’s ID. If no results are found, then we insert a new item in the ‘Members’ collection for the new member, with their ID and email.

When the Login Button is Clicked

Copy
1
export async function loginButton_click(event) {
2
if(await checkIfLoggedIn() === true) {
3
authentication.logout();
4
console.log('Member is logged out.')
5
$w("#loginButton").label = "Login";
6
$w("#profileButton").hide();
7
} else {
8
authentication.promptLogin({"mode": "login"})
9
.then(() => {
10
console.log('Member is logged in.');
11
$w("#loginButton").label = "Logout";
12
$w("#profileButton").show();
13
})
14
.catch((error) => {
15
console.error(error);
16
});
17
}
18
}

Lines 1-6: We add an async event handler to the login button. When a member clicks the login button, the function first checks if the member is logged in. If the member is logged in, the button’s label already shows ‘Logout’ from the logic in the onReady() function above. So when the member clicks the (logout) button, they are logged out, the button label changes to 'Login', and the profile button is hidden.
Lines 7-18: If the member is not logged in, the button’s label already shows ‘Login’ from the logic in the onReady() function above. So when the member clicks the (login) button, they are prompted to log in, the button label then changes to 'Logout', and the profile button is shown.

When the Profile Button is Clicked

Copy
1
export async function profileButton_click(event) {
2
let loggedInMember = await currentMember.getMember();
3
memberId = loggedInMember._id;
4
wixLocationFrontend.to(`/items/${memberId}`);
5
}

Lines 1-2: We add an async event handler to the profile button. When a member clicks the profile button, the function first gets the current member data.
Lines 3-5: We set the current member ID, returned from the current member data to a variable. We pass the variable to the wix-location-frontend.to() function, sending members to their personal profile page.


Full Example Code

Here is the complete code for this example:

Copy
1
import { currentMember, authentication } from 'wix-members-frontend';
2
import wixData from 'wix-data';
3
import wixLocationFrontend from 'wix-location-frontend';
4
5
let memberId, memberEmail;
6
7
async function checkIfLoggedIn(){
8
const thisMember = await currentMember.getMember();
9
return thisMember ? true : false;
10
}
11
12
$w.onReady( () => {
13
if (wixWindowFrontend.rendering.env === "browser") {
14
checkIfLoggedIn().then(res => {
15
if (res === true) {
16
$w("#loginButton").label = "Logout";
17
$w("#profileButton").show();
18
} else {
19
$w("#loginButton").label = "Login";
20
$w("#profileButton").hide();
21
}
22
})
23
authentication.onLogin(async (member) => {
24
const loggedInMember = await member.getMember();
25
memberId = loggedInMember._id;
26
memberEmail = loggedInMember.loginEmail;
27
return wixData.query("Members")
28
.eq("_id", memberId)
29
.find()
30
.then((results) => {
31
if (results.Members.length === 0) {
32
const toInsert = {
33
"_id": memberId,
34
"email": memberEmail
35
};
36
wixData.insert("Members", toInsert)
37
.catch( (err) => {
38
console.log(err);
39
} );
40
}
41
})
42
.catch( (err) => {
43
console.log(err);
44
});
45
});
46
}
47
});
48
49
export async function loginButton_click(event) {
50
if(await checkIfLoggedIn() === true) {
51
authentication.logout();
52
console.log('Member is logged out.')
53
$w("#loginButton").label = "Login";
54
$w("#profileButton").hide();
55
} else {
56
authentication.promptLogin({"mode": "login"})
57
.then(() => {
58
console.log('Member is logged in.');
59
$w("#loginButton").label = "Logout";
60
$w("#profileButton").show();
61
})
62
.catch((error) => {
63
console.error(error);
64
});
65
}
66
}
67
68
export async function profileButton_click(event) {
69
let loggedInMember = await currentMember.getMember();
70
memberId = loggedInMember._id;
71
wixLocationFrontend.to(`/items/${memberId}`);
72
}

To learn more about the wix-members-frontend module, see the API Reference.

Was this helpful?
Yes
No