From 69a6ff14c3c8a7a593b2e8b1813c533e76abe6c2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 11:26:31 +0000 Subject: [PATCH 1/7] Initial plan From 43e3c549183e4667336bca1b694942b0905f10c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 11:34:25 +0000 Subject: [PATCH 2/7] Simplify auth to server-side only: remove auth-store, use $page.data.user and invalidateAll() Co-authored-by: acoyfellow <1666099+acoyfellow@users.noreply.github.com> --- .gitignore | 1 + README.md | 1 - packages/create-remote-app/template/README.md | 1 - .../template/src/lib/auth-store.svelte.ts | 93 ------------------ .../template/src/routes/+layout.svelte | 6 +- .../template/src/routes/+page.svelte | 48 ++++++++-- src/lib/auth-store.svelte.ts | 96 ------------------- src/routes/+layout.svelte | 6 +- src/routes/+page.svelte | 35 +++++-- 9 files changed, 66 insertions(+), 221 deletions(-) delete mode 100644 packages/create-remote-app/template/src/lib/auth-store.svelte.ts delete mode 100644 src/lib/auth-store.svelte.ts diff --git a/.gitignore b/.gitignore index b4a8d1c..f4b59e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules +package-lock.json # Output .output diff --git a/README.md b/README.md index 90f0bae..29e1293 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,6 @@ src/ ├── lib/ │ ├── auth.ts # Better Auth configuration │ ├── auth-client.ts # Auth client setup -│ ├── auth-store.svelte.ts # Auth state management │ └── schema.ts # Database schema ├── routes/ │ ├── api/auth/[...all]/ # Better Auth API routes diff --git a/packages/create-remote-app/template/README.md b/packages/create-remote-app/template/README.md index 34f6e8a..e686cf3 100644 --- a/packages/create-remote-app/template/README.md +++ b/packages/create-remote-app/template/README.md @@ -27,7 +27,6 @@ src/ ├── lib/ │ ├── auth.ts # Better Auth configuration │ ├── auth-client.ts # Auth client setup -│ ├── auth-store.svelte.ts # Auth state management │ └── schema.ts # Database schema ├── routes/ │ ├── api/auth/[...all]/ # Better Auth API routes diff --git a/packages/create-remote-app/template/src/lib/auth-store.svelte.ts b/packages/create-remote-app/template/src/lib/auth-store.svelte.ts deleted file mode 100644 index 9ac8e01..0000000 --- a/packages/create-remote-app/template/src/lib/auth-store.svelte.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { browser } from '$app/environment'; -import { signIn, signOut as signOutClient, signUp } from '$lib/auth-client'; -import type { User, Session } from 'better-auth'; - -interface AuthState { - user: User | null; - session: Session | null; - isLoading: boolean; -} - -class AuthStore { - private state = $state({ - user: null, - session: null, - isLoading: false - }); - - // Reactive getters - get user() { return this.state.user; } - get session() { return this.state.session; } - get isLoading() { return this.state.isLoading; } - get isAuthenticated() { return !!this.state.user; } - - // Initialize with server data (called from layout) - initialize(serverUser: User | null, serverSession: Session | null) { - this.state.user = serverUser; - this.state.session = serverSession; - this.state.isLoading = false; - } - - // Auth actions that update the store - async signIn(email: string, password: string) { - this.state.isLoading = true; - try { - const result = await signIn.email({ email, password }); - - if(result.error){ - return alert(result.error.message); - }; - this.state.user = result.data.user; - this.state.isLoading = false; - return result; - } catch (error) { - throw error; - } finally { - this.state.isLoading = false; - } - } - - async signUp(email: string, password: string, name?: string) { - this.state.isLoading = true; - try { - const result = await signUp.email({ - email, - password, - name: name || email.split('@')[0] - }); - - if(result.error){ - return alert(result.error.message); - }; - return result; - } catch (error) { - throw error; - } finally { - this.state.isLoading = false; - } - } - - async signOut() { - this.state.isLoading = true; - try { - await signOutClient(); - this.state.user = null; - this.state.session = null; - this.state.isLoading = false; - } catch (error) { - throw error; - } finally { - this.state.isLoading = false; - } - } - - // Update session data (for when auth state changes) - updateSession(user: User | null, session: Session | null) { - this.state.user = user; - this.state.session = session; - this.state.isLoading = false; - } -} - -// Export singleton instance -export const authStore = new AuthStore(); diff --git a/packages/create-remote-app/template/src/routes/+layout.svelte b/packages/create-remote-app/template/src/routes/+layout.svelte index 8147eb7..801be90 100644 --- a/packages/create-remote-app/template/src/routes/+layout.svelte +++ b/packages/create-remote-app/template/src/routes/+layout.svelte @@ -1,11 +1,7 @@ {@render children()} diff --git a/packages/create-remote-app/template/src/routes/+page.svelte b/packages/create-remote-app/template/src/routes/+page.svelte index 6c3f6cf..47b8cc8 100644 --- a/packages/create-remote-app/template/src/routes/+page.svelte +++ b/packages/create-remote-app/template/src/routes/+page.svelte @@ -1,8 +1,11 @@ @@ -50,11 +78,11 @@ SvelteKit + Better Auth + Durable Objects - {#if authStore.user} + {#if $page.data.user}

