SEE COMMENT FOR SOLUTION!
I have a custom members area that links to a database collection called 'Client' and I have been using a custom login button, which makes use of the promptLogin function for sign up/register - the code [shown below] creates a new item in the Client collection, using the newly registered user's ID.
I have now tried to switch to using the login/register APIs to build custom Login and Register lightboxes, which are much nicer...
[The Login works just fine] BUT when I use the register function on the Register Lightbox it only adds the new user to the Wix members database, but not my Client collection as before - I am missing the wixData.insert code.
How can I merge the two pieces of code below so that I am using the wixUsers.register function, but so upon registration a new item will be created in the Client collection synonymously, i.e. with the same ID like my previous code did!
I am struggling to merge these code, possibly because I am now using the register function in the onReady code, rather than as an export function?
Many thanks in advance :)
using wixUsers.register (new version)
$w.onReady(function () {
$w('#button1').onClick(function () {
let email = $w('#email').value;
let password = $w('#password').value;
wixUsers.register(email, password).then(() => { wixLocation.to('/Client/${wixUsers.currentUser.id}'); })
})
})
using wixUsers.promptLogin (previous working version):
export function signUp_click() { let userId; let userEmail; wixUsers.promptLogin({ "mode": "login" }) .then((user) => { userId = user.id; return user.getEmail(); }) .then((email) => { userEmail = email; return wixData.query("Client") .eq("_id", userId) .find(); }) .then((results) => { if (results.items.length === 0) { const toInsert = { "_id": userId, "email": userEmail }; wixData.insert("Client", toInsert) .catch((err) => { console.log(err); }); } }) .then(() => { wixLocation.to(`/Client/${wixUsers.currentUser.id}`) }) .catch((err) => { console.log(err); }) }
SOLUTION:
import wixUsers from 'wix-users'; import wixData from 'wix-data'; import wixLocation from 'wix-location'; $w.onReady(() => { if (wixUsers.currentUser.loggedIn) { wixLocation.to(`/Client/${wixUsers.currentUser.id}`) } else { $w('#register').onClick(function () { $w('#error').collapse() let email = $w('#email').value; let password = $w('#password').value; wixUsers.register(email, password) .then((result) => { let user = result.user; let userId = user.id; let userEmail = email; const toInsert = { "_id": userId, "email": userEmail }; wixData.insert("Client", toInsert) .then(() => { wixLocation.to(`/Client/Details/${wixUsers.currentUser.id}`) }) .catch((err) => { console.log(err) }) }) .catch((err) => { console.log(err); $w('#error').expand() }) }) } })