Getting Current userEmail for Profile Page

Hi,

I’m attempting to create a profile page for members of my club to be able to view and update their information.

Facts about my site:

  • I just migrated our club site over to Wix about 6 months ago.
  • We currently have a database (MembershipDatabase) of active and non-active club members. I have set up an import/export page using this API Velo Tutorial: Importing and Exporting Collection Data with Code | Help Center | Wix.com (this works currently)
  • I have created a new form on a new page for new club members to complete to join the club and it is linked to the MembershipDatabase. (this also works as expected)
  • I have a separate log in page I created using the Create Member Profile Pages article above.

My issue is with the code below are that the userId that created the data in the Database will all be the Id of the person that performed the upload, so club member data will not be able to be linked to current user Id, and needs to be linked to current user email.

export function profileButton_click(event) { wixLocation.to(/MembershipDatabse/${wixUsers.currentUser.email}); }
But the link is still pulling up currentUser.id.

Also in the example, I do not want items to be added to the database if email does not exist. The profile page should only show for people that are in the database.

I am testing this in the published site.

Please let me know if you have any questions. I look forward to everyone’s assistance!

Thanks,
Tracy

Hey
You cant reference email that way. Try this.

export function profileButton_click(event) {
let user = wixUsers.currentUser;
user.getEmail() .then( (email) => {
let userEmail = email; // “user@something.com
} );
wixLocation.to (/MembershipDatabse/ + userEmail );
}

Thanks Andreas, unfortunately that didn’t work either.
With that exact code, I got an error that userEmail was undefined.

I moved the last line of code up to reflect this:

export function profileButton_click() {
let user = wixUsers.currentUser;
user.getEmail() .then( (email) => {
let userEmail = email; // “user@something.com
wixLocation.to(/MembershipDatabse/ + userEmail);
}
);
}

It did remove the error, but when I publish, log in as my club member account to match data in my database, and click My Profile nothing occurs now.

Do you have any other ideas?

Thanks!

What do you get if you console.log(user); when running the page?

Hi Tracy,
Can you please share a link to your site? I’ll try to take a look.

Liran.

https://www.stlmini.com/log-in

Thank you for taking a look!

Ok, I think I got it :slight_smile:
You are actually not using ‘wixUsers’ at all. ‘wixUsers’ is a module used for integrating with Wix built in LOGIN/SIGNUP button.
In your case, you’re having your own registration system, which means that a user of your site is not considered as a ‘wixUser’ .
So in your case, the code you’re looking for should search your database for the relevant email and pull it out.
Now, the issue is that you need to know which user is logged in to your site.
I would consider moving to Wix built in LOGIN/SIGNUP as it will have all the functionality you want in terms of authentication. You can find it at the ADD Panel, or just use the ’ promptLogin() ’ function in code.
Otherwise, have a look here at a brief discussion I had earlier in the forum regarding building your own login system.

Liran.

So the users currently do log in using the integrated wixUser log in on a different page, I was just trying to follow this example https://support.wix.com/en/article/how-to-create-member-profile-pages-with-wix-code and the first step it to create a dedicated log in page. I’m pretty sure I’m using wix as the registration system, I wouldn’t know how to build my own! :slight_smile:

Before I tried building the page, users would log in here: stlminiclub

Here’s information from the tutorial I was following:

Login Page

In our example we have a dedicated page for members to use for logging into the site. However, you can add the contents of this page to any page or even show them on all pages .

Note: Because we have a dedicated login page, we’ll be adding the code below to the Page tab of the code panel. But if you’re showing the elements on all pages, you need to add the code below to the Site tab of the code panel.

The design of our login page is very simple. It contains two buttons. The first button is used for either logging in or logging out, depending on whether the current user is already logged in or not. The second button is used to navigate to a member’s profile page. It is only shown for users who are already logged in.

i also just realized you may be seeing the text box in the screen that was just there hoping i could imput yhe current user email in there to make sure the getEmail code was working…sorry for the confusion

Oh I did find the code where it prompts log in like expected.

I’d attach a picture, but for some reason I cannot upload any images to this forum, my site, or my forum. (totally separate issue, I know)

Thanks so much for your help!

export function loginButton_click() { 
  // user is logged in
  if(wixUsers.currentUser.loggedIn) {
    // log the user out
    wixUsers.logout()
      .then( () => {
        // update buttons accordingly
        $w("#loginButton").label = "Login";
        $w("#profileButton").hide();
    } );
  }
  // user is logged out
  else {
    let userId;
    let userEmail;
  
    // prompt the user to log in 
    wixUsers.promptLogin( {"mode": "login"} );

        // update buttons accordingly
        $w("#loginButton").label = "Logout";
        $w("#profileButton").show();
      } 

      } 



export function profileButton_click() {
let user = wixUsers.currentUser;
user.getEmail() .then( (email) => { 
    let userEmail = email; // "user@something.com" 
    wixLocation.to(`/MembershipDatabse/` + userEmail); 

$w('#profileButton').onClick(function() {
  console.log('#someButton was clicked');
  // do something
});

});
 
} 

Hi Tracy, I can see you’re making some changes on your site.
If you still encounter issues on Sunday, let me know and I’ll try to further investigate.

Liran.

BTW, Just got this video by Nayeli about how to make a user profile page:

Maybe it’ll help :slight_smile:
Liran.

Hi Liran!

I was finally able to get that text box to populate current user email on the profileButton_click, I was just using the text box to make sure it was pulling the correct user. I tested with my person club member email login and my admin login, so that’s a step in the right process.

Thanks for the video, I checked it out and she follows the steps in the tutorial article which has steps that do not apply to my situation.

  • my database will be populated based on an import, userIDs for each entry will be the same and not linked to user individually.

This is the code that I’m having issues with now, I need it to link to the dynamic page using currentuser.userEmail:

export function profileButton_click(event) {
wixLocation.to(/MembershipDatabase/${wixUsers.currentUser.id});
}

Any suggestions would be greatly appreciated!
Thanks

Oh my…I got it!! Here’s the code that I create the dedicated login page based linked to a database populated with imported data vs. user created data.

I also had to change my database permissions to Site Member (not Site Member Author as the article suggests) to get this to work correctly.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady( () => {
  if(wixUsers.currentUser.loggedIn) {
    $w("#loginButton").label = "Logout";
    $w("#profileButton").show();
  }
  else {
    $w("#loginButton").label = "Login";
    $w("#profileButton").hide();
  }
} );

export function loginButton_click() { 
  // user is logged in
  if(wixUsers.currentUser.loggedIn) {
    // log the user out
    wixUsers.logout()
      .then( () => {
        // update buttons accordingly
        $w("#loginButton").label = "Login";
        $w("#profileButton").hide();
    } );
  }
  // user is logged out
  else {
    let userId;
    let userEmail;
  
    // prompt the user to log in 
    wixUsers.promptLogin( {"mode": "login"} )
      .then( (user) => {
        userId = user.id;
        return user.getEmail();
      } );
        // update buttons accordingly
        $w("#loginButton").label = "Logout";
        $w("#profileButton").show();
  }
}

export function profileButton_click() {

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true
let userRole = user.role;       // "Member"

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
    wixLocation.to("http://www.stlmini.com/MembershipDatabase/"+userEmail);
  } );

}

I’m doing my happy dance!

Video unavailable