// public/passwordChange.js
import { authentication, currentMember } from 'wix-members-frontend';
import { changeMemberPasswordAndSync } from 'backend/updateCollections.jsw';
import { isStrongPassword, isNonEmpty } from 'public/validators';
export async function handlePasswordChange(sectionId, inputs, selectors) {
const {
pswrdChngeOldPasswordInput,
pswrdChngeNewPasswordInput,
pswrdChngeConfirmPasswordInput,
} = inputs;
const oldPassword = $w(pswrdChngeOldPasswordInput)?.value?.trim();
const newPassword = $w(pswrdChngeNewPasswordInput)?.value?.trim();
const confirmPassword = $w(pswrdChngeConfirmPasswordInput)?.value?.trim();
const errorEl = $w(selectors.errorText);
const successEl = $w(selectors.successText);
const showMessage = (type, message) => {
const targetEl = type === 'error' ? errorEl : successEl;
const hideEl = type === 'error' ? successEl : errorEl;
if (targetEl) {
targetEl.text = message;
if ('textColor' in targetEl) {
targetEl.textColor = type === 'error' ? '#D32F2F' : '#388E3C';
}
targetEl.visible = true;
if (type === 'error') {
setTimeout(() => {
targetEl.visible = false;
}, 3000);
}
}
if (hideEl) hideEl.visible = false;
};
// ๐งช Validation
if (!isNonEmpty(oldPassword) || !isNonEmpty(newPassword) || !isNonEmpty(confirmPassword)) {
return showMessage('error', 'Please fill in all password fields.');
}
if (newPassword !== confirmPassword) {
return showMessage('error', 'New passwords do not match.');
}
if (!isStrongPassword(newPassword)) {
return showMessage(
'error',
'Password must be at least 8 characters, include a number and an uppercase letter.'
);
}
// ๐ Debug pre-check: who is logged in & what is the old password value
try {
const member = await currentMember.getMember();
console.group('๐ Password Change Debug Info');
console.log('Logged in as:', member?.loginEmail || 'Unknown email');
console.log('Member ID:', member?._id || 'Unknown ID');
console.log('Old password length:', oldPassword?.length ?? 0);
console.log('Old password (JSON view):', JSON.stringify(oldPassword));
console.groupEnd();
} catch (debugErr) {
console.warn('โ Could not fetch current member info before password change:', debugErr);
}
// Step 1: Change password in Wix
try {
await authentication.changePassword(oldPassword, newPassword);
} catch (err) {
const timestamp = new Date().toISOString();
const errorInfo =
typeof err === 'number'
? { code: err, message: null }
: err || {};
console.group('โ Wix Password Change Failed');
console.error('๐ Timestamp:', timestamp);
console.error('๐ Message:', errorInfo.message || 'No message provided');
console.error('๐ข Code:', errorInfo.code ?? 'No code provided');
if (errorInfo.stack) console.error('๐ Stack:', errorInfo.stack);
console.error('๐ฆ Full error object:', err);
console.groupEnd();
if (errorInfo.code === -19972) {
$w(pswrdChngeOldPasswordInput).value = '';
return showMessage('error', 'Your current password is incorrect. Please try again.');
}
return showMessage('error', errorInfo.message || 'Something went wrong. Please try again.');
}
// Step 2: Sync to SellitMembers only if Wix change succeeded
try {
const member = await currentMember.getMember();
const wixMemberId = member._id;
const result = await changeMemberPasswordAndSync(oldPassword, newPassword, wixMemberId);
if (result.success) {
showMessage('success', 'Your password has been changed and synced successfully.');
$w(sectionId)?.collapse?.();
$w(pswrdChngeOldPasswordInput).value = '';
$w(pswrdChngeNewPasswordInput).value = '';
$w(pswrdChngeConfirmPasswordInput).value = '';
} else {
showMessage('error', result.message);
}
} catch (err) {
console.group('โ SellitMembers Sync Failed');
console.error('๐ Timestamp:', new Date().toISOString());
console.error('๐ Message:', err?.message || 'No message provided');
console.error('๐ฆ Full error object:', err);
console.groupEnd();
showMessage('error', 'Password changed in Wix, but syncing failed. Please contact support.');
}
}
trying to reach out to wix help desk but I keep running AI that can attend to my need.
Is there a solution to this?