upload working for avatar

This commit is contained in:
2025-11-14 23:29:44 +05:30
parent 10aa43fa27
commit bd8aea46b1
2 changed files with 50 additions and 29 deletions

View File

@@ -120,21 +120,21 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const arrayBuffer = await file.arrayBuffer();
const binary = new Uint8Array(arrayBuffer);
const res = await fetch('/media/upload', {
method: 'POST',
headers: {
'Content-Type': file.type,
'Content-Disposition': `attachment; filename="${file.name}"`,
},
body: binary,
});
const res = await api.post(
"/uploads",
binary,
{
headers: {
"Content-Type": file.type,
"Content-Disposition": `attachment; filename="${file.name}"`,
},
}
);
if (!res.ok) throw new Error('Avatar upload failed');
const data = await res.json();
return data.url; // "/media/uploads/uuid.jpg"
} catch (err) {
console.error('Failed to upload avatar:', err);
return res.data.url;
} catch (err: any) {
console.error("Avatar upload failed:", err);
setError(err.response?.data?.detail || "Failed to upload avatar");
return null;
}
};
@@ -146,18 +146,15 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
if (!currentUser) return;
const url = await uploadAvatar(file);
if (!url) {
setError("Failed to upload avatar");
return;
}
if (!url) return;
// Update the user's avatar
const updated = await updateProfile({
// Now update the author document in DB
const updatedUser = await updateProfile({
...currentUser,
avatar: url,
});
return updated;
return updatedUser;
};
/** 🔹 On mount, try to fetch user if token exists */