Restricting non-members from downloads

Question:
Is it possible to restrict non-members from downloading files from your site instead of blocking their access to the pages? I want non-members to be able to access every page of my site to see what I have to offer, but not be able to download the files.

Product:
Wix Editor

What are you trying to achieve:
I am trying to allow all visitors to my site to browse the whole site, but for members only to be able to download files.

What have you already tried:
Restricting access to certain pages on my site to non-members.

Additional information:
Many other sites do this but I don’t know if it is possible on Wix editor.

Yes this is possible:

import { authentication } from 'wix-members-frontend';
import wixLocation from 'wix-location';

$w.onReady(function () {
    // Check if the user is logged in
    const isLoggedIn = authentication.loggedIn();

    if (isLoggedIn) {
        // If the user is logged in, enable the download buttons
        $w("#downloadButton").enable();
    } else {
        // If the user is not logged in, disable the download buttons
        $w("#downloadButton").disable();
    }
});


Thank you for your help.
What do I need to do with this code in order to disable users from downloading files? How do I do it on Wix editor?
Also is there a way for a pop up to appear when they try to download without a log in, telling them that they will need a subscription.
Thanks!

Hi Alison

First of all you’ll have to activate Dev-Mode (Velo by Wix).
You’ll have to write the Code on the Page, where you offer the downloads to your customers. I assume the downloads are regular buttons, which have a document linked to them (if not, please tell me).

What the code, I provided you, does is it deactivates the button when a user isn’t logged in, meaning the user is unable to click it, thus no download. If the user is logged in, the button is activated so the user can press it.

If you want to add a message, that the user has to log in, you can either do it with a Lightbox or directly prompt the standard login-form.

Version with standard-Login form:

import { authentication } from 'wix-members-frontend';
import wixLocation from 'wix-location';

$w.onReady(function () {
    // Check if the user is logged in
    const isLoggedIn = authentication.loggedIn();

    if (isLoggedIn) {
        // If the user is logged in, enable the download buttons
        $w("#downloadButton").enable();
    } else {
        // If the user is not logged in, disable the download buttons
        $w("#downloadButton").disable();
        authentication.promptLogin(options)
            .then(() => {
                console.log('Member is logged in');
            })
            .catch((error) => {
                console.error(error);
            });
    }
});

And the version with a Lightbox:

import { authentication } from 'wix-members-frontend';
import wixLocation from 'wix-location';
import wixWindowFrontend from 'wix-window-frontend';

$w.onReady(function () {
    // Check if the user is logged in
    const isLoggedIn = authentication.loggedIn();

    if (isLoggedIn) {
        // If the user is logged in, enable the download buttons
        $w("#downloadButton").enable();
        $w("#downloadButton2").enable();
        //Add other download buttons here 
    } else {
        // If the user is not logged in, disable the download buttons
        $w("#downloadButton").disable();
        $w("#downloadButton2").disable();
        //Add other download buttons here 
        wixWindowFrontend.openLightbox("LightboxName");

    }
});

Thank you so much for your help really appreciated!
They are text that are linked to download documents, not buttons if that makes a difference?

I also just trialled it with a download button on a page and it completely disabled the button for non-members, but I was unable to click it at all for a lightbox to appear.
How that makes sense.

what do you mean by that?

When I inputted the code for a button I had on the page and tested it as a logged in member and someone not logged in:
Logged in it worked as a button and downloaded the file
Not logged it it wasn’t a button anymore and wouldn’t click. It was just a ‘dead’ button that didn’t change colour on hover or anything.
Thanks again for your help

Hello! I’m trying to disable buttons for non-members but I have an issue: I’m using buttons created within the Blog Post. These buttons cannot have IDs assigned to the, so the code doesn’t work. Is there another way to do this? Our blog is going to have “downloadable” content for members only. Also, I’m using WIX STUDIO Editor.