Новые правки
This commit is contained in:
parent
37b87e5b71
commit
f6c8a49fbf
2 changed files with 33 additions and 44 deletions
|
|
@ -10,17 +10,18 @@ interface ToastMessage {
|
||||||
|
|
||||||
interface CommentsProps {
|
interface CommentsProps {
|
||||||
postSlug: string;
|
postSlug: string;
|
||||||
|
isAuthorized?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Comments(props: CommentsProps) {
|
export default function Comments(props: CommentsProps) {
|
||||||
const [isAuthenticated, setIsAuthenticated] = createSignal(false);
|
const [isAuthenticated, setIsAuthenticated] = createSignal(!!props.isAuthorized);
|
||||||
const [currentUser, setCurrentUser] = createSignal<{
|
const [currentUser, setCurrentUser] = createSignal<{
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
email: string;
|
email: string;
|
||||||
avatar?: string;
|
avatar?: string;
|
||||||
} | undefined>(undefined);
|
} | undefined>(undefined);
|
||||||
const [isLoading, setIsLoading] = createSignal(true);
|
const [isLoading, setIsLoading] = createSignal(false);
|
||||||
const [comments, setComments] = createSignal<CommentWithReplies[]>([]);
|
const [comments, setComments] = createSignal<CommentWithReplies[]>([]);
|
||||||
const [replyTo, setReplyTo] = createSignal<string | null>(null);
|
const [replyTo, setReplyTo] = createSignal<string | null>(null);
|
||||||
const [editingComment, setEditingComment] = createSignal<string | null>(null);
|
const [editingComment, setEditingComment] = createSignal<string | null>(null);
|
||||||
|
|
@ -33,66 +34,34 @@ export default function Comments(props: CommentsProps) {
|
||||||
};
|
};
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if (localStorage.getItem("isAuthenticated") === "false") {
|
if (!props.isAuthorized) {
|
||||||
console.log("[Comments] localStorage: explicitly unauthenticated");
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
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 {
|
try {
|
||||||
const controller = new AbortController();
|
const response = await fetch("/api/auth/me", {
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
||||||
|
|
||||||
const url = new URL("/api/auth/me", window.location.origin);
|
|
||||||
const response = await fetch(url.toString(), {
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
signal: controller.signal,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
|
|
||||||
console.log("[Comments] Auth response status:", response.status);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
localStorage.setItem("isAuthenticated", "false");
|
setIsAuthenticated(false);
|
||||||
console.log("[Comments] Auth response not ok, staying unauthenticated");
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log("[Comments] Auth data:", data);
|
|
||||||
|
|
||||||
if (data.authenticated && data.user) {
|
if (data.authenticated && data.user) {
|
||||||
localStorage.setItem("isAuthenticated", "true");
|
|
||||||
console.log("[Comments] User authenticated:", data.user.name);
|
|
||||||
setIsAuthenticated(true);
|
|
||||||
setCurrentUser({
|
setCurrentUser({
|
||||||
id: data.user.id,
|
id: data.user.id,
|
||||||
name: data.user.name || "Пользователь",
|
name: data.user.name || "Пользователь",
|
||||||
email: data.user.email,
|
email: data.user.email,
|
||||||
avatar: data.user.avatar,
|
avatar: data.user.avatar,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
localStorage.setItem("isAuthenticated", "false");
|
|
||||||
console.log("[Comments] User NOT authenticated");
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[Comments] Ошибка проверки авторизации:", error);
|
console.error("[Comments] Ошибка получения данных пользователя:", error);
|
||||||
localStorage.setItem("isAuthenticated", "false");
|
|
||||||
setIsAuthenticated(false);
|
setIsAuthenticated(false);
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -330,11 +299,6 @@ export default function Comments(props: CommentsProps) {
|
||||||
)}
|
)}
|
||||||
</Show>
|
</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() ? (
|
{isLoading() ? (
|
||||||
<div class="max-w-4xl mx-auto mt-12 pt-8 border-t border-gray-200">
|
<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">
|
<div class="flex items-center gap-3 mb-8">
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ import { marked } from 'marked';
|
||||||
|
|
||||||
export const prerender = false;
|
export const prerender = false;
|
||||||
|
|
||||||
|
const PB_POCKETBASE_URL = import.meta.env.PB_POCKETBASE_URL || 'http://127.0.0.1:8090';
|
||||||
|
|
||||||
const slug = Astro.params.slug;
|
const slug = Astro.params.slug;
|
||||||
|
|
||||||
if (!slug) {
|
if (!slug) {
|
||||||
|
|
@ -21,6 +23,29 @@ if (!post) {
|
||||||
return Astro.redirect('/blog');
|
return Astro.redirect('/blog');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SSR проверка авторизации
|
||||||
|
let isAuthorized = false;
|
||||||
|
const pbAuthCookie = Astro.cookies.get('pb_auth')?.value;
|
||||||
|
if (pbAuthCookie) {
|
||||||
|
try {
|
||||||
|
const token = pbAuthCookie.trim();
|
||||||
|
const response = await fetch(`${PB_POCKETBASE_URL}/api/collections/users/auth-refresh`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (response.ok) {
|
||||||
|
isAuthorized = true;
|
||||||
|
} else {
|
||||||
|
Astro.cookies.delete('pb_auth', { path: '/' });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[SSR Auth] Error:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const { likes = 0, dislikes = 0 } = await getPostVotesStats(post.id).catch(() => ({ likes: 0, dislikes: 0 }));
|
const { likes = 0, dislikes = 0 } = await getPostVotesStats(post.id).catch(() => ({ likes: 0, dislikes: 0 }));
|
||||||
const views = await getPostViews(post.id).catch(() => 0);
|
const views = await getPostViews(post.id).catch(() => 0);
|
||||||
|
|
||||||
|
|
@ -85,7 +110,7 @@ const heroImage = getPostImageUrl(post);
|
||||||
|
|
||||||
<!-- Комментарии и похожие статьи -->
|
<!-- Комментарии и похожие статьи -->
|
||||||
<div class="comments-wrapper">
|
<div class="comments-wrapper">
|
||||||
<Comments postSlug={post.slug} client:load />
|
<Comments postSlug={post.slug} isAuthorized={isAuthorized} client:load />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<RelatedPosts posts={filteredRelated} currentSlug={slug} />
|
<RelatedPosts posts={filteredRelated} currentSlug={slug} />
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue