following the code to create the private member login page, but here is the problem i want someone to help:
- I want to direct member to registration page for those are 1st time login,
- 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}
);
}