profile and update view for author

This commit is contained in:
2025-11-11 20:47:37 +05:30
parent 89aa1c6ce4
commit 8a29261a3e
4 changed files with 239 additions and 15 deletions

View File

@@ -74,13 +74,40 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
}
};
/** 🔹 Update current user (full model) */
const updateProfile = async (userData: AuthorModel) => {
if (!userData._id) {
console.error('updateProfile called without _id');
return;
}
try {
setLoading(true);
setError(null);
const res = await api.put<AuthorModel>(`/authors/${userData._id}`, userData);
setCurrentUser(res.data);
return res.data;
} catch (err: any) {
console.error('Profile update failed:', err);
setError(err.response?.data?.detail || 'Failed to update profile');
} finally {
setLoading(false);
}
};
/** 🔹 Auto-load current user if token exists */
const fetchCurrentUser = async () => {
if (!token) return;
try {
const res = await api.get<AuthorModel>('/auth/me');
setCurrentUser(res.data);
} catch (err: any) {
const me = await api.get<{ _id: string; username: string; email: string }>('/auth/me');
const author = await api.get<AuthorModel>(`/authors/${me.data._id}`);
const fullUser = { ...me.data, ...author.data };
setCurrentUser(fullUser);
} catch (err) {
console.error('Failed to fetch current user:', err);
logout(); // invalid/expired token
}
@@ -103,6 +130,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
logout,
register,
refreshAuthors,
updateProfile,
}}
>
{children}