private member login problem

following the code to create the private member login page, but here is the problem i want someone to help:

  1. I want to direct member to registration page for those are 1st time login,
  2. And those with already create the profile, and I want them to direct to their profile page after each time login

Here is the coding I’m writing
login page name: wixreg
registration page: testing
Dynamic profile page: Agentprofile(ID)

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w(“#button30”).label = “Logout”;
$w(“#button31”).show();
}
else {
$w(“#button30”).label = “Login”;
$w(“#button31”).hide();
}
} );

export function button30_onclick() {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#button30”).label = “Login”;
$w(“#button31”).hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;

// prompt the user to log in  
wixUsers.promptLogin( {"mode": "login"} ) 
  .then( (user) => { 
    userId = user.id; 
    return user.getEmail(); 
  } ) 
  .then( (email) => { 
    // check if there is an item for the user in the collection 
    userEmail = email; 
    return wixData.query("agentprofile") 
      .eq("_id", userId) 
      .find(); 
  } ) 
  .then( (results) => { 
    // if an item for the user is not found 
    if (results.items.length === 0) { 
      // create an item 
      const toInsert = { 
        "_id": userId, 
        "email": userEmail 
      }; 
      // add the item to the collection 
      wixData.insert("agentprofile", toInsert) 
        .catch( (err) => { 
          console.log(err); 
        } ); 
    } 
    // update buttons accordingly 
    $w("#button30").label = "Logout"; 
    $w("#button31").show(); 
  } ) 
  .catch( (err) => { 
    console.log(err); 
  } ); 

}
}

export function button31_onclick() {
wixLocation.to(/agentprofile/${wixUsers.currentUser.id});
}

Hi,

See my changes and comments inline.
If you plan to navigate to a page right after a user is logged in, some code here is probably unnecessary and can be removed.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(() => {
	if (wixUsers.currentUser.loggedIn) {
		$w("#button30").label = "Logout";
		$w("#button31").show();
	} else {
		$w("#button30").label = "Login";
		$w("#button31").hide();
	}
});

export function button30_onclick() {
	// user is logged in
	if (wixUsers.currentUser.loggedIn) {
		// log the user out
		wixUsers.logout()
			.then(() => {
				// update buttons accordingly
				$w("#button30").label = "Login";
				$w("#button31").hide();
			});
	}
	// user is logged out
	else {
		let userId;
		let userEmail;

		// prompt the user to log in 
		wixUsers.promptLogin({
				"mode": "login"
			})
			.then((user) => {
				userId = user.id;
				return user.getEmail();
			})
			.then((email) => {
				// check if there is an item for the user in the collection
				userEmail = email;
				return wixData.query("agentprofile")
					.eq("_id", userId)
					.find();
			})
			.then((results) => {
				// if an item for the user is not found
				if (results.items.length === 0) {
					// create an item
					const toInsert = {
						"_id": userId,
						"email": userEmail
					};
					// add the item to the collection
					wixData.insert("agentprofile", toInsert)
						.catch((err) => {
							console.log(err);
						});
					//navigate to registration page
					wixLocation.to(`/testing/`);
				}
				else {
					//navigate to profile page
					wixLocation.to(`/agentprofile/${wixUsers.currentUser.id}`);
				}
				
			})
			.catch((err) => {
				console.log(err);
			});
	}
}

export function button31_onclick() {
	wixLocation.to(`/agentprofile/${wixUsers.currentUser.id}`);
}

Hi,
copy and paste the coding you modified, sounds not working for new member login, and pending on this page. Please check and help. Thanks!

Hi,

Do you have any errors in the console?
Test it on the live version of your site after you publish. Make sure to open developer tools > select the console tab. Use Ctrl + Shift + i or Cmd + Option + i to open dev tools

Hi Ido,
Please comment the vedio, thanks!

Hi Waksman,

Change wixLocation.to(/testing/); to wixLocation.to(/testing);
Make sure to save, publish and test the flow on your published site.