Is there a way to allow a member - once logged in - to update a separate collection using their log in ID? i.e. Profile collection and product collection? They would need to be able to update (read/write) both collections once logged in…I’m good on permission understanding.
I also get that the login ID is per collection item and I’m trying to get around that since the signed in member will have multiple items that we want only the member to be able to update. There are a ton of details per item so I do not want to simply add more fields into the “profile” collection…
I know I am missing a query or create/insert in the below code, but this is what i have so far…
Button5 = Login - works
Button7 = Profile- works - connects to " Profile Collection"
Button8 = Offers - broken w “404” code - trying to connect to " Product Collection"
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w(“# button5 “).label = “Logout”;
$w(”# button7 “).show();
$w(”# button8 “).show();
}
else {
$w(”# button5 “).label = “Login”;
$w(”# button7 “).hide();
$w(”# button8 “).hide();
}
} );
export function button5_onclick() {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(”#button5”).label = “Login”;
$w(“#button7”).hide();
$w(“#button8”).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(" **Profile** ")
.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( **"Profile** ", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("# **button5** ").label = "Logout";
$w("# **button7** ").show();
$w("# **button8** ").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function button7_onclick(event) {
wixLocation.to(/ **Profile** /${wixUsers.currentUser.id}
);
}
export function button8_onclick(event) {
wixLocation.to(/newdynamicpage/product/${wixUsers.currentUser.id}
);
}