Hi, i successfully created a registration lightbox that writes to my custom userProfiles database, but i don't know how to set up a login lightbox that inputs email and passwords (both fields stored in the said database for each user) and checks if they exists, allowing login... what i want the lightbox to do is something like:
if email in input form is present in the field email of a record in userProfiles database, AND input password is present in the same record of the userProfile database, then switch to page XXXX
Any ideas?
Thank you in advance
In the Register page ->
import wixData from 'wix-data'; import wixUsers from 'wix-users'; $w.onReady( function() { $w("#submit").onClick( (event) => { // submit button let user = wixUsers.currentUser; let userId = user.id; // the below code check if email is already taken or not wixData.query("userProfiles") .eq("email", $w("#emailInput").value) // email field and input .find() .then( (results) => { if(results.items.length > 0) { console.log("email already taken"); } // the below code inserts the email, password and userId and set the no: of login to 0 else { let toInsert = { "email": $w("#emailInput").value, // userName input "password": $w('#passwordInput").value, // password input "userId": userId, "numberOfLogins" : "0"
}; wixData.insert("userProfiles", toInsert) .then( (results) => { let item = results; console.log("User registered");
} ) } } ) } ); } );
In the Login page ->
look at the orange
wixData.query("userProfiles") .eq("email", $w('#emailInput').value) .find() .then( (results) => { if(results.items.length > 0) { let items = results.items[0]; let password = items.FieldNameHere; // password field //here (below) we are adding code let totallogins = items.numberOfLogins; //total login field if (password === $w('#passwordInput').value) { // password //here (below) we are adding code items.numberOfLogins = Number(items) + Number(1); wixData.update('userProfiles', items) .then((resuls1) => { console.log("Logged In"); wixLocation.to(`/XXXX`); } ); }else { console.log("Password mismatch"); } } else { console.log("Please register"); wix.location.to(`/regitserpage`); // register page } } ) }
Is it possible to redirect the login to a second website?
Reference ->
https://www.wix.com/corvid/reference/wix-data/wixdataquery/eq
https://www.wix.com/corvid/forum/community-discussion/typeerror-error-capturestacktrace-is-not-a-function?origin=member_posts_page
https://www.wix.com/corvid/forum/main/comment/5c3e6e5d046e180142bd863b
https://www.wix.com/corvid/reference/wix-data/insert
Thank you Ajith now it seems to work but i have a problem...
can I steal more of your time to solve a problem in the userProfile database?
I have a field called numberOfLogins as a number to store the number of logins of the user...
at first login it must be 0, but i don't know how to set the 0 value. If i enter 0 manually in the database, at next logins it is increased via code, but if a user has never logged in, how can i set the field value to 0 automatically after a user is created?
I have tried the following (dataset1 is a read/write dataset):
wixData.query("userProfiles").find().then((result)=>{ let $item = $w.at(event.context); let logins = $item("#dataset1").getCurrentItem(); //to get the current user logins.numberOfLogins="0"; wixData.update("userPprofiles", logins).then(()=>{ $w("#dataset1").refresh()} ); });
but it does not work.
Hi Alessandro Demontis ,
After logging in , if you are trying to redirect user to a dynamic page, then ->
You can do the above in two methods
let items = results.items; let password = items[0].password; let loggedID = items[0].id; let link = `${loggedID }` wixLocation.to(`/userprofile/${link}`);
If this doesn't work do this ->
1) Go to your Collection
2) Get the dynamic page link field's id
3) Copy the field key (only) and
let items = results.items; let password = items[0].password; let loggedID = items[0].id; let id = results.items[0]['link-userprofiledata-all']; // copied field let link = `${id}` console.log("link " + id); wixLocation.to(`${id}`);
For more reference, you can check on my post ->
https://www.wix.com/corvid/forum/community-discussion/linking-a-button-to-dynamic-page-through-code?origin=member_posts_page
Ajith
Something like this ->
export function submit_click(event) { wixData.query("userProfiles") .eq("email", $w('#emailInput').value) // email .find() .then( (results) => { if(results.items.length > 0) { let items = results.items; let password = items[0].FieldNameHere; // password field if (password === $w('#passwordInput').value) { // password console.log("Logged In"); wixLocation.to(`/XXXX`); }else { console.log("Password mismatch"); } } else { console.log("No email found"); } } ) }
Best regards,
Ajith !