Clickable download buttons only for site members

Hi, user3965 !!

I wanted to try it out myself, so I gave it a shot. I think this should work for now! Please make any improvements on your own! :wink: :+1:

I used a method where the page is reloaded once after the login process, but if anyone knows a better solution, please let me know. :thinking:

// frontend

import { getPdfDownloadUrlFunction } from "backend/getDownloadUrl.web.js";
import { authentication } from "wix-members-frontend";
import wixLocationFrontend from "wix-location-frontend";

$w.onReady(async function () {

    try {

        const isLoggedIn = authentication.loggedIn();

        if (isLoggedIn) {

            $w("#downloadButton").label = "You can download!";

            try {
                const downloadLink = await getPdfDownloadUrlFunction(0);
                $w("#downloadButton").link = downloadLink;
            } catch (error) {
                console.log(error);
                $w("#downloadButton").label = "Error fetching download link.";
            }

        } else {

            $w("#downloadButton").label = "You can't download...";

            $w("#downloadButton").onClick(() => {
                authentication
                    .promptLogin({ mode: "login", modal: true })
                    .then(() => {
                        wixLocationFrontend.to(wixLocationFrontend.url);
                    })
                    .catch((error) => {
                        console.log(error);
                    });

            });

        }

    } catch (error) {
        console.log(error);
    }

});

// backend

import { Permissions, webMethod } from "wix-web-module";
import { mediaManager } from "wix-media-backend";

export const getPdfDownloadUrlFunction = webMethod(
    Permissions.SiteMember,
    async (pdfUrlsNo) => {

        const pdfUrls = [
            "wix:document://v1/ugd/abcd1234.pdf/first_document.pdf",
            "wix:document://v1/ugd/efgh5678.pdf/second_document.pdf",
            "wix:document://v1/ugd/ijkl9101.pdf/third_document.pdf"
        ];

        const myFileDownloadUrl = await mediaManager.getDownloadUrl(pdfUrls[pdfUrlsNo]);
        return myFileDownloadUrl;

    }
);