added upload and update avatar methods for AUthor Provider
This commit is contained in:
@@ -95,7 +95,6 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/** 🔹 Auto-load current user if token exists */
|
/** 🔹 Auto-load current user if token exists */
|
||||||
const fetchCurrentUser = async () => {
|
const fetchCurrentUser = async () => {
|
||||||
if (!token) return;
|
if (!token) return;
|
||||||
@@ -113,6 +112,54 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** --------------------------------------------
|
||||||
|
* 🔹 Upload avatar binary → return URL
|
||||||
|
* -------------------------------------------- */
|
||||||
|
const uploadAvatar = async (file: File): Promise<string | null> => {
|
||||||
|
try {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
|
||||||
|
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 null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** --------------------------------------------
|
||||||
|
* 🔹 Full flow: upload avatar → update profile
|
||||||
|
* -------------------------------------------- */
|
||||||
|
const updateAvatar = async (file: File) => {
|
||||||
|
if (!currentUser) return;
|
||||||
|
|
||||||
|
const url = await uploadAvatar(file);
|
||||||
|
if (!url) {
|
||||||
|
setError("Failed to upload avatar");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the user's avatar
|
||||||
|
const updated = await updateProfile({
|
||||||
|
...currentUser,
|
||||||
|
avatar: url,
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
};
|
||||||
|
|
||||||
/** 🔹 On mount, try to fetch user if token exists */
|
/** 🔹 On mount, try to fetch user if token exists */
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (token) fetchCurrentUser();
|
if (token) fetchCurrentUser();
|
||||||
@@ -131,6 +178,8 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
register,
|
register,
|
||||||
refreshAuthors,
|
refreshAuthors,
|
||||||
updateProfile,
|
updateProfile,
|
||||||
|
uploadAvatar,
|
||||||
|
updateAvatar,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -18,4 +18,6 @@ export interface AuthContextModel {
|
|||||||
logout: () => void;
|
logout: () => void;
|
||||||
refreshAuthors: () => Promise<void>;
|
refreshAuthors: () => Promise<void>;
|
||||||
updateProfile: (user: AuthorModel) => Promise<AuthorModel | void>;
|
updateProfile: (user: AuthorModel) => Promise<AuthorModel | void>;
|
||||||
|
uploadAvatar: (file: File) => Promise<string | null>;
|
||||||
|
updateAvatar: (file: File) => Promise<AuthorModel | undefined>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user