Hi! I am trying to link my E-commerce with CMS. I created an extra CMS collection called Users and on a new order I send the email address from Webflow Users to “Users” collection with Logic.
And then I get the current user email with SA5, making an API call to my “Users” collection and if they match I return a slug and append it to my user profile url.
Seems to be working but obviously not secure, and on top of that all other profile pages are not restricted for other users if they “guess the link”
I think I am reinventing a wheel and there must be a simpler way to do this?
Here is my code
<!-- Sygnal Attributes 5 | Memberships -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sygnaltech/webflow-util@5.3.4/dist/css/webflow-membership.css">
<script defer src="https://cdn.jsdelivr.net/gh/sygnaltech/webflow-util@5.3.4/dist/nocode/webflow-membership.js"></script>
<script>
window.sa5 = window.sa5 || [];
window.sa5.push(['getMembershipConfig',
(config) => {
// Apply any configuration settings here
// such as access groups
return config;
}]);
</script>
<script>
window.sa5 = window.sa5 || [];
window.sa5.push(['userInfoChanged', (user) => {
// Access the 'email' property of the 'user' object
var userEmail = user.email;
// Log the email separately
console.log("User Email:", userEmail);
const apiUrl = "https://FIREBASE_URL";
// Function to fetch data iteratively
const fetchData = async (limit, offset) => {
const response = await fetch(`${apiUrl}?limit=${limit}&offset=${offset}`);
return response.json();
};
// Recursive function to fetch all items
const fetchAllData = async (limit, offset = 0) => {
const response = await fetchData(limit, offset);
const { items, pagination } = response;
// Check each item in the response for a match with the logged-in user's email
const matchingItem = items.find(item => item.fieldData.name === userEmail);
if (matchingItem) {
// If a match is found, log the corresponding "slug" field
const globalUserSlug = matchingItem.fieldData.slug;
console.log(`User's slug: ${globalUserSlug}`);
// Update the URL using globalUserSlug
const profileLink = document.getElementById('profileLink');
profileLink.href = `/users/${globalUserSlug}`;
} else {
console.log("User not found in the response.");
}
// If there are more items, fetch the next batch
if (pagination.total > pagination.limit + offset) {
await fetchAllData(limit, offset + limit);
} else {
console.log("All data fetched");
}
};
// Start fetching data
fetchAllData(100); // Assuming your limit is 100 items
}]);
</script>