Accessing Username on a Dashboard Page

Question:
I have a dashboard page that is used to update a workflow, i want to figure out when someone updates an item, how i can capture their username to enter into the collection being edited. A login instance is not captured when using the dashboard or not one that i can find a way to access.

Product:
Wix Studio Editor Dashboard Page.

What are you trying to achieve:
Capture user name and enter to a collection field when an update is made from a dashboard page

What have you already tried:
I’m a novice at best when it comes to coding, I use AI a lot here is what i have attempted so far with no success.

  1. User Information: The wixUsers API is used to get the logged-in user’s email. If the user is not logged in, Anonymous is used as the value for editBy.
  2. editBy Field: This field is now populated with the email (or user ID if preferred) of the person who clicked the button.
  3. Error Handling: The code handles potential issues with retrieving user details, ensuring functionality for both logged-in and anonymous users.

Now, when the button is clicked, the editBy field will record who performed the action.

When i tried this method everything seems to execute correctly, if i run it in a preview page it can capture any of the information i need, but when run from the dashboard no log in is made so i cant capture the information. This was tested with console logs. heres the log method i used.

  • Console Output: Check the console output to see if currentUser.loggedIn is returning true. If it’s false, it indicates that the user session is not being recognized properly, despite them being on the dashboard.
  • Check for getEmail() errors: The log from the getEmail() function will help understand why the email might not be returned (e.g., permission issues).

I then tried to work around with a backend code version but this ran into multiple errors probably due to my lack of knowledge. AI told me it was my ad blockers fault, but it looks to me like it couldn’t access the reference properly, the code did not show any errors but again I’m not experienced working with backend stuff.

// backend/userhelper.web.js
import { currentUser } from ‘wix-users-backend’;

export function getUserEmail() {
const user = currentUser;
if (user.loggedIn) {
return user.getEmail()
.then(email => {
return email;
})
.catch(error => {
console.error(“Error fetching user email from backend:”, error);
return “Unknown User”; // Fallback for errors
});
} else {
return Promise.resolve(“Guest”); // Return a resolved promise for Guest
}
}

If you are updating a item in a collection on the Dashboard Page, it is possible you can use a Data Hook to intercept the update and capture the userId of the user who is updating the collection field.

You can try using the beforeUpdate() hook to add their userId into the editBy column for the item. The userId will uniquely identify the user who is updating the item.