diff --git a/biome.json b/biome.json index fa29fb62..88cb5b34 100644 --- a/biome.json +++ b/biome.json @@ -27,6 +27,18 @@ } } }, + "overrides": [ + { + "include": ["examples/svelte-kit-example/**"], + "linter": { + "rules": { + "correctness": { + "noUnusedImports": "off" + } + } + } + } + ], "files": { "ignore": [ "dist", diff --git a/examples/svelte-kit-example/.gitignore b/examples/svelte-kit-example/.gitignore index 79518f71..5521b458 100644 --- a/examples/svelte-kit-example/.gitignore +++ b/examples/svelte-kit-example/.gitignore @@ -19,3 +19,8 @@ Thumbs.db # Vite vite.config.js.timestamp-* vite.config.ts.timestamp-* + +# You cannot create root page under (protected)/ route +# because it is not protected by authwall. +./src/routes/(protected)/+page.* +./src/routes/(protected)/+server.* diff --git a/examples/svelte-kit-example/README.md b/examples/svelte-kit-example/README.md index 1d0b8cd3..5e82d187 100644 --- a/examples/svelte-kit-example/README.md +++ b/examples/svelte-kit-example/README.md @@ -12,7 +12,11 @@ Email & Password . Social Sign-in with Google . Passkeys . Email Verifica 2. Move .env.example to .env and provide necessary variables 3. Run the following commands ```bash + cd /path/to/better-auth/ # Project root of this better-auth repo, not the root of this example pnpm install + pnpm build + cd ./examples/svelte-kit-example/ # The root of this example project + pnpm migrate pnpm dev ``` 4. Open the browser and navigate to `http://localhost:3000` diff --git a/examples/svelte-kit-example/package.json b/examples/svelte-kit-example/package.json index 20414d37..10719b79 100644 --- a/examples/svelte-kit-example/package.json +++ b/examples/svelte-kit-example/package.json @@ -5,21 +5,24 @@ "scripts": { "dev": "vite dev --port 3000", "build": "vite build", + "migrate": "pnpx @better-auth/cli migrate", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", - "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch" + "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", + "prepare": "svelte-kit sync" }, "devDependencies": { - "@sveltejs/adapter-auto": "^3.3.1", + "@better-auth/cli": "workspace:*", + "@sveltejs/adapter-auto": "^4.0.0", "@sveltejs/kit": "^2.9.0", - "@sveltejs/vite-plugin-svelte": "^3.1.2", + "@sveltejs/vite-plugin-svelte": "^5.0.3", "@tailwindcss/typography": "^0.5.15", "autoprefixer": "^10.4.20", - "svelte": "^4.2.19", + "svelte": "^5.22.5", "svelte-check": "^4.1.1", "tailwindcss": "^3.4.16", "typescript": "^5.7.2", - "vite": "^5.4.19" + "vite": "^6.3.0" }, "type": "module", "dependencies": { @@ -34,7 +37,7 @@ "formsnap": "^1.0.1", "mode-watcher": "^0.4.1", "paneforge": "^0.0.6", - "svelte-radix": "^1.1.1", + "svelte-radix": "^2.0.1", "svelte-sonner": "^0.3.28", "sveltekit-superforms": "^2.21.1", "tailwind-merge": "^2.5.5", diff --git a/examples/svelte-kit-example/src/app.d.ts b/examples/svelte-kit-example/src/app.d.ts index 743f07b2..30b8db74 100644 --- a/examples/svelte-kit-example/src/app.d.ts +++ b/examples/svelte-kit-example/src/app.d.ts @@ -2,8 +2,11 @@ // for information about these interfaces declare global { namespace App { + interface Locals { + session?: Session; + user?: User; + } // interface Error {} - // interface Locals {} // interface PageData {} // interface PageState {} // interface Platform {} diff --git a/examples/svelte-kit-example/src/hooks.server.ts b/examples/svelte-kit-example/src/hooks.server.ts index bec86f40..30c80121 100644 --- a/examples/svelte-kit-example/src/hooks.server.ts +++ b/examples/svelte-kit-example/src/hooks.server.ts @@ -1,6 +1,22 @@ import { auth } from "$lib/auth"; +import { redirect, type Handle } from "@sveltejs/kit"; import { svelteKitHandler } from "better-auth/svelte-kit"; -export async function handle({ event, resolve }) { - return svelteKitHandler({ event, resolve, auth }); -} +export const handle: Handle = async ({ event, resolve }) => { + if (event.route.id?.startsWith("/(protected)/")) { + const session = await auth.api.getSession({ + headers: event.request.headers, + }); + + if (session) { + event.locals.session = session?.session; + event.locals.user = session?.user; + + return svelteKitHandler({ event, resolve, auth }); + } else { + redirect(307, "/sign-in"); + } + } else { + return svelteKitHandler({ event, resolve, auth }); + } +}; diff --git a/examples/svelte-kit-example/src/lib/auth.ts b/examples/svelte-kit-example/src/lib/auth.ts index f70006d1..62af92bd 100644 --- a/examples/svelte-kit-example/src/lib/auth.ts +++ b/examples/svelte-kit-example/src/lib/auth.ts @@ -16,4 +16,10 @@ export const auth = betterAuth({ console.log("Reset password url:", url); }, }, + emailVerification: { + sendOnSignUp: false, // TODO enable this option to send email to the user on sign up + // sendVerificationEmail: async ({ user, url, token }, request) => { + // // TODO add function(s) to send verification email. + // }, + }, }); diff --git a/examples/svelte-kit-example/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte b/examples/svelte-kit-example/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte index 064b544b..3d6fbd80 100644 --- a/examples/svelte-kit-example/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte +++ b/examples/svelte-kit-example/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte @@ -1,5 +1,6 @@ - - + + - + diff --git a/examples/svelte-kit-example/src/lib/components/ui/avatar/avatar-fallback.svelte b/examples/svelte-kit-example/src/lib/components/ui/avatar/avatar-fallback.svelte index 8b88c0d7..0cede4b5 100644 --- a/examples/svelte-kit-example/src/lib/components/ui/avatar/avatar-fallback.svelte +++ b/examples/svelte-kit-example/src/lib/components/ui/avatar/avatar-fallback.svelte @@ -1,5 +1,6 @@ @@ -33,9 +41,7 @@ const session = client.useSession(); on:click={() => { client.signOut({ fetchOptions: { - body: { - callbackURL: "/", - }, + onSuccess: () => goto("/auth"), }, }); }}>Sign Out import { client } from "$lib/auth-client"; +import Button from "$lib/components/ui/button/button.svelte"; + const features = [ "social sign-in", "email and password", diff --git a/examples/svelte-kit-example/src/routes/forget-password/+page.svelte b/examples/svelte-kit-example/src/routes/forget-password/+page.svelte index 77c5277d..ef11fb97 100644 --- a/examples/svelte-kit-example/src/routes/forget-password/+page.svelte +++ b/examples/svelte-kit-example/src/routes/forget-password/+page.svelte @@ -1,5 +1,10 @@ diff --git a/examples/svelte-kit-example/src/routes/reset-password/+page.svelte b/examples/svelte-kit-example/src/routes/reset-password/+page.svelte index cdea0834..0de0b331 100644 --- a/examples/svelte-kit-example/src/routes/reset-password/+page.svelte +++ b/examples/svelte-kit-example/src/routes/reset-password/+page.svelte @@ -1,5 +1,10 @@