// app/account/profile/page.tsx 'use client'; import Link from 'next/link'; import React, { useState } from 'react'; import { useCustomer } from '@/providers/customer'; export default function ProfilePage() { // Access customer data (and possibly other methods: register, login, logout) const { customer, token } = useCustomer(); // For demonstration, we keep track of form states to update first/last name const [firstName, setFirstName] = useState(customer?.first_name ?? ''); const [lastName, setLastName] = useState(customer?.last_name ?? ''); const [message, setMessage] = useState(''); // If the user isn't logged in, `customer` will be undefined if (!customer) { return
Please log in to view your profile.
; } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setMessage(''); try { // Make a PUT request to update the currently logged-in Medusa customer const res = await fetch(`${process.env.NEXT_PUBLIC_STORE_URL}/store/customers/me`, { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, // pass the JWT 'x-publishable-api-key': process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY as string, }, credentials: 'include', body: JSON.stringify({ first_name: firstName, last_name: lastName, }), }); if (!res.ok) { const errorData = await res.json(); throw new Error(errorData?.message || 'Failed to update profile.'); } setMessage('Profile updated successfully!'); // If you'd like to refresh the data in your context, // you could call a function from your provider here (e.g., getCustomer()). // Or simply do another fetch of /customers/me and update context: // await someProviderMethodToRefreshCustomer(); } catch (error: any) { setMessage(`Error: ${error.message}`); } }; return (

Your Profile

Email: {customer.email}

First Name: {customer.first_name}

Last Name: {customer.last_name}


Update Profile

{message &&

{message}

} Manage addresses
); }