auth-patterns

Implement Better Auth authentication and session management in SvelteKit apps.

6|Updated Aug 8, 2023
One-click install
npx skills add https://github.com/spences10/devhub-crm --skill auth-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: auth-patterns
Source: https://github.com/spences10/devhub-crm/tree/main/.claude/skills/auth-patterns
Command: npx skills add https://github.com/spences10/devhub-crm --skill auth-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires better-auth, better-sqlite3, resend, valibot, and includes scripts (resource) and references (resource) and assets (resource) components.

What problem does it solve?

This Skill simplifies the implementation of secure authentication flows using better-auth with SvelteKit remote functions. It handles complex aspects like user registration, login, protected routes, email verification, and social logins (e.g., GitHub OAuth), reducing the boilerplate and security risks associated with building auth from scratch.

Core Features & Use Cases

  • Email/Password Authentication: Provides patterns for user registration, login, and password management, including robust validation.
  • Protected Endpoints: Secures server-side queries, forms, and commands, ensuring only authenticated users can access sensitive data or perform actions.
  • Email Verification: Implements a complete email verification flow, including sending verification emails and handling unverified user login attempts.
  • Social Login Integration: Guides on integrating GitHub OAuth for seamless user onboarding and profile data synchronization.
  • Use Case: Build a SaaS application where users need to register, log in, and access personalized dashboards. This Skill provides the complete authentication framework, including email verification to prevent spam accounts and protected routes to secure user data, allowing you to focus on core features.

Quick Start

Configure better-auth with your database, email service (Resend), and social providers (GitHub). Use form() for login/registration, ensuring redirect(303, '/dashboard') is outside try/catch. Protect server functions using guarded_query(), guarded_form(), or guarded_command() helpers. For logout, use a command() and handle client-side navigation with goto('/login').

Frequently Asked Questions about auth-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement user authentication and login in a SvelteKit application?

Authentication in SvelteKit uses Better Auth to handle user registration, login, and session management. Configure Better Auth with your database and email service, then use form() for login/registration forms with redirects outside try/catch blocks to securely manage user credentials and maintain sessions.

How can I protect server endpoints so only authenticated users can access them?

Protected endpoints in SvelteKit are secured using guarded_query(), guarded_form(), or guarded_command() helpers that verify user authentication before executing sensitive operations. These helpers extract the authenticated session from request headers and reject unauthenticated requests automatically.

How do I set up email verification to prevent spam accounts during registration?

Email verification flows with Better Auth and Resend send verification emails to new users and validate their address before granting full access. The Skill provides patterns to handle unverified login attempts and complete the verification workflow end-to-end.

Can I add social login like GitHub OAuth to my SvelteKit app?

Social login integration with Better Auth and GitHub OAuth is supported for seamless user onboarding. Configure GitHub as a provider in Better Auth and the Skill provides patterns for synchronizing profile data and managing OAuth callbacks securely.

What's the best way to handle user logout and session cleanup?

Logout with Better Auth uses a command() function on the server to terminate the session, then client-side navigation with goto('/login') completes the flow. This ensures both server-side session invalidation and proper client-side redirect occur.

Does Better Auth work with SvelteKit's remote functions and form actions?

Better Auth integrates with SvelteKit remote functions and form actions through getRequestEvent() to extract headers and manage authenticated requests. The Skill provides patterns for using form() with protected routes and guarded endpoints in SvelteKit's action framework.