@russian-dima Well i tried it and it is sooo close! The registration code works perfectly in preview mode ( it is generating an ID for each member) but it is not working on live mode (it does the registration but it does not insert ID to the members collection). On the login side, i changed the code for a simple one and it is loging in but to the last ID generated, not to the related one.
I know that i am bothering too much, but a little more help and i think this will be solved!
@cryptoirt
I had similar problem. It worked in Preview but not in live. The thing is that while you are in Preview mode, the current user is Admin and do not have any restrictions.
In the doRegistration function or any function that is dealing with contacts and members’ info, you need to use options. Options are declared as following.
PS Kudos to wixsupport that guided me to find the solution that works. The trick was that any function that is related to Contacts and Members needs to have Options to bypass the default (collection) permissions. Needless to say, you need to use these functions in your backend.
@russian-dima Hi!! I am trying to use the RESULTS that you created, but in the login code. I think that it could work generating an ID for the members (and not have restrictions for the live mode).
Code:
$w.onReady(function(){
$w('#loginNow').onClick(async function (){
let email = $w('#email').value;
let password = $w('#password').value;
if ($w('#password').valid) {
let RESULTS = await wixUsers.login(email,password)
console.log("RESULTS: ", RESULTS)
let userID = await RESULTS.user. id; console.log("User-ID: ", userID)
const toInsert = {
"_id": userID,
"email": email,
"password": password,
};
// add the item to the collection
wixData.insert("Members", toInsert)
.then( () => {
const milliseconds = 5 * 1000;
setTimeout( function(){
wixLocation. to("/inicio");
} , milliseconds); // this is to put a gif when login run and to show a message if error.
$w("#loginNow").hide();
$w("#boxLog").expand();
$w("#text38").hide();
})
.catch( (err) => {
console.log(err);
$w("#text38").show();
})
}
})
})
The problem is that this code give the error “Cannot read property ‘user’ of undefined”. Could you help me to solve this, pleaseee?
@venkog Thanks for your response! I am trying to undestand where do i have to put those lines. Should it go in a new backend module or with the DoRegistration function in the register backend module?
Register Module:
import wixUsers from 'wix-users-backend';
import wixData from 'wix-data';
export function doRegistration(email, password, firstName, lastName) {
// register the user
return wixUsers. register(email, password, {
"contactInfo": {
"firstName": firstName,
"lastName": lastName
}
})
.then(async(RESULTS)=> {
await wixUsers. emailUser('verifyRegistration', RESULTS.user. id, {variables:
{approvalToken: RESULTS.approvalToken}})
return RESULTS
});
}
export function doApproval(token) {
// approve the user
return wixUsers.approveByToken(token)
// user is now active, but not logged in
// return the session token to log in the user client-side
.then((sessionToken) => {
return { sessionToken, "approved": true };
})
.catch((error) => {
return { "approved": false, "reason": error };
});
}
Do i have to put something in the register front-end code?
$w.onReady(function(){
$w('#RegButton').onClick(async function(){
let firstName = $w("#nombre").value
let lastName = $w("#apellido").value
let email = $w("#email").value
let password = $w("#password").value
let confirmPassword = $w("#confirmPassword").value
if ($w('#password').value === $w('#confirmPassword').value) {
let RESULTS = await doRegistration(email, password, firstName, lastName)
console.log("RESULTS: ", RESULTS)
let userID = RESULTS.user. id; console. log("User-ID: ", userID)
const toInsert = {
"_id": userID,
"email": email,
"nombre": firstName,
"apellido": lastName,
}
wixData.insert("Members", toInsert)
}
else {
$w("#text40").show()
}
})
})
@cryptoirt
I am not completely sure what you want to achieve - I assume you would need approval process for new members by email.
Here is an example https://www.wix.com/velo/reference/wix-users-backend/approvebytoken. The example demonstrates a common email verification flow. A user is initially registered but not yet approved. At registration, a verification email is sent with a link to a verification page. When a user goes to the verification page, the approval is granted and the user is logged into the site.