updateProfile
Update the Shopify profile fields for the currently logged-in customer.
Syntax
window.lantern.api.updateProfile(fields, options);
Description
The updateProfile
method updates customer profile information in both Shopify and Lantern. This is an asynchronous operation that validates and saves the provided customer data.
Parameters
fields
(Object)
An object containing the customer fields to update. All fields are optional.
Property | Type | Description |
---|---|---|
firstName | string | The customer's first name |
lastName | string | The customer's last name |
phone | string | The customer's phone number in E.164 format (e.g., "+12125552368" ) |
birthDate | string | The customer's birth date in ISO-8601 format (e.g., "1990-01-01" ) |
acceptsMarketing | boolean | Whether the customer accepts email marketing communications |
options
(Object)
An object containing additional options for the update operation. All fields are optional.
Property | Type | Default | Description |
---|---|---|---|
refetchData | boolean | false | Whether to refetch and update the customer data from the server after the update |
Return Value
Type: Promise<Object>
The method returns a Promise that resolves to an object with the following structure:
Property | Type | Required | Description |
---|---|---|---|
success | boolean | ✓ | Whether the update operation was successful |
errors | Record<string, string> | Object with field names as keys and error messages as values |
Error Object Structure
The errors
object uses field names as keys and error messages as values:
{
firstName: "First name is required",
phone: "Phone number must be in E.164 format"
}
Examples
Basic Usage
// Update customer's name
const result = await window.lantern.api.updateProfile({
firstName: 'John',
lastName: 'Doe'
});
if (result.success) {
console.log('Profile updated successfully');
} else {
console.error('Update failed:', result.errors);
}
Complete Profile Update with Refetching
// Update multiple fields at once
try {
const result = await window.lantern.api.updateProfile(
{
firstName: 'Jane',
lastName: 'Smith',
phone: '+15551234567',
birthDate: '1990-05-15',
acceptsMarketing: true
},
{ refetchData: true }
);
if (result.success) {
console.log('Profile updated successfully');
} else {
// Handle validation errors
Object.entries(result.errors).forEach(([field, message]) => {
console.error(`Error in ${field}: ${message}`);
});
}
} catch (error) {
console.error('Network or unexpected error:', error);
}
Form Integration
// Example with form handling
async function handleProfileSubmit(formData) {
const fields = {
firstName: formData.get('firstName'),
lastName: formData.get('lastName'),
phone: formData.get('phone'),
acceptsMarketing: formData.get('marketing') === 'on'
};
const result = await window.lantern.api.updateProfile(fields);
if (!result.success) {
// Display field-specific errors to user
Object.entries(result.errors).forEach(([field, message]) => {
showFieldError(field, message);
});
}
return result.success;
}
Error Handling
The method can fail in two ways:
-
Validation Errors: When field values don't meet requirements (e.g., invalid phone format)
- The Promise resolves with
success: false
and anerrors
object - The errors object maps field names to their respective error messages
- The Promise resolves with
-
Network/System Errors: When the request cannot be completed
- The Promise is rejected with an error object
- Handle these with try/catch blocks