When a new user registers for my site, I would like to capture their email address. I thought I had done this in the past and it has worked perfectly. Now it seems something has changed. Below is 100% of the code I put on a test site with one single blank "members" page.
When the page loads (after successful registration by the user), the "currentUser" is not found. If I simply refresh the screen, the "currentUser" is found. The performance is the same for me on Chrome and Safari (for both "open access" and "incognito"access").
Could this have something to do with Wix multi-stage "onReady" processing? Do I need to find some way to force "onReady" to run twice?
I would be most appreciative if someone could try this very simple snippet and tell me if it is working or if I have missed something obvious.
Thanks!
import wixUsers from 'wix-users';
$w.onReady(async function () {
let user = wixUsers.currentUser;
user.getEmail().then((email) => {
console.log("Found User= " + email); //<-Works on screen refresh only
})
.catch((error) => {
console.log("No User Found error= " + error) //<-Errors out on initial page load
});
});
Thank you for the suggestion. I have done this in the past, but find it frustrating that I have to write this to get around a very simple function that should be working. Anyway...guess I am off to the races in writing a register/login/logout routine.
You can view this tutorial here for making your own members area:
https://support.wix.com/en/article/wix-code-tutorial-building-your-own-members-area
However, please note, that you will still need a custom login lightbox on this page which refreshes the members page after they have logged in through the custom login lightbox, otherwise the page itself won't work as it needs to be refreshed for the now logged in members to see the members only parts and without the page refresh the code on the page won't work as it won't run itself to detect that the user is already logged in.
My custom signup lightbox code, simply take out the page location and replace it with wixLocation.url, like in my login code above and it will simply close the lightbox and refresh the page.
However, my website is setup for manual approve so it takes any new potential members to a signup status page.
You will need to set your members as auto approval otherwise the refresh on the page will not work as the member won't be logged in until they are approved.
import wixUsers from 'wix-users'; import wixWindow from 'wix-window'; import wixLocation from 'wix-location'; $w.onReady(function () { $w("#registerButton").onClick( (event) => { let email = $w("#email").value; let password = $w("#password").value; let first = $w("#firstName").value; let last = $w("#lastName").value; wixUsers.register(email, password, { contactInfo: { "firstName": $w('#firstName').value, "lastName": $w('#lastName').value, } } ) .then( (result) => { let resultStatus = result.status; wixWindow.lightbox.close(); wixLocation.to("/sign-in-status"); //Change the URL ending to whatever page you want to send the user to after they log in. } ); } ); });
Just use a custom signup lightbox and take the wixLocation.to part of the text from my custom login lightbox below and add it to a custom signup lightbox code.
This will basically close the login lightbox and refresh the page which will then let the code work and show all my member only parts of that page, which are normally hidden when any logged out user views the page.
import wixUsers from 'wix-users'; import wixLocation from 'wix-location'; import wixWindow from 'wix-window'; $w.onReady(function () { $w("#forgotPassword").onClick( (event) => { //wixWindow.lightbox.close() wixUsers.promptForgotPassword() .then( ( ) => { // } ) .catch( (err) => { let errorMsg = err; //"The user closed the forgot password dialog" }); }); }); export function loginButton_onclick(event) { let email = $w("#email").value; let password = $w("#password").value; wixUsers.login(email, password) .then( () => { console.log("User is logged in"); wixWindow.lightbox.close(); wixLocation.to(wixLocation.url); //This reloads the same page and allows code to show hidden member parts. } ) .catch( (err) => { console.log(err); $w("#errorMessage").expand(); // You can delete this line if you are not going to add an error message. Use a regular text element set to 'collapse on load' from the Properties Panel. } ); }