Allow file download only if user is logged in

Hi folks,
I’m adding some PDF documents to a page. But I only want to allow people to download those documents if they are logged in as site users. I don’t want to hide the document.
I could perhaps have two images - one that’s just a PDF image and one that is the actual file linkl, and just hide/show each one as appropriate. But this seems overly complicated, plus there could be a number of those PDF documents on the page, and also the page needs to be editable by a non-technical designer (I don’t want them messing with the code!).

Any suggestions?

Thanks!

Hello Mr. Bensky,

how to solve your Problem you can look here at the Wix-Corvid-Reference.
https://www.wix.com/corvid/reference/wix-users.html

Codes like this one could give you an idea.
import wixUsers from ‘wix-users’;

// …
wixUsers.onLogin( (user) => {
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true
let userRole = user.role; // “Member”
} );


Like you said, you can have 2 images and set both to be hidden on load using the properties panel.

Then you can just use the Wix Users API to show the relevant image.

import wixUsers from 'wix-users';

$w.onReady( function() {
      if(wixUsers.currentUser.loggedIn === true) {
         $w("#imageWithLink").show();
      } else {
         $w("#noLink").show();
      }
});

If you have a number of PDF files then better insert them in a database, link the database to a repeater on the page using a dataset and then show/hide the repeater. That would be one of the most straight forward solution.

Thanks - I guess that is what we will have to do!
Can a wildcard be used in the object name? EG I have objects named “pdf1” “pdf2” etc. I tried

$w(“#pdf*”).show();

but that doesn’t work :slight_smile:

Is there a way to achieve this?

Pat