wix-users API

Hello,
I would like to use wix-users module APIs on my site. To control user access to specific page I added wixUsers.promptLogin() in $w.onReady event init function, which seems to works well.
As well I was able to perform logout correctly using wixUsers.logout() in button onClick event handler.
However I want to ensure that user account is also logged out once they close the page.
As I may see right now, user remains logged in after page is closed and reopened.
To workaround it, I used wix-storage session object:
(1) to check if previous session still exists when page starts loading;
(2) to logout user if (1) is false (page was closed previously);
(3) run wixUsers.promptLogin() again.
However this method has some side effects, one of them is that page starts rendering and being visible, and only then login screen appears (I suppose it happens because logout runs asynchronously).

-Is there any event I may use when page is being closed to perform logout?
-Is it possible to recognize user inactivity for specified time and suggest / perform logout?

Thanks,
Dima

1 Like

Are you waiting for the logout promise to resolve?

Yes. And in its .then() I call wixUsers.promptLogin(). This way I think I shouldn’t have race conditions.

Hay Dima,

We do not have the functionality you are asking for at this time.

However, we can help with the workaround you have built - your code should look something like the following -

import {session} from 'wix-storage';
import users from 'wix-users';

$w.onReady(() => {
  if (users.currentUser.loggedIn && 
      !session.getItem('isLoggedInThisSession')) 
    return users.logout();
  else if (!users.currentUser.loggedIn)
    return users.promptLogin()
      .then(() => {
        session.setItem('isLoggedInThisSession', 'yes'); 
      });
});

The trick here is that we are using session storage, which is not persistent between sessions. It is deleted once you close the browser.

If a user is logged in but does not have the isLoggedInThisSession flag set, it means they did not login in this session, and we log them out.

If a user is not logged in, we prompt login. If the login completes ok, we set the flag isLoggedInThisSession in session storage.

Note that in both cases we make sure to return the promises from the $w.onReady function. This makes sure we complete the processing before the page is displayed to the user.

Yoav, thanks for suggestion. I used session in the way you proposed, but didn’t use ‘return’ with promises. I’ll try it out and update about result, just now having some troubles with saving changes in editor.
Regarding the second part of the question - does it make sense to try implementing ‘inactivity’ recognition and logout as result? Or it wouldn’t possible with current tools?

This is exactly what I’m interested in. I would like to log members out after some period of inactivity. I haven’t seen anything that allows that. Is this something that is being worked by Wix?

–Al

1 Like