I have set up a database for a member dashboard. It ties the members to specific information and a link to member specific pages. I don’t want the members to see each others member specific links. The only way I have been able to do that is to password protect those member specific linked pages. That means the user must enter a password twice (once to get to their dashboard and again to access their specific link). Is there a better way to do that? If I don’t, I have not been able to prevent a member from seeing the other member pages when they are logged in so long as they can determine the URL of those member specific links. Is there a way around this?
That helps. I guess I could check the page URL against my database with the wix-data API, grab the authorized user from that same record, and then compare that to the user ID returned by wixUsers.currentUser. WixUsers only works after the page is loaded; will that work to protect the page?
Well, I guess I’m lost. I have a database with four fields: User Name, Email, ID (automatically generated by Wix), and fileURL. I can grab the current user and the page location as follows:
let user = wixUsers.currentUser;
let userId = user.id;
let url = wixLocation.url;
Now I want to look in my database for the user, and check the vale of the fileURL for that user. If the user is in the database and has that URL in the fileURL field, I want to let him look at the page, otherwise I want to put out an error or in some other way prevent the user from seeing the data on the page. My Javascript is limited. I’m not sure how to query my database to determine a) if the user is in it and b) (if the user is in the database ) what is the user’s fileURL. Once I have that, I’m assuming I could do an if statement to check if the two were equal urls were equal. Can someone point me in the right direction for such a query? An example would be great.
OK. My database has an Email field and FileURL field. I tried this:
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
if (!user.loggedIn){
wixLocation.to('/'); //if the user is not logged in take them to the home page or could set to member only page and go to login
}
let email = user.getEmail(); // logged in user; so get email
let url = wixLocation.url; //current page url
wixData.query("OurTest")
.eq('Email', email)
.find()
.then( (results) => {
if (results.FileURL !== url)
wixLocation.to('/');
} )
.catch( () => {
wixLocation.to('/');
} );
});
I wanted to check if the FileURL set for the user with that email was not the URL of this page. If it wasn’t, I wanted to kick them out. Otherwise, I wanted the page to display. This code seems to work:
if (!user.loggedIn){
wixLocation.to('/');
}
The query, though, is mucked up. Everyone gets taken back to the login page. Can someone help me with this query or tell me where I’m going wrong?
Here’s what finally worked. My biggest issue was remembering that Wix renames database fields even when they might be legitimate javascript names. In any case:
import wixData from 'wix-data'; // Used to query database
import wixLocation from 'wix-location'; // Used to get current page URL
import wixUsers from 'wix-users'; // Used to get the current user
$w.onReady(function () {
// Grab the current user's ID
let user = wixUsers.currentUser;
let userId = user.id; // set user ID. could set userId = wixUsers.currentUser.id
let url = wixLocation.url; //current page url
// Should only have to check for the URL matching in the DB, but just to be sure look for user entry
wixData.query("OurDB")
.eq('_id', userId)
.find()
.then( (results) => {
if ((results.items.length === 0) || (results.items[0].fileUrl !== url)) {
wixLocation.to(`/Test/Update/${wixUsers.currentUser.id}`);
//or wixLocation.to('/'); //would send them to home
}
});});
Hi Gaspar, thanks for sharing. I am having a similar problem, I want to create a membership dashboard area for some of my clients and I am wondering if you can help.
1st, I created my database, which I then linked to a Member Dashboard Dynamic Page I designed.
2nd - I created an index page with a photo grid that links to the Member Dashboard Dynamic Pages based on selection. I am under the impression that the only way to open a dynamic page, is to create some kind of index page, for members to select an field item, which then opens the corresponding dynamic page.
However, the issue I am having is, I do not want all my clients to see all of the other clients on the index page. I don’t know how to have a client log in as a site member and be automatically directed to their corresponding Dynamic Page, which I could some how link by the email they used to register. This email will be a field on their corresponding dynamic page in my database.
The only idea I could even think of was creating an index page (which will be for site members only) that has a user input field. The user input field will allow the member to enter their email and click a button (Open Account Button). The button will perform the action of querying my database for the email address entered (I would put a loader page up that says “Your account information is loading”) and return the Dynamic Page linked to the email entered.
Once users were logged in, I wanted to give these users access to certain files for download, but I didn’t want the users to be able to see other users files. I did this by providing a field in the database identifying the URL to which they were authorized and on which I displayed the files that they could download. The code above kept users out of pages to which they weren’t authorized, and I made the pages member only; so non-members would just get sent to a login page. The link to that page could be displayed as a button on their dynamic dashboard or embedded in their dashboard with an iframe; the iframe (#html1 below) can obtain the url (called theirUrl below) from the database with this code:
No users would know who the other users were and unless they were authorized in the database to reach the download page, they can’t. Your design sounds like an iframe, wouldn’t work, but presumably you could protect each of the pages as I did above and have your images connect to the protected pages. I’m thinking you would have to have multiple URL fields in your database. Your design sounds more dynamic than mine, but I hope that helps.
Thanks for the info Gaspar. To have the database query for the link, how do I adjust your code above. I changed “theirUrl” to the field name corresponding in my database. Is there anything else in the code I need to change for the button
However, I get the error above. I want the code you gave me to look at the current users id and take them directly to their corresponding dashboardLink (their link), just like you did.
Just saw your last post and I think you’re getting things a little confused.
Is the code that you embedded in the HtmlComponent ?
What is the purpose of the iframe tag? An HtmlComponent is an iframe .
Code that is embedded in an HtmlComponent is “standard” HTML/Javascript. Wix Code API is not recognized within an HtmlComponent .
Please give me some more information about what you’re trying to do and I’ll try my best to help.
Hi Yisrael,
I want to create an account dashboard that my site members will be directed to right after logging in. The account dashboard will be a Dynamic Page. I simply cant just create a dynamic page and then make it for site members only, because I would have to send each of my members an individual link for them to access their corresponding dynamic page, after they log in.
So I followed this tutorial which seems to grab the current id and directs the user to their corresponding dynamic page, as long as the dynamic page url has mydatabase/Update/id ( https://support.wix.com/en/article/how-to-create-member-profile-pages-with-wix-code .) This worked, however the problem with this is, the user has to input some type of data for the database linked to the dynamic page to create their user id. If I go to the database and add a new user’s name, email and favorite food for example, before they create an account, When that user using the same email address logs in, the dynamic page does not register the same user logging in.
The only thing I can come up with is creating an account for all of my customers, entering at least one data element in the database. Then importing the rest of the data for all of the fields in the database. This would make the dynamic page complete, then I would send my customers their log in information.
Or just leaving it up to the user to create an account, allowing them to enter their first, last and email. Then I provide a button that links to another dynamic page (this link would be a field n the database I used to create the initial dynamic page).
In the database collection, you should have an ID for each user. Just use that ID in a URL for the dynamic page. So you would have something like this: /Members/ID/
This is defined in the dynamic page setting panel:
In the above example, I want to show data from the Members collection, and I want the dynamic page key field to be ID. To go to this page, follow the instructions in the article Linking to a Dynamic Page .
You should realize that you don’t necessarily need a dynamic page in order to display the user’s dashboard. You can create a dashboard using a regular page. Then in the onReady() function of this page, perform a query based on the user who is currently logged in. The query will be on the Members collection and will be filtered using the _id (the user’s ID). This will result in the query returning that user’s record from the database. The fields can then be populated either manually, or automatically if you’ve used a dataset to connect the elements on the form to the collection.
Yisrael,
I created the page, but as to what exactly to write in the onReady() function of the page, I am not sure. Could you give me an example of a query? The name of my collection is UserAccounts.
I don’t know that this helps, but you can have more than one dataset; so you might capture your additional information from your user in a second dataset…
Gaspar. But how would i connect the current user to the additional data set, to know what information to display for the current user logged in. I am just all confused.
wixData.query("Members")
.eq('_id', userId)
.find()
.then( (results) => {
let items = results.items; // items is an array of results
let item = items[0]; // get the first and only item
// item contains the user's details
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
}
);
Yisrael,
So i just need to copy this query and paste it in the OnReady() function on the page I create and change Members to my actual collection name?
Yisrael, I am not experienced with writting queries. I apologize if I am asking too many questions, but I feel like you are giving me bits and peices each time. I understand how the database and data sets and all that work. I am just really frustrated by all of this issur I am having. Does Wix offer remote desktop support?
We appreciate your interest in really getting the most out of Wix Code and how much you want to learn. Please understand that if you are going to work with code extensively in the product and not just the features in the user interface, you will need to familiarize yourself with basic coding concepts to accomplish what you want. There are a wealth of Javascript coding sites which will help you learn Javascript from basic to advanced - Javascript.info is a good one. The Wix Code Resources page provides tutorials, examples, and articles on getting the most out of Wix Code. We are happy to get you pointed in the right direction, but you’ll need to take it from there. As questions or difficulties arise, we are here to help.
You may also want to check out the WixArena - it’s a hub where you can look for Wix Code and design experts for hire.