How to track user changing login email

According to the documentation at: Building Your Own Members Area

It is recommended that custom member data be kept in a Members database, which is indexed by the wix member id. It shows the creation of the record upon login if it does not exist, and shows storing the login email in the record.

On the My Account members page, a user can change their login email. If they do so, the email in the members record is not longer correct. So how would this record get updated?

It is possible to always sync the members login email upon every login, but that does not solve the situation where they have changed the login and have not logged out.

My site was constructed with the assumptions that the user’s login email is the email to use for contact and primary reference for the user. Thus there are many repercussions to that email changing. It seems the My Account page does not give me anyway of detecting this change so as to compensate for it.

Am I missing something here?

The only solution I can currently see is to cover up the existing UI for making the change with a box containing my own UI and custom code.

Any insights would be appreciated.

Changed emails are handled by Wix so if you don’t store emails in your custom collections it’ll be updated by Wix automatically when changed. But if you also want to update the records in your custom database/collection you can use wix-members events.

When a member data updated an event will be fired:

In this event you can compare what you have in your custom collection and what is the updated email if they are not matching you can update the email in your custom collection with the updated one.

I don’t know if there are any other ways to do this.


If this helps mark this answer as solution, if not let me know.

Thanks for your response.

As for the case of always going to the wix members DB to get the user’s email via the member id, this results in A LOT OF WORK to refactor a lot of code that simply relies on the email address as a reference to the user. I have already written code to modify all those references in the case of a change. That was done after the new function was made available but before I discovered the members page change option.

As for the event function onMemberUpdated(), my investigation shows that when the login is changed, the function is called 9 times which is a little bewildering. To make a call from that context that has to access 5 databases in parallel with what ever the user is currently doing, and have robust exception handling, seems to be fraught with peril.

I have therefore opted to cover up the wix option with my own code to best ensure all goes well and any issues can be readily handled.

Yes some events are fired too many times for some reasons, instead you can also use collection hooks if possible for PrivateMembersData collection so you can grab updated email from there.

You can also build custom my account page to control which info can be updated.

And I think you shouldn’t use members email addresses to reference them instead you should use member ids to reference them.

The data collection hook is not useful here because the update hook is needed but not available for this collection.

According to the documentation at: Building Your Own Members Area

It is recommended that custom member data be kept in a Members database, which is indexed by the wix member id. It shows the creation of the record upon login if it does not exist, and shows storing the login email in the record.

On the My Account members page, a user can change their login email. If they do so, the email in the members record is not longer correct. So how would this record get updated?

It is possible to always sync the members login email upon every login, but that does not solve the situation where they have changed the login and have not logged out.

My site was constructed with the assumptions that the user’s login email is the email to use for contact and primary reference for the user. Thus there are many repercussions to that email changing. It seems the My Account page does not give me anyway of detecting this change so as to compensate for it.

Am I missing something here?

The only solution I can currently see is to cover up the existing UI for making the change with a box containing my own UI and custom code.

Any insights would be appreciated.

I have figured out the best solution for my situation in which I have so many databases populating tables and code relying directly the the users email address.

I discovered the code I had written for admins to change a user’s login, only works for admins, there is no API that allows a user (non admin) to change the loginEmail. Thus the only alternative for a user to change their login is via the wix UI on the My Account member page.

EDIT:

I initially thought that because a login event was triggered by the login email change, the new email could be detected and acted upon. Sadly that solution did not work. The login event that is triggered with the change does not contain the new loginEmail. Go figure. The member record is not updated yet. Thus the change can not be detected within the event handler.

Only when a new page loads does the updated wix member record become available. That can be caught on the masterPage by simply monitoring the login email for every page loaded. This is probably the best solution anyway since it ensures proper synchronization no matter how the login email is changed.

You can then check the loginEmail against your stored member email for a change. If changed, then do a search and replace in all relevant databases. The user does however have to have permission to update those databases.

Additional note:
The onMemberUpdated() event was unusable because of the flurry of 8 events one after the other. The result was that change detection and calling DB update code would happen multiple times, seemingly simultaneously. That was too ugly and scary for me to deal with.

This is my final solution to track loginEmail changes so I can update the many databases I have with the user’s email that populate tables and are used by code relying directly the address.

I discovered the code I had written for admins to change a user’s login, only works for admins, there is no API that allows a user (non admin) to change the loginEmail. Thus the only alternative for a user to change their login is via the wix UI on the My Account member page.

The only reliable way to track the update is with the onLogin() event handler. This should be declared in the onReady function of masterPage.js. The event is called immediately after the user updates their login email. However, the wix member record has not yet been updated at that point, so the new login is not available. The workaround for that issue is to delay a call to code that can read the current member record and take action accordingly.

Thus, I created a syncLoginEmail() function which checks the currentMember loginEmail against the stored member email for a change. If changed, then it does a search and replace in all relevant databases. The user does however have to have permission to update those databases.

I call syncLoginEmail() from the onLogin() event handler after the 500ms delay (using setTimeout()). That delay seems to be adequate to allow the member record to be updated. Also, to ensure proper synchronization no matter how the login email is changed I also call syncLoginEmail() on every page load. I cache the saved user email in local storage to eliminate a database query on every page load.

Additional note:
The onMemberUpdated() event was unusable because of the flurry of 8 events one after the other. The result was that change detection and calling DB update code would happen multiple times, seemingly simultaneously. That was too ugly and scary for me to deal with.