fix: use initData directly for reactions instead of session token

This commit is contained in:
2026-02-07 06:12:47 +03:00
parent 2adf3760ce
commit d98ae6dd81
5 changed files with 63 additions and 101 deletions
+5 -4
View File
@@ -144,7 +144,7 @@ export function useAnnouncements() {
});
}
export function useAnnouncementReaction(sessionToken: string | null) {
export function useAnnouncementReaction() {
const queryClient = useQueryClient();
return useMutation({
@@ -155,11 +155,12 @@ export function useAnnouncementReaction(sessionToken: string | null) {
announcementId: string;
reaction: 'like' | 'dislike';
}) => {
if (!sessionToken) throw new Error('Not authenticated');
const initData = window.Telegram?.WebApp?.initData;
if (!initData) throw new Error('Telegram not available');
// Call Edge Function for secure reaction handling
// Call Edge Function with initData for validation
const { data, error } = await supabase.functions.invoke('announcement-reaction', {
body: { sessionToken, announcementId, reaction },
body: { initData, announcementId, reaction },
});
if (error) {
+7 -54
View File
@@ -10,46 +10,27 @@ import {
import { cn, formatDate, formatNumber } from '@/lib/utils';
import { useTelegram } from '@/hooks/useTelegram';
import { useAnnouncements, useAnnouncementReaction } from '@/hooks/useSupabase';
import { useAuth } from '@/contexts/AuthContext';
export function AnnouncementsSection() {
const { hapticImpact, hapticNotification, openLink } = useTelegram();
const {
isAuthenticated,
sessionToken,
user,
authError,
signIn,
isLoading: authLoading,
} = useAuth();
const { data: announcements, isLoading, refetch, isRefetching } = useAnnouncements();
const reactionMutation = useAnnouncementReaction(sessionToken);
// Debug: Log auth state
console.warn('[Announcements] Auth state:', {
isAuthenticated,
hasSessionToken: !!sessionToken,
user: user?.first_name,
authError,
});
const reactionMutation = useAnnouncementReaction();
const handleReaction = (id: string, reaction: 'like' | 'dislike') => {
if (!isAuthenticated) {
if (!window.Telegram?.WebApp?.initData) {
hapticNotification('error');
// Show alert or toast here if UI library allows, for now using browser alert for clarity in dev
// In production better to use a Toast component
if (window.Telegram?.WebApp) {
window.Telegram.WebApp.showAlert('Ji bo dengdanê divê tu têketî bî');
} else {
window.alert('Ji bo dengdanê divê tu têketî bî');
window.Telegram.WebApp.showAlert('Ji bo dengdanê divê tu di Telegramê de bî');
}
return;
}
hapticImpact('light');
reactionMutation.mutate(
{ announcementId: id, reaction },
{ onSuccess: () => hapticNotification('success') }
{
onSuccess: () => hapticNotification('success'),
onError: () => hapticNotification('error'),
}
);
};
@@ -81,34 +62,6 @@ export function AnnouncementsSection() {
</div>
</header>
{/* Debug Banner - Remove after fixing */}
<div className="bg-yellow-500/20 text-yellow-300 text-xs p-2 mx-4 mt-2 rounded break-all">
<div>
Auth: {isAuthenticated ? 'YES' : 'NO'} | Token: {sessionToken ? 'YES' : 'NO'} | User:{' '}
{user?.first_name || 'null'}
</div>
<div>Err: {authError || 'none'}</div>
<div>
TG: {window.Telegram?.WebApp ? 'YES' : 'NO'} | initData:{' '}
{window.Telegram?.WebApp?.initData
? window.Telegram.WebApp.initData.length + ' chars'
: 'EMPTY'}
</div>
<div>
Platform: {window.Telegram?.WebApp?.platform || 'unknown'} | Ver:{' '}
{window.Telegram?.WebApp?.version || '?'}
</div>
{!isAuthenticated && (
<button
onClick={() => signIn()}
disabled={authLoading}
className="mt-2 px-3 py-1 bg-yellow-500 text-black rounded text-xs font-medium"
>
{authLoading ? 'Deneniyor...' : 'Retry Auth'}
</button>
)}
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto hide-scrollbar">
{isLoading ? (
+3 -3
View File
@@ -1,5 +1,5 @@
{
"version": "1.0.150",
"buildTime": "2026-02-07T02:53:36.684Z",
"buildNumber": 1770432816685
"version": "1.0.151",
"buildTime": "2026-02-07T03:12:47.597Z",
"buildNumber": 1770433967598
}