Wix user-api example

Following is the ample for how to use the wix user api from the support information .

import wixUsers from ‘wix-users’;
// …
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true
let userRole = user.role; // “Member”

Can someone explain what the following 3 lines are doing?
All of the wix user api examples have this same example.

user.getEmail()
.then(
(email) => { let userEmail = email; // “user@something.com” }
);

What I want to do is revert instead of update if the email address of the current user is not the same as the email in the record which is in the dynamic page. result -User A can change their own information but they can’t change someone else’s.

I created an onclick event for the update button, and am looking to put the code there.

This is in my png68.online site, but I have another use if I can just get this to work.

Hi Sandy,

bellow is an example of code you could use. Be sure to check the update button is not assigned to Dataset Submit action, as the snippet invokes submit from code.

export function customUpdateButton_click(event) {
  const userDataset = $w('#memberDataset') // Dataset of the page
	
  wixUsers.currentUser.getEmail().then(
    (email) => {
      // Check if field 'email' of currently 
      // displayed item matches user email 
      if (userDataset.getCurrentItem().email === email) {
        userDataset.save() // Save the current dataset item
      } else {
        // Notify user he can't update the data
      }
    })
}

You could also choose to disable the button and other controls if current page is not owned by the user. Do do so, you could some some code to the onReady handler:

wixUsers.currentUser.getEmail()
  .then( (email) => {   
    if (userDataset.getCurrentItem().email !== email) {
        $w('#someControl').disable();
    } 
  })

I hope this helps. Let me know if you need any more details.

This is great, thanks. I tried both parts, am posting my code below.

The second part I thought was working fine. but now I am not sure.
I know this is a basic javascript issue but I am not sure how to assing/read values from the page. I thought I could do, because my search page works, but something is not working here. Now it always disables the update button, even if I am logged on with my email and looking at my record.

Any suggestions, let me know. I am sure I have a curly bracket, close parent or something in the wrong spot. BTW, the two let statement I added later to try to debug, but have to run to a meeting, so hoping you will give me a clue by the time I get back.

Thanks again so much for the response, I think I am close.

Forgot the code -
// Check if logged in user matches email address in page

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady(function () {

const userDataset = $w(‘#dynamicDataset’) ;// Dataset of the page
let email === $w(‘#emailIn’).value;
let $w(‘#textout1’).value === “Msg 1”;

wixUsers.currentUser.getEmail()
.then( (email) => {
if (userDataset.getCurrentItem().email !== email) {
$w(‘#update’).disable();
}
});
});

export function update_click(event) {

// …
const userDataset = $w(‘#dynamicDataset’) ;

wixUsers.currentUser.getEmail().then(
(email) => {
// Check if field ‘email’ of currently
// displayed item matches user email
if (userDataset.getCurrentItem().email === email) {
userDataset.save() ;// Save the current dataset item

  } else { 
    // Notify user he can't update the data 
  } 
}); 

}

1 Like

Thanks Giedries, I found my bug, had email element wrong/different from my collection.

Anyway fixed it and it works, both ways!! I decided disableing the button was best and easiest.

Thanks again.

1 Like

I’m glad you got it working. Good luck!