revert captcha

This commit is contained in:
Face 2025-06-24 20:49:27 +03:00
parent 519236913e
commit 96cb799cc7
12 changed files with 12 additions and 186 deletions

View file

@ -8,9 +8,6 @@
import { Send, DollarSign, Coins, Loader2 } from 'lucide-svelte';
import { PORTFOLIO_DATA } from '$lib/stores/portfolio-data';
import { toast } from 'svelte-sonner';
import { Turnstile } from 'svelte-turnstile';
import { PUBLIC_TURNSTILE_SITE_KEY } from '$env/static/public';
import { page } from '$app/stores';
let {
open = $bindable(false),
@ -27,9 +24,6 @@
let amount = $state('');
let selectedCoinSymbol = $state('');
let loading = $state(false);
let turnstileToken = $state('');
let turnstileError = $state('');
let turnstileReset = $state<(() => void) | undefined>(undefined);
let numericAmount = $derived(parseFloat(amount) || 0);
let hasValidAmount = $derived(numericAmount > 0);
@ -63,9 +57,6 @@
let isWithinCoinValueLimit = $derived(transferType === 'COIN' ? estimatedValue >= 10 : true);
const turnstileVerified = $derived(!!$page.data?.turnstileVerified);
let optimisticTurnstileVerified = $state(false);
let canSend = $derived(
hasValidAmount &&
hasValidRecipient &&
@ -73,8 +64,7 @@
isWithinCashLimit &&
isWithinCoinValueLimit &&
!loading &&
(transferType === 'CASH' || selectedCoinSymbol.length > 0) &&
(turnstileVerified || optimisticTurnstileVerified || !!turnstileToken)
(transferType === 'CASH' || selectedCoinSymbol.length > 0)
);
function handleClose() {
@ -124,8 +114,7 @@
recipientUsername: recipientUsername.trim(),
type: transferType,
amount: numericAmount,
coinSymbol: transferType === 'COIN' ? selectedCoinSymbol : undefined,
turnstileToken
coinSymbol: transferType === 'COIN' ? selectedCoinSymbol : undefined
})
});
@ -152,9 +141,6 @@
onSuccess?.();
handleClose();
turnstileToken = '';
optimisticTurnstileVerified = true;
} catch (e) {
toast.error('Transfer failed', {
description: (e as Error).message
@ -339,34 +325,6 @@
</div>
</div>
{/if}
{#if !(turnstileVerified || optimisticTurnstileVerified)}
<div>
<Turnstile
siteKey={PUBLIC_TURNSTILE_SITE_KEY}
theme="auto"
size="normal"
bind:reset={turnstileReset}
on:callback={(e: CustomEvent<{ token: string }>) => {
turnstileToken = e.detail.token;
turnstileError = '';
}}
on:error={(e: CustomEvent<{ code: string }>) => {
turnstileToken = '';
turnstileError = e.detail.code || 'Captcha error';
}}
on:expired={() => {
turnstileToken = '';
turnstileError = 'Captcha expired';
}}
execution="render"
appearance="always"
/>
{#if turnstileError}
<p class="text-destructive mt-1 text-xs">{turnstileError}</p>
{/if}
</div>
{/if}
</div>
<Dialog.Footer class="flex gap-2">

View file

@ -7,9 +7,6 @@
import { TrendingUp, TrendingDown, Loader2 } from 'lucide-svelte';
import { PORTFOLIO_SUMMARY } from '$lib/stores/portfolio-data';
import { toast } from 'svelte-sonner';
import { Turnstile } from 'svelte-turnstile';
import { PUBLIC_TURNSTILE_SITE_KEY } from '$env/static/public';
import { page } from '$app/stores';
let {
open = $bindable(false),
@ -27,8 +24,6 @@
let amount = $state('');
let loading = $state(false);
let turnstileToken = $state('');
let turnstileError = $state('');
let numericAmount = $derived(parseFloat(amount) || 0);
let currentPrice = $derived(coin.currentPrice || 0);
@ -44,14 +39,7 @@
let hasEnoughFunds = $derived(
type === 'BUY' ? numericAmount <= userBalance : numericAmount <= userHolding
);
const turnstileVerified = $derived(!!$page.data?.turnstileVerified);
let optimisticTurnstileVerified = $state(false);
let showCaptcha = $derived(!(turnstileVerified || optimisticTurnstileVerified));
let canTrade = $derived(
hasValidAmount && hasEnoughFunds && !loading && (!showCaptcha || !!turnstileToken)
);
let canTrade = $derived(hasValidAmount && hasEnoughFunds && !loading);
function calculateEstimate(amount: number, tradeType: 'BUY' | 'SELL', price: number) {
if (!amount || !price || !coin) return { result: 0 };
@ -82,8 +70,6 @@
loading = false;
}
let turnstileReset = $state<(() => void) | undefined>(undefined);
async function handleTrade() {
if (!canTrade) return;
@ -96,8 +82,7 @@
},
body: JSON.stringify({
type,
amount: numericAmount,
turnstileToken
amount: numericAmount
})
});
@ -116,9 +101,6 @@
onSuccess?.();
handleClose();
turnstileToken = '';
optimisticTurnstileVerified = true;
} catch (e) {
toast.error('Trade failed', {
description: (e as Error).message
@ -212,34 +194,6 @@
{type === 'BUY' ? 'Insufficient funds' : 'Insufficient coins'}
</Badge>
{/if}
{#if showCaptcha}
<div>
<Turnstile
siteKey={PUBLIC_TURNSTILE_SITE_KEY}
theme="auto"
size="normal"
bind:reset={turnstileReset}
on:callback={(e: CustomEvent<{ token: string }>) => {
turnstileToken = e.detail.token;
turnstileError = '';
}}
on:error={(e: CustomEvent<{ code: string }>) => {
turnstileToken = '';
turnstileError = e.detail.code || 'Captcha error';
}}
on:expired={() => {
turnstileToken = '';
turnstileError = 'Captcha expired';
}}
execution="render"
appearance="always"
/>
{#if turnstileError}
<p class="text-destructive mt-1 text-xs">{turnstileError}</p>
{/if}
</div>
{/if}
</div>
<Dialog.Footer class="flex gap-2">
@ -258,4 +212,4 @@
</Button>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>
</Dialog.Root>

View file

@ -15,14 +15,3 @@ if (!building) {
}
export { client as redis };
const TURNSTILE_PREFIX = 'turnstile:verified:';
const TURNSTILE_TTL = 5 * 60; // 5 minutes
export async function setTurnstileVerifiedRedis(userId: string) {
await client.set(`${TURNSTILE_PREFIX}${userId}`, '1', { EX: TURNSTILE_TTL });
}
export async function isTurnstileVerifiedRedis(userId: string): Promise<boolean> {
return !!(await client.get(`${TURNSTILE_PREFIX}${userId}`));
}

View file

@ -1,20 +0,0 @@
import { env } from '$env/dynamic/private';
const TURNSTILE_SECRET = env.TURNSTILE_SECRET_KEY;
export async function verifyTurnstile(token: string, request: Request): Promise<boolean> {
if (!TURNSTILE_SECRET) return false;
const ip = request.headers.get('x-forwarded-for') || request.headers.get('cf-connecting-ip') || undefined;
const body = new URLSearchParams({
secret: TURNSTILE_SECRET,
response: token,
...(ip ? { remoteip: ip } : {})
});
const res = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
method: 'POST',
body,
headers: { 'content-type': 'application/x-www-form-urlencoded' }
});
const data = await res.json();
return !!data.success;
}