From 7aa8d6073689c10189fb9bed42e83c416a7689f4 Mon Sep 17 00:00:00 2001 From: Web-serfer Date: Thu, 7 May 2026 18:28:02 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/blog/comments/Comments.tsx | 25 +++++++++++++------ frontend/src/lib/pb.ts | 19 ++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/blog/comments/Comments.tsx b/frontend/src/components/blog/comments/Comments.tsx index 21836ad..5e12b68 100644 --- a/frontend/src/components/blog/comments/Comments.tsx +++ b/frontend/src/components/blog/comments/Comments.tsx @@ -14,8 +14,10 @@ interface CommentsProps { } export default function Comments(props: CommentsProps) { + console.log('[Comments] SSR isAuthorized:', props.isAuthorized); const [isAuthenticated, setIsAuthenticated] = createSignal(props.isAuthorized ?? false); - const [currentUser, setCurrentUser] = createSignal<{ + console.log('[Comments] Initial isAuthenticated:', isAuthenticated()); +const [currentUser, setCurrentUser] = createSignal<{ id: string; name: string; email: string; @@ -52,6 +54,7 @@ export default function Comments(props: CommentsProps) { const data = await response.json(); if (data.authenticated && data.user) { + setIsAuthenticated(true); setCurrentUser({ id: data.user.id, name: data.user.name || "Пользователь", @@ -67,6 +70,7 @@ export default function Comments(props: CommentsProps) { const loadComments = async () => { try { + setIsLoading(true); const response = await fetch( `/api/comments?post_slug=${encodeURIComponent(props.postSlug)}&parent=null`, { @@ -101,6 +105,8 @@ export default function Comments(props: CommentsProps) { setComments(commentsWithReplies); } catch (error) { console.error("[Comments] Ошибка загрузки комментариев:", error); + } finally { + setIsLoading(false); } }; @@ -299,6 +305,11 @@ export default function Comments(props: CommentsProps) { )} + {/* DEBUG */} +
+ SSR isAuthorized: {String(props.isAuthorized)} | client isAuth: {String(isAuthenticated())} +
+ {isLoading() ? (
@@ -315,12 +326,12 @@ export default function Comments(props: CommentsProps) { ) : ( }>
-
-

Комментарии

- - {comments().length} - -
+
+

Комментарии

+ + {comments().length} + +
0} diff --git a/frontend/src/lib/pb.ts b/frontend/src/lib/pb.ts index 8f364bc..d898212 100644 --- a/frontend/src/lib/pb.ts +++ b/frontend/src/lib/pb.ts @@ -5,6 +5,25 @@ const PB_URL = import.meta.env.PB_POCKETBASE_URL || 'http://127.0.0.1:8090'; export const pb = new PocketBase(PB_URL); +if (typeof window !== 'undefined') { + const token = localStorage.getItem('auth_token'); + const userStr = localStorage.getItem('user'); + + // Инициализируем куку из localStorage если её нет + if (token && !document.cookie.includes('pb_auth')) { + document.cookie = `pb_auth=${token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`; + } + + if (token && userStr) { + try { + const user = JSON.parse(userStr); + pb.authStore.save(token, user); + } catch (e) { + console.error('Failed to restore auth:', e); + } + } +} + export interface PostVotes { id: string; post_id: string;