Download Button Help

Hi!

I’ve been stuck on this issue for over an hour… I let the button check to see if the user’s logged in, if not, I redirect them to the login. Otherwise, I let them get access to the download link. The download button just doesn’t seem to work because nothing’s downloading. I masked the link as google.com just on this forum, but just know that I put the link there. Even if I did put google.com, it wouldn’t redirect it to google. Do you guys know the solution to this?

So this is my code
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;

$w.onReady( function () {
//TODO: write your page related code here…

});

export function download_click(event, $w) {
let user = wixUsers.currentUser;
let isLoggedIn = user.loggedIn;
if (isLoggedIn) {
wixLocation.to( ‘http://www.google.com/’ );
} else {

    wixUsers.promptLogin() 
} 

}

Hello.

Have you added the onClick event handler to the button? Your code works for me.

You can share the link of your site and the name of the page if you are still experiencing the issue. Only Wix employees with the right permissions can be able to access your site editor.

Cheers!

https://www.yachaywasi.us/covid-19 If you look at the download button. Everything seems to work as far as checking to see if they’re logged in except the google part.

And yes I did check onClick on the event handler

You have two options here.

You can simply have the download button hidden on load and only shown if the user is logged in from which they can go to the page url or download a file that you want to use instead etc…

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function () {
	if (wixUsers.currentUser.loggedIn) {
		$w('#download').show();
	} else {
		$w('#download').hide();
	}
});

wixUsers.onLogin((user) => {
	let userId = user.id;
	let isLoggedIn = user.loggedIn;
	let userRole = user.role;
	$w('#download').show();
});

export function download_click(event, $w) {
	wixLocation.to('http://www.wix.com');
}

Using the onLogin function here makes sure that the download button is shown on this page if they login on the same page without having to refresh the page to get the code to run again when they are logged in.

Or to do something like you have done already, which as Sam has already stated in his reply that it works. I have also tested it on a dummy site and it all works for me too.

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function () {
});

export function download_click(event, $w) {
	let user = wixUsers.currentUser;
	let isLoggedIn = user.loggedIn;
	if (isLoggedIn) {
		wixLocation.to('http://www.google.com');
	} else {
		wixUsers.promptLogin()
	}
}

Just make sure that you are testing anything with Wix Users API on your live published site and not just through the preview mode as it won’t always work for you, as stated in the Wix Users API Reference.

Note
The APIs in wix-users are only partially functional when previewing your site.
View a published version of your site to see their complete functionality.