Directory Structure
This guide explains the recommended directory structure for a SvelteKit project using Svelte Guardian.
Overview
Key Files
src/lib/server/auth.ts
This is the main configuration file for Svelte Guardian. It exports the authentication functions and middleware used throughout your application.
src/hooks.server.ts
This file links your authentication system to SvelteKit’s request handling pipeline.
Authentication Routes
These routes handle user authentication flows:
src/routes/signin/
- User sign-insrc/routes/signup/
- User registrationsrc/routes/signout/
- User sign-outsrc/routes/verify-email/
- Email verificationsrc/routes/reset-password/
- Password reset
TypeScript Definitions (Optional)
For TypeScript users, you’ll want to extend the app.d.ts file to include session types:
Best Practices
Separation of Concerns: Keep authentication configuration in the server directory to prevent client-side bundling.
Type Safety: Use TypeScript for better autocompletion and error checking.
Environment Variables: Store sensitive values like API keys in environment variables.
Progressive Enhancement: Design your forms to work without JavaScript by using form actions.
Security: Follow the principle of least privilege when setting up route protection.