← Back to blog

Updated 18 November 2026

Own Auth with SvelteKit server routes


A SvelteKit authentication integration belongs in server only modules, request hooks, server load functions, and server actions. Create the Own Auth instance where browser bundles cannot import it. Read the session token from the application's chosen cookie. Resolve the current session for the incoming request. Put a narrow identity result in request locals, then let protected routes decide what that identity may do. This structure keeps database access, credentials, and internal authentication records away from client code while giving every server route one consistent session boundary.

Create one server authentication module

The shared server module should own database configuration and the Own Auth instance. Import it only from files that SvelteKit guarantees will stay on the server. Do not place it in a generic utility folder that client components can reach. Environment secrets should use SvelteKit's private server environment interface or the deployment platform's server variables. The module exports the configured authentication service, not raw credentials or an unrestricted database client for the browser.

Keep application response shaping outside the authentication instance. A route that signs someone in can select a safe user identifier, email, display name, and session expiry. It should not return the complete internal user or session record. The server sets the session cookie with secure attributes that match the deployment. Local development may need a different secure setting from production, but the cookie name, path, same site policy, and expiry rules should remain deliberate and documented.

Resolve sessions in the request hook

The server hook is the natural place to read the cookie and resolve the current session. Store only the result needed by downstream handlers in request locals. If no cookie exists, locals can represent a signed out request without a database call. If a token is invalid, expired, or revoked, clear the cookie in the response and continue as signed out unless the route requires authentication. Unexpected database or configuration failures should become server errors, not invented signed out results, because hiding an outage changes product behaviour.

Current session checks should happen on requests that depend on current access. Avoid putting a user result into a shared cache or a module global. SvelteKit can reuse server processes across people and requests, so global identity state can cross request boundaries. Request locals are scoped to one request. A protected server load function can read those locals, redirect a signed out visitor to sign in, and load account data only after the identity is established.

Use server actions for credential forms

Signup and sign in forms can post to SvelteKit server actions. Parse and validate the submitted fields before calling the authentication service. Return field errors for expected invalid input without exposing whether an unrelated account exists. On success, set the session cookie and redirect to the intended application page. Preserve a validated return path through authentication so a person who opened a protected page can continue there after sign in. Never accept an arbitrary external return URL from the form.

Password reset, email verification, and magic link routes need the same server boundary. The browser can carry the one time value to the route, but the server passes it to Own Auth for verification. The route must not log the URL or token. A successful verification response can update the cookie or redirect as required by that flow. A reused or expired value should return the normal invalid token outcome without revealing stored token data.

Keep authorization close to the protected action

A current session proves identity, not permission for every operation. Each server action still checks ownership, organisation membership, or the exact permission required by the record being changed. Use the user or organisation identifier from the authenticated server result in the database query. Do not accept an owner identifier from form data when the server already knows it. This prevents a valid session from becoming permission to act on another account's records.

Complete the integration by checking sign in, sign out, expiry, revocation, and a later protected action in the same browser session. Confirm that clearing the cookie signs the browser out, and that revoking the saved session blocks the next request even when the cookie remains. Confirm that concurrent requests do not share identity state. Those outcomes prove that SvelteKit request handling and Own Auth session rules meet at one reliable server boundary.

SvelteKit authentication questions

Where should the Own Auth instance live in SvelteKit?

Create it in a server only module that client components cannot import. Server hooks, server load functions, route handlers, and server actions can use that shared instance without exposing database configuration to the browser.

Should a current user be stored in a module global?

No. Server processes handle requests for many people. Resolve the session for each request and keep the narrow identity result in request locals so one person's state cannot leak into another request.