diff --git a/SharpbinV3.Client/src/app.d.ts b/SharpbinV3.Client/src/app.d.ts index da08e6d..e53a4ee 100644 --- a/SharpbinV3.Client/src/app.d.ts +++ b/SharpbinV3.Client/src/app.d.ts @@ -8,6 +8,24 @@ declare global { // interface PageState {} // interface Platform {} } + interface Window { + turnstile?: { + getResponse: () => string | null; + render: (container: string | HTMLElement, options?: Record) => string; + reset: (widgetId?: string) => void; + remove: (widgetId?: string) => void; + }; + turnstileLoaded?: () => void; + } + var turnstile: + | { + getResponse: () => string | null; + render: (container: string | HTMLElement, options?: Record) => string; + reset: (widgetId?: string) => void; + remove: (widgetId?: string) => void; + } + | undefined; + var turnstileLoaded: (() => void) | undefined; } export {}; diff --git a/SharpbinV3.Client/src/lib/components/Navbar.svelte b/SharpbinV3.Client/src/lib/components/Navbar.svelte index f1ad143..21c0817 100644 --- a/SharpbinV3.Client/src/lib/components/Navbar.svelte +++ b/SharpbinV3.Client/src/lib/components/Navbar.svelte @@ -2,10 +2,10 @@ import { user } from '$lib/stores/user'; import { resolve } from '$app/paths'; import { page } from '$app/state'; + import { clearTokens } from '$lib/utils/auth'; function logout() { - localStorage.removeItem('token'); - localStorage.removeItem('refreshToken'); + clearTokens(); user.set(null); window.location.href = '/'; } diff --git a/SharpbinV3.Client/src/routes/+layout.server.ts b/SharpbinV3.Client/src/routes/+layout.server.ts new file mode 100644 index 0000000..8286c63 --- /dev/null +++ b/SharpbinV3.Client/src/routes/+layout.server.ts @@ -0,0 +1,11 @@ +import type { LayoutServerLoad } from './$types'; + +export const load: LayoutServerLoad = async ({ fetch }) => { + try { + const res = await fetch('/api/auth/info'); + const options = await res.json(); + return { options }; + } catch { + return { options: null }; + } +}; diff --git a/SharpbinV3.Client/src/routes/+layout.svelte b/SharpbinV3.Client/src/routes/+layout.svelte index d84998d..20d0b4f 100644 --- a/SharpbinV3.Client/src/routes/+layout.svelte +++ b/SharpbinV3.Client/src/routes/+layout.svelte @@ -5,9 +5,16 @@ import Navbar from '$lib/components/Navbar.svelte'; import { user } from '$lib/stores/user'; import { startTokenRefreshInterval, getToken, refreshTokenIfNeeded } from '$lib/utils/auth'; + import { page } from '$app/state'; + import type { LayoutData } from './$types'; + + export let data: LayoutData; if (typeof window !== 'undefined') { startTokenRefreshInterval(); + window.turnstileLoaded = () => { + window.dispatchEvent(new CustomEvent('turnstile:loaded')); + }; } onMount(async () => { @@ -29,6 +36,13 @@ + {#if data.options?.cf_turnstile_site_key && (page.url.pathname === '/login' || page.url.pathname === '/register')} + + {/if} diff --git a/SharpbinV3.Client/src/routes/health/+server.ts b/SharpbinV3.Client/src/routes/health/+server.ts index 4ce3615..27c7fde 100644 --- a/SharpbinV3.Client/src/routes/health/+server.ts +++ b/SharpbinV3.Client/src/routes/health/+server.ts @@ -1,5 +1,5 @@ import { json } from '@sveltejs/kit'; export const GET = () => { - return json({ status: 'ok' }); + return json({ status: 'ok' }); }; diff --git a/SharpbinV3.Client/src/routes/login/+page.server.ts b/SharpbinV3.Client/src/routes/login/+page.server.ts deleted file mode 100644 index 4a7aa60..0000000 --- a/SharpbinV3.Client/src/routes/login/+page.server.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { PageServerLoad } from './$types'; - -export const load: PageServerLoad = async ({ fetch }) => { - try { - const res = await fetch('/api/auth/info'); - const options = await res.json(); - return { options }; - } catch { - return { options: null }; - } -}; diff --git a/SharpbinV3.Client/src/routes/login/+page.svelte b/SharpbinV3.Client/src/routes/login/+page.svelte index b9ee47f..012516e 100644 --- a/SharpbinV3.Client/src/routes/login/+page.svelte +++ b/SharpbinV3.Client/src/routes/login/+page.svelte @@ -1,6 +1,7 @@ - - {#if data.options?.cf_turnstile_site_key} - - {/if} - -
@@ -75,16 +91,12 @@ /> {#if data.options?.cf_turnstile_site_key}
-
+
{/if} diff --git a/SharpbinV3.Client/src/routes/register/+page.server.ts b/SharpbinV3.Client/src/routes/register/+page.server.ts deleted file mode 100644 index 4a7aa60..0000000 --- a/SharpbinV3.Client/src/routes/register/+page.server.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { PageServerLoad } from './$types'; - -export const load: PageServerLoad = async ({ fetch }) => { - try { - const res = await fetch('/api/auth/info'); - const options = await res.json(); - return { options }; - } catch { - return { options: null }; - } -}; diff --git a/SharpbinV3.Client/src/routes/register/+page.svelte b/SharpbinV3.Client/src/routes/register/+page.svelte index a8ec4bd..a35ba3d 100644 --- a/SharpbinV3.Client/src/routes/register/+page.svelte +++ b/SharpbinV3.Client/src/routes/register/+page.svelte @@ -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 = { 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 @@ } - - {#if data.options?.cf_turnstile_site_key} - - {/if} - -
@@ -197,17 +213,13 @@ {/if} {#if data.options?.cf_turnstile_site_key}
-
+
{/if} diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte index 89ee327..8629f7b 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte @@ -1,34 +1,31 @@