upload working for avatar
This commit is contained in:
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user