The basic login page would be like this:
First you import the API needed for this to work, in this case, wixData and wixLocation .
import wixData from "wix-data"
import wixLocation from "wix-location"
Then you need to call a method called $w.onReady() that executes commands related to the elements you have in your page, as soon as the page is ready.
$w.onReady(() => {
//Every code related to elements have to be put here
})
Then you need to create a event listener on the button you created, in the image you called it button1 and labeled it “Enviar”.
$w.onReady(() => {
$w('#button1').onClick(async() => {
//This is listening for clicks
})
})
Now, you can put the code directly inside this method that, but is best practice to create an async function to do it properly. This functions is called checkLogin() and it receives 2 parameters, email and contrasena.
async function checkLogin(email, contrasena) {
}
To compare the login credentials you will have to do an asynchronous query to the dataset logingers.
async function checkLogin(email, contrasena) {
const query = await wixData
.query("logingers")
.eq("email", email)
.eq("contrasena", contrasena)
.find()
if (query.items.length > 0) {
wixLocation.to("/homepage") //Change this to the URL you want to redirect
} else {
console.error("Email/Contrasena wrong!")
}
}
You have also to store the email and contrasena that were type inside variables to feed the function, like this:
$w.onReady(() => {
$w("#button1").onClick(async () => {
let email = $w("#email").value
let contrasena = $w("#contrasena").value
await checkLogin(email, contrasena)
})
})
Your whole code should be something like this:
import wixData from "wix-data"
import wixLocation from "wix-location"
$w.onReady(() => {
$w("#button1").onClick(async () => {
let email = $w("#email").value
let contrasena = $w("#contrasena").value
await checkLogin(email, contrasena)
})
})
async function checkLogin(email, contrasena) {
const query = await wixData
.query("logingers")
.eq("email", email)
.eq("contrasena", contrasena)
.find()
if (query.items.length > 0) {
wixLocation.to("/homepage") //Change this to the URL you want to redirect
} else {
console.error("Email/Contrasena wrong!")
}
}
Try it and let me know!