OnLogin function is not clearing the session.

I’m trying to clear the previous session as soon the user login using .onLogin() in the site code.

Since, I could not find any other way to clear the session while user logs out, so I thought I will clear the session as soon as an user login. But session.clear() doesn’t seems to be working at all. Session will still hold all the previous users records in it.

wixUsers.onLogin((user) => {
        session.clear();
 let userId = user.id; // 
 let isLoggedIn = user.loggedIn; 
 let userRole = user.role; 
        getCurrentClientID();
        wixLocation.to("/dynamic-dashboard");
    });

As you can see, first thing I’m trying to clear the session. But for some reason it is not at all working.

Anybody has any idea, what am I doing wrong?

Thanks

How are you storing the session data? How do you know it isn’t cleared?

Ok, here is what I’m trying to do with session:

As user first logs in, I’ll fetch user details and some access details from DB and then store it to session using wix session storage method. I store it to session, because for almost all pages, there are access checks and I need user details, if user makes any update or insert, so to avoid multiple calls to DB, I save it to session.

How I’m storing to session?
I’m calling a method which will first check if session has any value/data, if not then fetch the data from DB using currentuser.id. So, when I call these methods every-time in all different places in my app, it will first check if session has value if not then get it from DB. I did this to improve the performance of my web app.

Now here is what going wrong.
All my methods works fine, but as I noticed if one user (let’s say Patrick) logs out from the app (using wix member navbar), session will still holds all the data of Patrick who has already logged out, and when an another user (let’s say Mike) logs into the same computer and in the same browser. The app responds as if, it is Patrick, because session still holds Patrick’s information. Though Mike is logged in and session has values/data of Patrick my methods are not getting the data from DB, instead taking it from session.

How I tried to fix this?
First I thought I’ll try to somehow clear the session as user logs out, but unfortunately there is no option by which I can override or edit the code of logout button under “Wix Member NavBar”. So, I tried an another hack by using the onLogin() to clear the session, as every-time a new user or the same user logs into the system. Session.clear() is not doing its job, even though I’ve put this code in the Site code instead of page code, so no matter where user login their session will be cleared.

How do I know, session.clear() not working?
When I login as a different user in the same browser, I can clearly see the session still holds the previous user’s details instead of current logged in user. With the help of site monitor.

Please show the code that you are using to set and clear session data.

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import { getCurrentClientID } from 'public/helper/CurrentClient.js'
import { session } from 'wix-storage';

$w.onReady(function () {
    wixUsers.onLogin((user) => {
        session.clear();
 let userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
 let isLoggedIn = user.loggedIn; // true
 let userRole = user.role; // "Member"
        getCurrentClientID();
        wixLocation.to("/dynamic-dashboard");
    });
});

Below is one among many other methods in my public folder

export async function getCurrentClientID() {
    console.log("getCurrentClientId triggered");
    console.log("In session: " + session.getItem("ClientDetails"));
 let client = JSON.parse(session.getItem("ClientDetails"));
 if (!isEmpty(client) && client._id !== undefined && client._id.length > 0) {
        console.log("Client ID: " + client._id);
 return client._id;
    } else {
        console.log("Nothing in client details session, loading from DB for getCurrentClientID()");
 let clientDetails = await loadCurrentClientDetails();
 return clientDetails._id;
    }
}


At loadCurrentClientDetails() I’ll load data from DB and then set it to session

Hi Yisreal,

Thank you for your reply, I’m sorry I cannot see you 4th reply. I’ve mistakenly answered my question instead of replying to yours, so I’ve deleted all my answers and then replied to your query. So may in that process, I missed one of your reply. Can you please repeat the last reply again?

How are you saving session data? That is, where are you using setItem() ?