Новые правки
This commit is contained in:
parent
7aa8d60736
commit
b298e69f7b
4 changed files with 21 additions and 34 deletions
|
|
@ -14,10 +14,8 @@ interface CommentsProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Comments(props: CommentsProps) {
|
export default function Comments(props: CommentsProps) {
|
||||||
console.log('[Comments] SSR isAuthorized:', props.isAuthorized);
|
|
||||||
const [isAuthenticated, setIsAuthenticated] = createSignal(props.isAuthorized ?? false);
|
const [isAuthenticated, setIsAuthenticated] = createSignal(props.isAuthorized ?? false);
|
||||||
console.log('[Comments] Initial isAuthenticated:', isAuthenticated());
|
const [currentUser, setCurrentUser] = createSignal<{
|
||||||
const [currentUser, setCurrentUser] = createSignal<{
|
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
email: string;
|
email: string;
|
||||||
|
|
@ -305,11 +303,6 @@ const [currentUser, setCurrentUser] = createSignal<{
|
||||||
)}
|
)}
|
||||||
</Show>
|
</Show>
|
||||||
|
|
||||||
{/* DEBUG */}
|
|
||||||
<div style="background:#333;color:white;padding:5px;font-size:10px;">
|
|
||||||
SSR isAuthorized: {String(props.isAuthorized)} | client isAuth: {String(isAuthenticated())}
|
|
||||||
</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">
|
||||||
|
|
|
||||||
|
|
@ -5,25 +5,6 @@ const PB_URL = import.meta.env.PB_POCKETBASE_URL || 'http://127.0.0.1:8090';
|
||||||
|
|
||||||
export const pb = new PocketBase(PB_URL);
|
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 {
|
export interface PostVotes {
|
||||||
id: string;
|
id: string;
|
||||||
post_id: string;
|
post_id: string;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,19 @@
|
||||||
import type { APIRoute } from 'astro';
|
import type { APIRoute } from 'astro';
|
||||||
|
|
||||||
export const POST: APIRoute = async ({ cookies }) => {
|
export const POST: APIRoute = async ({ cookies }) => {
|
||||||
|
// Удаляем куку на сервере
|
||||||
cookies.delete('pb_auth', { path: '/' });
|
cookies.delete('pb_auth', { path: '/' });
|
||||||
|
|
||||||
|
// Возвращаем заголовок для удаления куки на клиенте
|
||||||
return new Response(JSON.stringify({
|
return new Response(JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Вы успешно вышли из аккаунта'
|
message: 'Вы успешно вышли из аккаунта'
|
||||||
}), { status: 200 });
|
}), {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
// Принудительно удаляем куку на клиенте
|
||||||
|
'Set-Cookie': 'pb_auth=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT',
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -23,15 +23,17 @@ if (!post) {
|
||||||
return Astro.redirect('/blog');
|
return Astro.redirect('/blog');
|
||||||
}
|
}
|
||||||
|
|
||||||
// SSR проверка авторизации
|
// SSR проверка авторизации - используем реальную проверку через API
|
||||||
let isAuthorized = false;
|
let isAuthorized = false;
|
||||||
const pbAuthCookie = Astro.cookies.get('pb_auth')?.value;
|
const pbAuthCookie = Astro.cookies.get('pb_auth')?.value;
|
||||||
|
|
||||||
|
console.log('[SSR] Cookie exists:', !!pbAuthCookie);
|
||||||
|
|
||||||
if (pbAuthCookie && PB_POCKETBASE_URL && PB_POCKETBASE_URL.startsWith('http')) {
|
if (pbAuthCookie && PB_POCKETBASE_URL && PB_POCKETBASE_URL.startsWith('http')) {
|
||||||
try {
|
try {
|
||||||
const token = pbAuthCookie.trim();
|
const token = pbAuthCookie.trim();
|
||||||
const controller = new AbortController();
|
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
console.log('[SSR] Token preview:', token.substring(0, 20) + '...');
|
||||||
|
|
||||||
const response = await fetch(`${PB_POCKETBASE_URL}/api/collections/users/auth-refresh`, {
|
const response = await fetch(`${PB_POCKETBASE_URL}/api/collections/users/auth-refresh`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -39,18 +41,20 @@ if (pbAuthCookie && PB_POCKETBASE_URL && PB_POCKETBASE_URL.startsWith('http')) {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
signal: controller.signal,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
clearTimeout(timeoutId);
|
console.log('[SSR] PB response:', response.status);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
console.log('[SSR] Auth valid, user:', data.record?.email);
|
||||||
isAuthorized = true;
|
isAuthorized = true;
|
||||||
} else {
|
} else {
|
||||||
|
console.log('[SSR] Auth invalid, deleting cookie');
|
||||||
Astro.cookies.delete('pb_auth', { path: '/' });
|
Astro.cookies.delete('pb_auth', { path: '/' });
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[SSR Auth] Error:', e);
|
console.error('[SSR] Error:', e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue