It seems that the wixUsers.onLogin function has stopped working for dynamic pages that have membership permissions – it may have stopped for other types of pages as well but it is working for non-dynamic and non-membership pages.
I am interested to know if anyone else has observed this behavior - and also to request Wix ( @brett-haralson could you kindly forward to relevant team) to fix the issue.
Background: I have a dynamic page with membership permissions that checks for active session and asks user to login if there is no active session. So this code used to work previously:
Simplified code for Dynamic Page with membership permissions:
import wixUsers from 'wix-users';
$w.onReady(function() {
if(session.getItem("activeSession") === null) {
wixUsers.logout();
} else {
// Code to run during normal active session for page display
}
});
Simplified site-code:
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
$w.onReady(function () {
wixUsers.onLogin( (user) => {
let currentUrl = wixLocation.url;
session.setItem("activeSession", 1);
//Below refreshes page user was on after user completes logging in.
wixLocation.to(currentUrl);
} );
});
However, since yesterday/today: the code just keeps forcing the user to login again and again because the site code is not getting called and therefore the session variable never gets set.
I wanted to share my current workaround so anyone else encountering this could use it till Wix releases a fix (hopefully soon - because the extra checks do delay page display).
Change the dynamic page code to (simplified):
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
$w.onReady(function() {
if(session.getItem("activeSession") === null) {
let user = wixUsers.currentUser;
if(user.loggedIn) {
wixUsers.logout();
}
wixUsers.promptLogin()
.then( (user) => {
let currentUrl = wixLocation.url;
session.setItem("activeSession", 1);
//Below refreshes page user was on after user completes logging in.
wixLocation.to(currentUrl);
})
.catch( (err) => {
console.log(err);
});
} else {
// Code to run during normal active session for page display
}
});
@tabraham , There was a problem back then, that if you had a certain page (dynamic as well as static) with permission limitation, the onLogin() didn’t trigger once the user logged in from the permission notification page.
I don’t know if this problem still exists.
Anyway, as workaround, I checked the loggedIn status once the header got into viewport (which only happened if the user has logged in and had the correct permission).
To avoid running the function every time the user scrolls the page up, I’ve added a counter that triggers:
let loginCounter = 0;
$w("#header1").onViewportEnter((event) => {
loginCounter++;
if (wixUsers.currentUser.loggedIn && loginCounter < 2) {
doIfLoggedIn();//call for my function
}
})
@jonatandor35 Thanks much for responding and sharing your workaround. I implemented my active session and login functionality recently and as of a week ago it was working and then it stopped working this Monday - very frustrating when things stop working for no apparent reason.
Hope someone at Wix will forward the issue to the right team to fix. Thanks again!