Wix-users to Wix-Members

Question:
Can someone help me transition from wix-users to wix-members?

Product:
Wix Editor

What are you trying to achieve:
Can someone please help me change my code so that is goes from wix-users which is depreciated to wix-members ?

Here is my masterpage.js code :

import wixUsers from 'wix-users';
import { registerSession, invalidateOtherSessions } from 'backend/sessionManager';
import wixLocation from 'wix-location';
import { authentication } from 'wix-members-frontend';
import {session, local} from 'wix-storage-frontend';



wixUsers.onLogin((user) => {
    
    registerSession(user.id)
        .then((sessionId) => {
            local.setItem("storedSessionId", sessionId);
            console.log("SessionId Set, running invalidateOtherSessions now")
            invalidateOtherSessions(user.id, sessionId)
        })
    
    wixLocation.to(wixLocation.url);

});


// Handle user logout
authentication.onLogout(() => {
    local.removeItem("storedSessionId");
});

It is part of a code that manages user sessions with wix-storage.

Can someone please help me transition to wix-members. There is a problem with wix-users that prevents the code from working onLogin on mobile devices according to wix support, but i am a beginner and I can’t figure out how to transition. I need the same exact code but with wix-members.

Here you go.

import { registerSession, invalidateOtherSessions } from 'backend/sessionManager';
import wixLocation from 'wix-location';
import { authentication } from 'wix-members-frontend';
import {session, local} from 'wix-storage-frontend';

$w.onReady(function () {

    authentication.onLogin(async(member) => {
      
      const loggedInMember = await member.getMember();
      const memberId = loggedInMember._id;
          
        registerSession(memberId)
            .then((sessionId) => {
                local.setItem("storedSessionId", sessionId);
                console.log("SessionId Set, running invalidateOtherSessions now")
                invalidateOtherSessions(memberId, sessionId)
            })
          
        wixLocation.to(wixLocation.url);
      
    });
      
    // Handle user logout
    authentication.onLogout(() => {
        local.removeItem("storedSessionId");
    });

});

Thank you very much this works perfectly. You are a lifesaver!

1 Like