diff --git a/src/blog/providers/Article.tsx b/src/blog/providers/Article.tsx index 9a676a5..1f7dce5 100644 --- a/src/blog/providers/Article.tsx +++ b/src/blog/providers/Article.tsx @@ -10,15 +10,25 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child const [articles, setArticles] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const { token } = useAuth(); + const { token, currentUser } = useAuth(); /** 🔹 Author IDs must be strings for API, so we normalize here */ - const normalizeArticleForApi = (article: Partial) => ({ - ...article, - authors: (article.authors ?? []).map(a => - a._id - ), - }); + const normalizeArticleForApi = (article: Partial) => { + // Extract existing authors as a list of IDs (string[]) + const existingIds = (article.authors ?? []).map(a => + typeof a === "string" ? a : a._id + ); + + // Inject currentUser if missing + const allAuthorIds = currentUser?._id + ? Array.from(new Set([...existingIds, currentUser._id])) // dedupe + : existingIds; + + return { + ...article, + authors: allAuthorIds, + }; + }; /** 🔹 Fetch articles (JWT automatically attached by api.ts interceptor) */ const fetchArticles = async () => {