Custom Wix Members Login Error

I am making a a custom login page by VELO through wix members. The a user clicks ‘log in’ a lightbox opens and the user is asked their email id and password. I then check if the email id exists in the members database, if not then i want to register them but i am coming across the following error with “authentication.register”: Cannot read properties of undefined (reading ‘status’)

Following is the code im using

// Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
import { currentMember, authentication } from 'wix-members-frontend';
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixWindow from 'wix-window';

$w.onReady(function () {

	// Write your Javascript code here using the Velo framework API

	// Print hello world:
	// console.log("Hello world!");

	// Call functions on page elements, e.g.:
	// $w("#button1").label = "Click me!";

	// Click "Run", or Preview your site, to execute your code

});

/**
*	Adds an event handler that runs when the element is clicked.
	[Read more](https://www.wix.com/corvid/reference/$w.ClickableMixin.html#onClick)
*	 @param {$w.MouseEvent} event
*/
export function loginButton_click(event) {
	let email = $w('#email')
	let password = $w('#password')
	console.log('button clicked')
	if(email.valid && password.valid){
		let emailInput = email.value;
		let passwordInput = password.value;

		wixData.query('Members/PrivateMembersData')
		.eq('loginEmail', emailInput)     
 		.find()       
 		.then((results) => {  
 		if(results.items.length > 0) {
			 console.log('inside if loop')               
 		authentication.login(emailInput, passwordInput)
			.then((result)=> {
				console.log(result)
				console.log('Logged In')
				$w("#errorMessage").show()
			})
			.catch((error) => {
				console.log(error)
			})
		         
 		} else {    
			 console.log('not found in members data')
 		wixData.query('User-data')
		.eq('emailId', emailInput)
		.find()
		.then((res) => {
			if(res.items.length > 0){
				let items = res.items;
				let data_email = items[0].emailId
				console.log(data_email+ ' '+ emailInput)
				if(data_email == emailInput){
					console.log('email matches')
					authentication.register(emailInput,passwordInput)
					.then((results_1)=>{
						console.log(results_1)
						console.log('user registered')
						
						authentication.login(emailInput, passwordInput)
						.then((result2)=> {
							
							wixWindow.lightbox.close()
							console.log(result2)
							console.log('Logged In')

							/*$w("#errorMessage2").show()
							$w("#errorMessage").html = `<center><h6>USER LOGGED IN<h6></center>`*/
						})
						.catch((err) => {
							console.log(err)
						})

					}).catch((reg_err)=>{
						console.log(reg_err)
					})
				}
			}
		})

 		}
	})
}
}

A normal user won’t be able to query Members/PrivateMembersData so this won’t work in general. It will look like it works when you use it as an administrator though. This kind of check would need to be put in a Velo Web Module but it’s still a bit insecure to let users query like this to see if an email is registered.

I’d recommend letting users choose whether they want to login or register and then handling each case separately.

As for the status error there’s no code here that accesses is a status property so I’m not sure what to make of it. Is there other code that is accessing this property?

There is no code that accesses the ‘status’ property.

I’m trying to make the code as such if a user already exists then they’ll just directly login but if they don’t then I’ll first register them and then login. I want to skip the register page as a whole. I’ve made some changes to the code above:

import {authentication} from 'wix-members';
import wixWindow from 'wix-window';
import wixData from 'wix-data';
// Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction

$w.onReady(function () {

});

export function signUpButton_click(event) {
	let email = $w('#email')
	let password = $w('#password') 

	if(!email.valid){
		email.updateValidityIndication();
	}

	if(!password.valid){
		password.updateValidityIndication();
	}

	if(email.valid && password.valid){
		let emailInput = email.value
		let passwordInput = password.value

		wixData.query('Members/FullData')
		.eq('loginEmail', emailInput)
		.find()
		.then((results) => {
			console.log(results)
			if(results.items.length > 0){
				console.log('Length is more than 1')
			
				$w('#text4').show();
				$w('#text6').show();
				console.log(results)
				console.log('Email Found')
				authentication.login(emailInput, passwordInput)
				.then((login_res) => {
					console.log(login_res)
				})
				.catch((error) =>{
					console.log(error)
				})
			}
		})

		if($w('#text4').isVisible == false){
			authentication.register(emailInput, passwordInput)
			.then((register_results) =>{
				console.log("registered")
				$w('#text5').show();
				authentication.login(emailInput, passwordInput)
				.then((login2_results)=>{
					console.log(login2_results)
					$w('#text4').show()
				}).catch((err2)=>{
					console.log(err2)
				})
			}).catch((err1)=>{
				console.log(err1)
			})
		}
	}
}

And the Console is as following:

Cannot read properties of undefined (reading 'status')
Copy 3 of login-2
Line 61

{...}
Copy 3 of login-2
Line 30

Length is more than 1
Copy 3 of login-2
Line 32

{...}
Copy 3 of login-2
Line 36

Email Found
Copy 3 of login-2
Line 37

TypeError: Cannot read properties of undefined (reading 'member')

when I remove the ‘if’ statement where it check if the results length is more than 0, then it works fine with the register and i dont get the ‘status’ error

Please help me as I’m just learning wix velo on the go and trying to make a site out of it

Thank you

Members/FullData is also only able to be read by admin users which is why results.length is always 0 for users who aren’t admins. The docs currently say it’s readable by anyone but this appears to be incorrect so we’ll get that fixed.

The reason most websites don’t combine register/login as the same form is that it opens up a variety of ways for people to determine whether or not an email is being used on the site and this is sensitive user information. If any user can wixdata.query(Members/FullData) then they can read the emails of all members on the site, along with other data.

This would be possible to do with a Velo Web Module but it requires a lot of care to not unintentionally reveal your users email addresses to others.

Thank you for the information

Since I’m making a website for just an event the data isnt really very sensitive. I wanted to make it seem as if the users already were members on the site with the password that I provide them and make use of wix members for the backend. For that reason i thought when they log in for the first time it’ll store the email id and password that i provide them and from the next time onwards they wont be registered again.

Is there a way i can pre define the passwords for users and make use of wix members?
Also is there a way I can copy over ‘Members/FullData’ to another database and sync them so the users can access some dynamic pages too?

Again thank you so much for your responses!

You can register users on the backend using https://www.wix.com/velo/reference/wix-members-backend/authentication/register

Sure. You could handle the onMemberCreated/updated/deleted events and use wix-data to write data to another database. Do keep in mind that you’ll still want to keep user’s PII secure.

Also do note that site members have the option already on whether or not they want to make their profiles public: Site Members: Making a Member Profile Public | Help Center | Wix.com

Happy to help :slight_smile: