mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: load turnstile script in layout and explicitly render
chore: npm run format + lint
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
import { Check, X } from '@lucide/svelte';
|
||||
import { slide, fade } from 'svelte/transition';
|
||||
import { resolve } from '$app/paths';
|
||||
import { onMount } from 'svelte';
|
||||
import type { PageData } from './$types';
|
||||
|
||||
let username = '';
|
||||
@@ -25,6 +26,27 @@
|
||||
|
||||
export let data: PageData;
|
||||
|
||||
onMount(() => {
|
||||
const render = () => {
|
||||
if (window.turnstile && data.options?.cf_turnstile_site_key) {
|
||||
window.turnstile.render('.cf-turnstile', {
|
||||
sitekey: data.options.cf_turnstile_site_key,
|
||||
theme: 'dark'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (window.turnstile) {
|
||||
render();
|
||||
} else {
|
||||
window.addEventListener('turnstile:loaded', render, { once: true });
|
||||
}
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('turnstile:loaded', render);
|
||||
};
|
||||
});
|
||||
|
||||
function validatePassword(pw: string) {
|
||||
passwordChecks.length = pw.length >= 8;
|
||||
passwordChecks.upper = /[A-Z]/.test(pw);
|
||||
@@ -44,8 +66,8 @@
|
||||
loading = true;
|
||||
error = '';
|
||||
const body: Record<string, unknown> = { username, password, email, displayName };
|
||||
if (data.options?.cf_turnstile_site_key && (window as any).turnstile) {
|
||||
const token = (window as any).turnstile.getResponse();
|
||||
if (data.options?.cf_turnstile_site_key && window.turnstile) {
|
||||
const token = window.turnstile.getResponse();
|
||||
if (token) body.token = token;
|
||||
}
|
||||
const res = await fetch('/api/auth/register', {
|
||||
@@ -76,12 +98,6 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
{#if data.options?.cf_turnstile_site_key}
|
||||
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
|
||||
{/if}
|
||||
</svelte:head>
|
||||
|
||||
<main
|
||||
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
|
||||
>
|
||||
@@ -197,17 +213,13 @@
|
||||
{/if}
|
||||
{#if data.options?.cf_turnstile_site_key}
|
||||
<div class="flex w-full justify-center">
|
||||
<div
|
||||
class="cf-turnstile"
|
||||
data-sitekey={data.options.cf_turnstile_site_key}
|
||||
data-theme="dark"
|
||||
></div>
|
||||
<div class="cf-turnstile"></div>
|
||||
</div>
|
||||
{/if}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!passwordValid || password !== confirmPassword || loading}
|
||||
class="w-full rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-800 disabled:text-neutral-500"
|
||||
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-800 disabled:text-neutral-500"
|
||||
>
|
||||
{loading ? 'Registering...' : 'Register'}
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user