Новые правки
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 {
|
||||
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">
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ import { marked } from 'marked';
|
|||
|
||||
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;
|
||||
|
||||
if (!slug) {
|
||||
|
|
@ -21,6 +23,29 @@ if (!post) {
|
|||
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 views = await getPostViews(post.id).catch(() => 0);
|
||||
|
||||
|
|
@ -85,7 +110,7 @@ const heroImage = getPostImageUrl(post);
|
|||
|
||||
<!-- Комментарии и похожие статьи -->
|
||||
<div class="comments-wrapper">
|
||||
<Comments postSlug={post.slug} client:load />
|
||||
<Comments postSlug={post.slug} isAuthorized={isAuthorized} client:load />
|
||||
</div>
|
||||
|
||||
<RelatedPosts posts={filteredRelated} currentSlug={slug} />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue