Try like
$('.ngg-gallery-thumbnail a').on('click', function(event){
event.preventDefault();
if (!$('body').hasClass('logged-in')) {
alert('Not logged in!');
return false;
}
$(this).addClass('lightbox');
});
Dont give the lightbox class before,first check for the condition and then if it satisfies then add class for the lightbox