I am trying to implement a reset password button with the following code:
export function changePassword_click(event) {
wixUsers.promptForgotPassword();
}
Attached to a button which works perfectly in the website preview within the wix editor. However, when used on the live site and using the developers console within chrome, I get the following error:
wixcode-users.js:789 Uncaught (in promise) User is already logged in
I have tried moving the code around, adding the built-in members area (as recommended here: https://www.wix.com/code/home/forum/community-discussion/wixusers-promptforgotpassword-not-working) and nothing has fixed the issue. I really need to get this working. Thanks.
Perhaps because you can only use it before any member has actually logged themselves in, otherwise it will throw up the already logged in error.
Plus you also need to already have a members area setup on your website so that the user already has a email and password stored and so can change it.
You need something like this on your custom login lightbox:
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.
} );
}
Hello @loganrdavis …
If the error shows the person is already logged in, then you must prompt log out. So modify your code to do just that. Depending on the logic of your page / setup you can either add if logged in, then log out … then prompt forgot password … before the prompt forgot password code or simply add it to a separate button trigger … etc … (There are many ways to get creative with it.)
Thank you both for your quick responses. You both nailed it on the head with it requiring the user to not be logged in. I totally missed that since it worked within the editor and in that environment, your always considered logged in. I ended up having the button redirect to a change password page with the following code:
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
$w.onReady( function () {
if (wixUsers.currentUser.loggedIn) { return wixUsers.logout(); }
wixUsers.promptForgotPassword().then(() => {
return wixLocation.to(“/”);
}). catch (() => {
return wixLocation.to(“/”);
});
});
I still wish there was a built in function for this but for now this solution will work. Thank y’all!