// Example function to capture logged-in user's email and update contact
async function updateContactEmail() {
try {
// Assuming Power Pages provides a user context with email after authentication
const userEmail = await getGoogleUserEmail(); // Replace with actual function if available
if (!userEmail) {
console.error("User email not available from Google Authenticator.");
return;
}
// Get the logged-in user's contact ID
const contactId = await getLoggedInContactId(); // Replace with actual function if available
if (contactId) {
// Update the contact's email address
const apiUrl = `/api/data/v9.2/contacts(${contactId})`;
const data = {
emailaddress1: userEmail
};
const response = await fetch(apiUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
'Accept': 'application/json'
},
body: JSON.stringify(data)
});
if (response.ok) {
console.log("Contact email updated successfully.");
} else {
console.error("Failed to update contact email:", response.statusText);
}
} else {
console.error("Contact ID not found.");
}
} catch (error) {
console.error("Error updating contact email:", error);
}
}
// Helper function to retrieve the Google user's email (for demonstration)
async function getGoogleUserEmail() {
// Use actual method to retrieve the authenticated user's email
// Replace this with your authentication provider’s method to fetch user email
return "leocartworldjfyitd@gmail.com"; // Example email
}
// Helper function to retrieve the logged-in user's contact ID (for demonstration)
async function getLoggedInContactId() {
// Use actual method to retrieve contact ID of the authenticated user
return "GUID_OF_CONTACT_RECORD"; // Replace with actual contact ID
}
// Call the function on page load or after authentication
updateContactEmail();