With this solution you will be able to track how many Sessions any given Member of your Website has at the moment, as well as restrict the amount of sessions a Member can have.
1. You’ll need to add a custom field in your Dashboard like this:
→ Make sure you name it “Sessions”. Also, it has to be “activated”/ “set to visible”. It should be a “Number”-Field.
.
.
.
2. Add this code to the events.js File on your Site:
export async function wixMembers_onMemberCreated(event) {
const id = event.entity._id;
console.log("NEW MEMBER CREATED");
const toUpdate = {
contactDetails: {
customFields: {
"custom.sessions": {
value: 1
}
}
}
};
const updatedMember = await members.updateMember(id, toUpdate);
console.log("INIT MEMBER WITH 1: " , updatedMember )
}
.
.
.
3. Create a new .jsw File named “SessionTracking.jsw” and paste this code:
import { currentMember, members } from 'wix-members-backend';
const amountSessionsPerMember = 4; //Here you can limit the Number of concurrent Sessions per User.
export async function CheckMemberSessionCount() {
const member = await currentMember.getMember({ fieldsets: ['FULL'] });
const sessionsValue = member.contactDetails.customFields["custom.sessions"].value;
console.log("Session Value: " + sessionsValue);
if (sessionsValue > amountSessionsPerMember) {
return "NotOk";
} else {
return "Ok";
}
}
export async function UpdateMemberSessionCount() {
try {
const memberObject = await currentMember.getMember({ fieldsets: ['FULL'] });
const id = memberObject._id;
const oldSessionsValue = memberObject.contactDetails.customFields["custom.sessions"].value;
const newSessionsValue = oldSessionsValue + 1;
// Vorbereitung des Update-Objekts
const toUpdate = {
contactDetails: {
customFields: {
"custom.sessions": {
value: newSessionsValue
}
}
}
};
// Mitgliedsdaten aktualisieren
const updatedMember = await members.updateMember(id, toUpdate);
return updatedMember;
} catch (error) {
console.error(error);
throw new Error("Fehler beim Aktualisieren der Mitgliederdaten.");
}
}
export async function UpdateMemberSessionCountSubtract() {
try {
const memberObject = await currentMember.getMember({ fieldsets: ['FULL'] });
const id = memberObject._id;
const oldSessionsValue = memberObject.contactDetails.customFields["custom.sessions"].value;
const newSessionsValue = oldSessionsValue - 1;
// Vorbereitung des Update-Objekts
const toUpdate = {
contactDetails: {
customFields: {
"custom.sessions": {
value: newSessionsValue
}
}
}
};
// Mitgliedsdaten aktualisieren
const updatedMember = await members.updateMember(id, toUpdate);
return updatedMember;
} catch (error) {
console.error(error);
throw new Error("Fehler beim Aktualisieren der Mitgliederdaten.");
}
}
.
.
.
4. In your SITE Code paste this in a onReady function:
import { authentication, currentMember } from 'wix-members-frontend';
import { CheckMemberSessionCount, UpdateMemberSessionCount, UpdateMemberSessionCountSubtract } from 'backend/SessionTracking.jsw';
import { local, session, memory } from 'wix-storage-frontend';
$w.onReady(async function () {
if (session.getItem("loggedInNow") === "true") {
session.removeItem("loggedInNow");
await UpdateMemberSessionCount();
let sessionResponse = await CheckMemberSessionCount();
if (sessionResponse === "NotOk") {
wixWindowFrontend.openLightbox("FullSession");
setTimeout(() => {
authentication.logout();
}, 5000); // Verzögere das Logout um 5 Sekunden
}
} else {
session.removeItem("loggedInNow");
}
authentication.onLogout(() => {
UpdateMemberSessionCountSubtract();
});
});
.
.
.
5. On your Custom Login Page (yes, it has to be a custom login page) paste this code:
$w.onReady( function() {
authentication.onLogin(async (member) => {
session.setItem("loggedInNow", "true");
});
} );
.
.
.
*Thanks to @CODE-NINJA for his inputs regarding this problem Btw. after some rethinking it’s possible to get the result of the problem even with the getMember, instead of querying the Collection.