profile and update view for author
This commit is contained in:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user