Logout on website leave (solved, sort of..)

Hi all. Has anybody managed to program their websites to automatically log users out when the close the webpage?

To anyone trying something similar, I’ve spent days on jquery blogs and went through the wix archive, but couldn’t find something that really worked for my purposes.

So what i’ve done was very simple, and efficient, although not foolproof.

I placed two small containers in each corner in the header that somewone would go over if they were to close the browser, made the opacity zero so they’re invisible, and added the code below to the ‘site’ section of the code.

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

export function container9_mouseIn(event) {
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout();
wixLocation.to(‘/’);
}
}

export function container10_mouseIn(event) {
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout();
wixLocation.to(‘/’);
}
}

Note that I added a mouseIn function on both containers, and sent the user to the homepage after logging out, which saves on the code to update all the other buttons.

This is a bit comic, but it works.

@wix - I welcome any other recommendations on how to get this done!

I had a similar problem, and I solved it in a kind of backwards fashion. I imported {session} from ‘wix-storage’ and set a session variable when they login:

 session.setItem('isLoggedInThisSession', 'yes'); 

Session variables “automatically” disappear when the browser session is closed; so I put the following code on the site page:

 if (wixUsers.currentUser.loggedIn && 
  !session.getItem('isLoggedInThisSession')) { 
     //wixLocation.to('/');
     return wixUsers.logout().then(() => {
 	wixLocation.to('/'); 
     }); 
 } 

It doesn’t log the user out when they close the browser, but when they come back, it is another session; so the session variable is gone, and they get logged out…

Now, if you could help me with my timer issue, let me know :slight_smile: https://www.wix.com/code/home/forum/questions-answers/logging-members-out-automatically

–Al

Good work guys. I love seeing the amazing things Wix coders do. :beers:

I implemented @gaspar-al method and it works well on my web pages, but it had one unexpected side effect - it caused one of my Dashboard Pages to endlessly loop.
The Site code loaded followed shortly by the Dashboard Page OnReady function that reads from a collection. The Site code failed at the “return wixUsers.logout().then(() => {” statement above with the fatal error “Cannot sign off Site Owner”. The code crashed and re-loaded the Dashboard Page, causing the Site Code to load, followed by the Page OnReady routine and on and on.

@yisrael-wix Is there any way in Site code to detect you are in a Dashboard page or to stop it running for Dashboard pages? I’m assuming the Dashboard page picked up the session storage item, but I havent tested that. I’ve put a workaround in by simply testing if the user is the Site Owner before the Session storage test and simply exit the code if it is.

@allentrev88 This is an old post and while @gaspar-al had an interesting way to do this, it was really just a workaround. The APIs have been greatly enhanced since then and I would recommend getting the currentUser and then if needed using the user’s loggedIn property to determine if the user is logged in or not.

It might be better to call the above currentUser and loggedIn property before calling wixUsers.logout().

Note: It’s better to start your own topic instead of dredging up an old post. A lot of improvements have been made since then. You can always include a link to the old post if considered relevant.