Question:
I want to set up custom sign up form using code (velo) for my site for which I am using register API to register by approaval token but I am not being able to send verification email at all using triggeredemails (emailcontact()) api to the user. It is getting stuck at - "[“Registration failed:”,"Contact does not have valid email for site “]”. How do I go past it?
Product:
WIX Editor - Velo
What are you trying to achieve:
I need to have custom sign up for my website that gets triggered only for users who meet certain criteria.
What have you already tried:
I have also tried ensuring email is in proper format, separately creating a contact for the user, query and send the email for the user.
Here’s my backend code (register.web.js):
import { Permissions, webMethod } from ‘wix-web-module’;
import { authentication } from ‘wix-members-backend’;
import { triggeredEmails } from ‘wix-crm-backend’;
export const doRegistration = webMethod(Permissions.Anyone, async (email, password, firstName, lastName) => {
// Convert email to lowercase to ensure case-insensitive handling
const emailLowercase = email.toLowerCase();
const registrationOptions = {
contactInfo: {
firstName: firstName,
lastName: lastName,
emails: [emailLowercase] // Use the lowercase email
}
};
try {
const registration = await authentication.register(emailLowercase, password, registrationOptions);
console.log('Member registration attempt:', registration);
if (registration.status.toLowerCase() === "pending" && registration.approvalToken) {
const emailOptions = {
variables: {
name: firstName,
verifyLink: `https://www.firstcontact.lgbt/post-registration?token=${registration.approvalToken}`
}
};
await triggeredEmails.emailContact('verifyRegistration', registration.member.contactId, emailOptions);
console.log('Verification email sent using triggeredEmails.');
return { success: true, message: "Registration successful, confirmation email sent." };
} else {
return { success: false, message: "Registration successful but did not enter approval flow." };
}
} catch (error) {
console.error('Registration failed:', error);
return { success: false, message: `Registration failed due to error: ${error.message}` };
}
});
export const doApproval = webMethod(Permissions.Anyone, async (token) => {
try {
const sessionToken = await authentication.approveByToken(token);
console.log(‘Member approved with session token:’, sessionToken);
return { approved: true, sessionToken: sessionToken };
} catch (error) {
console.error(‘Approval failed:’, error);
return { approved: false, reason: Approval failed due to error: ${error.message}
};
}
});