Member Logged out automatically

Question:

Hi everybody,

I would like the logged in member to be automatically logged out when he leaves the site, or when the browser is closed.
He will then have to log in again during the next visit to the site.
Is this possible?
Thank you in advance for your help.

Product:
Wix Editor

What are you trying to achieve:
To logout the member when he leaves the website

Yes, it is possible to automatically log out a member when they leave the site or close the browser in Wix Editor, but you will need to use custom code for this functionality.

that’s exactly what I want :grinning:

Paste this code it into homepage this will definitely work:)
import wixUsers from ‘wix-users’;
import { local } from ‘wix-storage-frontend’;

$w.onReady(function () {
if (!local.getItem(‘userLoggedIn’)) {
wixUsers.logout();
console.log(‘User has been logged out automatically.’);
} else {
console.log(‘User session is active.’);
}
if (wixUsers.currentUser.loggedIn) {
local.setItem(‘userLoggedIn’, ‘true’);
} else {
local.removeItem(‘userLoggedIn’);
}
wixUsers.onLogin(() => {
local.setItem(‘userLoggedIn’, ‘true’);
console.log(‘User logged in. Session flag updated.’);
});
});

1 Like

To automatically log out a member when they leave the site or close the browser, you can use the window.onbeforeunload event combined with Wix’s wixUsers.logout() function. This detects when a user navigates away or closes the browser and logs them out securely. While effective, it’s worth noting that this approach resets the user session entirely, requiring re-login on each visit, and may behave inconsistently across some browsers. If you’d like to explore this solution further or discuss other innovative coding techniques to enhance your site’s functionality, let’s set up a quick meeting. We can dive into your specific needs, tackle challenges, and brainstorm ideas to optimize your project.

wix-users has been depreciated, I’d recommend using wix-members-frontend.

And this will only work for the homepage then, in order to make it work site wide, it needs to be placed in masterpage.js

2 Likes

This is flawed.

If you log in on the homepage then this code will log out.

If you add this code to the masterpage code, then it will log you out after logging in.

1 Like