Josie Ning
Josie Ning

Reputation: 1

wixLocation.to() - how to transfer to a dynamic page by using wixLoation.to()

I have a login page and I want to check the username and password in a database called ProfileInfo ( there are only two columns in this database- username and password), and if it is matched, the page will shift to a dynamic Profile page.

I have two questions

  1. How to query from the database and check the username and password?
  2. How to use wixLocation.to() to shift to the dynamic page?

screenshot

Upvotes: 0

Views: 3269

Answers (1)

Sam
Sam

Reputation: 1109

First off, this is not a very secure way to deal with user logins. You might want to consider the built-in Wix members functionality instead.

That being said, you definitely want to make sure your ProfileInfo collection is set with very restrictive permissions. Also, you'll want to query and check the passwords in the backend.

So, in some backend web module (here I will assume it is authenticate.jsw), you should write a function similar to this:

import wixData from 'wix-data';

export function authenticate(username, password) {
  return wixData.query("ProfileInfo")
    .eq("username", username)
    .find({"supressAuth": true})
    .then( (results) => {
      if(results.items.length > 0) {
        return password === results.items[0].password;
      }
    } );
}

Then, from your page code you can call it like this. It's impossible to say exactly what the wixLocation.to() should contain without knowing how you set up your dynamic page. Here I assume you set it up with the prefix Profile and it is based on the username.

import {authenticate} from 'backend/authenticate';
import wixLocation from 'wix-location';

export function button_click() {
  authenticate($w("#username").text, $w("#password").text)
    .then( (authenticated) => {
      if(authenticated) {
        wixLocation.to(`/Profile/${$w("#username")}`)
      }
    } );
}

Upvotes: 1

Related Questions