- Welcome, {authStore.user.email}! + Welcome, {$page.data.user.email}!

@@ -74,7 +102,7 @@
diff --git a/src/lib/auth-store.svelte.ts b/src/lib/auth-store.svelte.ts deleted file mode 100644 index a65c061..0000000 --- a/src/lib/auth-store.svelte.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { browser } from '$app/environment'; -import { signIn, signOut as signOutClient, signUp } from '$lib/auth-client'; -import type { User, Session } from 'better-auth'; - -interface AuthState { - user: User | null; // User object from Better Auth - session: Session | null; - isLoading: boolean; -} - -class AuthStore { - private state = $state({ - user: null, - session: null, - isLoading: false - }); - - // Reactive getters - get user() { return this.state.user; } - get session() { return this.state.session; } - get isLoading() { return this.state.isLoading; } - get isAuthenticated() { return !!this.state.user; } - - // Initialize with server data (called from layout) - initialize(serverUser: User | null, serverSession: Session | null) { - this.state.user = serverUser; - this.state.session = serverSession; - this.state.isLoading = false; - } - - // Auth actions that update the store - async signIn(email: string, password: string) { - this.state.isLoading = true; - try { - const result = await signIn.email({ email, password }); - - if(result.error){ - return alert(result.error.message); - }; - this.state.user = result.data.user; - this.state.isLoading = false; - return result; - } catch (error) { - this.state.isLoading = false; - throw error; - } - } - - async signUp(email: string, password: string, name?: string) { - this.state.isLoading = true; - try { - const result = await signUp.email({ - email, - password, - name: name || email.split('@')[0] - }); - - if(result.error){ - return alert(result.error.message); - }; - return result; - } catch (error) { - this.state.isLoading = false; - throw error; - } - } - - async signOut() { - this.state.isLoading = true; - try { - await signOutClient(); - this.state.user = null; - this.state.session = null; - this.state.isLoading = false; - } catch (error) { - - this.state.isLoading = false; - throw error; - } - } - - // Update session data (for when auth state changes) - updateSession(user: User | null, session: Session | null) { - this.state.user = user; - this.state.session = session; - this.state.isLoading = false; - } -} - -// Export singleton instance -export const authStore = new AuthStore(); - -// Individual getters for convenience -export const getUser = () => authStore.user; -export const getSession = () => authStore.session; -export const getIsAuthenticated = () => !!authStore.user; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index ae3ea91..9f327f0 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,11 +1,7 @@ {@render children?.()} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 1fc2b16..c2349ff 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,7 +1,8 @@