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
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 !
Thanks Ajit but the code above only works if i am logged in with Wix login system… moreover if i try to redirect to: currentUser.ID it moves me to the ID of the owner, not the ID correstonding to the email in the database.
I have tried changing the code tothis but it moves me to ‘undefined’ page:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
export function loginButton_click(event) {
wixData.query("userProfile")
.eq("email", $w('#email').value) // email field in the database
.find()
.then( (results) => {
if(results.items.length > 0) {
let items = results.items;
let password = items[0].password; // password field
let loggedID = items[0].id; // id field
if (password === $w('#password').value) { // password
console.log("Logged In");
wixLocation.to("/userprofile/"+ loggedID); // the dynamic page for the user with ID from the database
}else {
console.log("Password mismatch");
}
} else {
console.log("No email found");
}
} )
}
What i would like to achieve is that external users that don’t have a wix account can register and login to my website.
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 →
- Go to your Collection
- Get the dynamic page link field’s id
- 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
@ademontis
Can you specify your query ???
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.
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?
The code above seems to create the same userId for every member who registers. Is there a way to generate a unique userId ?
@mattmfredericks Please add your own issue into a new post instead of bumping up an old post. Explain what you are trying to do, what works, and what doesn’t. Also, add any code in a code block as stated in the Forum Guidelines .
This is an old post and is being closed.