Search code examples
javascriptvelo

How to redirect and send variables of the current logged in correctly?


Hello how are you ? I'm new to javascript and I need help with a question. I'm trying to send data of the current logged in member and redirect a different domain via a button on the website creation www.wix.com. The data are status, full name, email and id. The language used is javascript. I would like to know what I do to be able to send this data together in the url and what I'm doing wrong in the code.

import { currentMember } from 'wix-members';
import wixLocation from 'wix-location';

export function button2_click(event) {
  wixLocation.to("www.page.com" + currentMember.getMember().then((member) => {
      const id = member._id;
      const status = member.status
      const fullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
      return member;
    })
    .catch((error) => {
      console.error(error);
    })
  );
}

Solution

  • After a lot of research on the Wix Velo forum, I discovered the members-wix API, at the moment it is having problems, which results in looking for functions in the users-wix API, but it is deprecated, which hides functions on the site the fleece making access to information difficult. However, after copying a migration function from users-wix to members-wix, along with members-wix authentication, it worked perfectly, I also want to thank Mosh Feu for help https://stackoverflow.com /users/863110/mosh-feu, which helped me understand and improve this code.

    import { currentMember } from 'wix-members';
    import wixLocation from 'wix-location';
    import { authentication } from 'wix-members';
    // NOTE: This example uses the new wix-members.currentMember.getMember()
    // function, which replaces wix-users.currentUser.
    // See the function description for more information, and
    // line 36 for the deprecated example.
    
    // ...
    
    currentMember.getMember('FULL')
        .then((member) => {
            // Replaces currentUser.id
            const id = member._id;
    
            // Replaces currentUser.loggedIn
            const loggedIn = member ? true : false;
    
            // Replaces currentUser.getEmail()
            const loginEmail = member.loginEmail;
            const contactEmails = member.contactDetails.emails;
        })
        .catch((error) => {
            console.error(error);
        });
    
    currentMember.getRoles()
        .then((roles) => {
            // Replaces currentUser.role and currentUser.getRoles()
            return roles;
        })
        .catch((error) => {
            console.error(error);
        });
    
    // NOTE: This example uses the deprecated
    // wix-users.currentUser object.
    
    import wixUsers from 'wix-users';
    
    // ...
    
    let user = wixUsers.currentUser;
    
    let userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
    let isLoggedIn = user.loggedIn; // true
    
    user.getEmail()
        .then((email) => {
            let userEmail = email; // "[email protected]"
        });
    
    user.getRoles()
        .then((roles) => {
            let firstRole = roles[0];
            let roleName = firstRole.name; // "Role Name"
            let roleDescription = firstRole.description; // "Role Description"
        });
    
    user.getPricingPlans()
        .then((pricingPlans) => {
            let firstPlan = pricingPlans[0];
            let planName = firstPlan.name; // "Gold"
            let startDate = firstPlan.startDate; // Wed Aug 29 2018 09:39:41 GMT-0500 (Eastern Standard Time)
            let expirationDate = firstPlan.expiryDate; // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
        });
    export async function button2_click(event) {
        authentication.onLogin(async(member) => {
            const loggedInMember = await member.getMember();
            const memberId = loggedInMember._id;
            console.log(`Member ${memberId} logged in:`, loggedInMember);
        });
    
        const member = await currentMember.getMember();
        const id = member._id;
        const status = member.status;
        const email = member.loginEmail
        const fullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
        const userDetails = JSON.stringify({ id, status, fullName ,email });
    
        wixLocation.to(`https://www.url.com/?user=${encodeURIComponent(userDetails)}`);
    }