Новые правки

This commit is contained in:
Web-serfer 2026-05-07 17:50:38 +05:00
parent 37b87e5b71
commit f6c8a49fbf
2 changed files with 33 additions and 44 deletions

View file

@ -10,17 +10,18 @@ interface ToastMessage {
interface CommentsProps {
postSlug: string;
isAuthorized?: boolean;
}
export default function Comments(props: CommentsProps) {
const [isAuthenticated, setIsAuthenticated] = createSignal(false);
const [isAuthenticated, setIsAuthenticated] = createSignal(!!props.isAuthorized);
const [currentUser, setCurrentUser] = createSignal<{
id: string;
name: string;
email: string;
avatar?: string;
} | undefined>(undefined);
const [isLoading, setIsLoading] = createSignal(true);
const [isLoading, setIsLoading] = createSignal(false);
const [comments, setComments] = createSignal<CommentWithReplies[]>([]);
const [replyTo, setReplyTo] = createSignal<string | null>(null);
const [editingComment, setEditingComment] = createSignal<string | null>(null);
@ -33,66 +34,34 @@ export default function Comments(props: CommentsProps) {
};
onMount(async () => {
if (localStorage.getItem("isAuthenticated") === "false") {
console.log("[Comments] localStorage: explicitly unauthenticated");
setIsLoading(false);
if (!props.isAuthorized) {
return;
}
const debugInfo = {
userAgent: navigator.userAgent,
cookieEnabled: navigator.cookieEnabled,
hasPbAuthCookie: document.cookie.includes('pb_auth'),
url: window.location.href,
localStorageAuth: localStorage.getItem("isAuthenticated"),
};
console.log("[Comments] Debug info:", debugInfo);
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const url = new URL("/api/auth/me", window.location.origin);
const response = await fetch(url.toString(), {
const response = await fetch("/api/auth/me", {
method: "GET",
credentials: "include",
signal: controller.signal,
});
clearTimeout(timeoutId);
console.log("[Comments] Auth response status:", response.status);
if (!response.ok) {
localStorage.setItem("isAuthenticated", "false");
console.log("[Comments] Auth response not ok, staying unauthenticated");
setIsLoading(false);
setIsAuthenticated(false);
return;
}
const data = await response.json();
console.log("[Comments] Auth data:", data);
if (data.authenticated && data.user) {
localStorage.setItem("isAuthenticated", "true");
console.log("[Comments] User authenticated:", data.user.name);
setIsAuthenticated(true);
setCurrentUser({
id: data.user.id,
name: data.user.name || "Пользователь",
email: data.user.email,
avatar: data.user.avatar,
});
} else {
localStorage.setItem("isAuthenticated", "false");
console.log("[Comments] User NOT authenticated");
}
} catch (error) {
console.error("[Comments] Ошибка проверки авторизации:", error);
localStorage.setItem("isAuthenticated", "false");
console.error("[Comments] Ошибка получения данных пользователя:", error);
setIsAuthenticated(false);
} finally {
setIsLoading(false);
}
});
@ -330,11 +299,6 @@ export default function Comments(props: CommentsProps) {
)}
</Show>
{/* DEBUG: показать состояние авторизации */}
<div class="debug-auth-info" style="position:fixed;bottom:0;left:0;background:red;color:white;padding:4px 8px;font-size:10px;z-index:9999;">
isAuthenticated: {isAuthenticated().toString()} | isLoading: {isLoading().toString()}
</div>
{isLoading() ? (
<div class="max-w-4xl mx-auto mt-12 pt-8 border-t border-gray-200">
<div class="flex items-center gap-3 mb-8">