Svelte Clerk
repository·main·Indexed 42 days ago
https://github.com/wobsoriano/svelte-clerkAn unofficial SDK for Svelte and SvelteKit applications providing authentication and user management capabilities, including sign up, sign in, and profile management. Version 1.2.0 features prebuilt components, hooks like useSignIn() and useSignUp(), and server-side helpers such as withClerkHandler() and buildClerkProps() for SSR support.
What's inside Svelte Clerk
- Svelte Clerk is an unofficial SDK designed to add authentication and user management to Svelte and SvelteKit applications. It provides tools for implementing sign up, sign in, and profile management workflows.
Available Clerk UI components in Svelte Clerk
mainSvelte Clerk provides a set of UI components that mirror the Clerk React SDK but utilize Svelte Snippets for content rendering.
Available components include:
- Authentication:
<SignIn>,<SignUp>,<GoogleOneTap>,<Waitlist /> - User Management:
<UserButton>,<UserAvatar>,<UserProfile> - Organization Management:
<OrganizationList>,<OrganizationProfile>,<OrganizationSwitcher>,<CreateOrganization> - Control/Loading:
<Show>,<ClerkLoaded>,<ClerkLoading> - Other:
<PricingTable />,<APIKeys />
For detailed prop definitions for each component, refer to the official Clerk UI components documentation.
- Authentication:
Set Clerk API keys in environment variables
mainAdd your Clerk API keys to your
.envfile. You can find these in your Clerk Dashboard under the API Keys section.Required keys:
PUBLIC_CLERK_PUBLISHABLE_KEY: The public key for client-side operations.CLERK_SECRET_KEY: The secret key for server-side operations.
PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_xxxxxxxx CLERK_SECRET_KEY=sk_test_xxxxxxxEnable SSR support for Clerk
mainTo enable Server-Side Rendering (SSR) support, pass the initial authentication state from your server load function to the client using
buildClerkProps.// src/routes/+layout.server.ts import { buildClerkProps } from 'svelte-clerk/server'; import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = ({ locals }) => { return { ...buildClerkProps(locals.auth()) }; };Build a custom sign-in or sign-up page in SvelteKit
mainYou can host Clerk's
<SignIn />component on a custom SvelteKit route to allow users to sign in or switch to sign up within a single flow.To implement this, you must create a rest route to support Clerk's internal sub-routes (like multi-step authentication) and configure the appropriate environment variables so
svelte-clerkknows where to redirect users.<script lang="ts"> import { SignIn } from 'svelte-clerk'; </script> <SignIn />Install svelte-clerk
mainInstall the Svelte Clerk SDK using npm to access prebuilt components, runes, and authentication helpers.
npm install svelte-clerkProtect routes server-side
mainTo protect specific routes on the server, check for the existence of
userIdwithin thelocals.auth()object in your SvelteKitloadfunction. If nouserIdis present, redirect the user to your sign-in page.import { redirect } from '@sveltejs/kit'; export const load = async ({ locals }) => { const { userId } = locals.auth(); if (!userId) { return redirect(307, '/sign-in'); } return { userId }; };Integrate Clerk authentication with `withClerkHandler()`
mainUse the
withClerkHandler()helper to integrate Clerk authentication and authorization into your SvelteKit application via hooks. This is typically exported from yoursrc/hooks.server.tsfile. While it accepts an optional options object, it is recommended to configure Clerk using environment variables instead.import { withClerkHandler } from 'svelte-clerk/server'; export const handle = withClerkHandler();Add locals types for Clerk
mainTo ensure the
localsadded by the Clerk handler are properly typed throughout your application, add thesvelte-clerk/envreference to yoursrc/app.d.tsfile./// <reference types="svelte-clerk/env" /> declare global { namespace App {...} }Create the sign-in route for Clerk
mainTo support Clerk's internal sub-routes (such as multi-step auth paths), create a rest route in SvelteKit at
src/routes/sign-in/[...sign-in]/+page.svelteand render the<SignIn />component.<script lang="ts"> import { SignIn } from 'svelte-clerk'; </script> <SignIn />Add <ClerkProvider> to your root layout
mainWrap your application in the
<ClerkProvider>component within your root layout. This component provides the active session and user context required by all Clerk runes and components.<script lang="ts"> import type { Snippet } from 'svelte'; import { ClerkProvider } from 'svelte-clerk'; const { children }: { children: Snippet } = $props(); </script> <ClerkProvider> {@render children()} </ClerkProvider>Configure the Clerk server handler
mainIn SvelteKit, use
withClerkHandlerin yourhooks.server.tsfile. This handler authenticates incoming requests and populatesevent.locals.auth()with the ClerkAuthobject, making it available in server loaders and actions.// hooks.server.ts import { withClerkHandler } from 'svelte-clerk/server'; export const handle = withClerkHandler();