auth-patterns

Better-auth integration for authentication. Use when implementing login, registration, protected routes, or email verification.

⭐6stars🔀0forks📁View on GitHub🕐Updated Jan 1, 2026⬇️1downloads

When & Why to Use This Skill

This Claude skill provides standardized patterns and best practices for integrating Better-auth into modern web applications. It streamlines the implementation of essential security features like user authentication, registration, and protected route management, ensuring developers follow secure coding principles while reducing boilerplate code.

Use Cases

  • Implementing secure login and registration: Quickly generate robust authentication forms and backend logic using Better-auth's API.
  • Protecting sensitive data routes: Utilize 'guarded_query' and 'guarded_form' patterns to ensure that only authenticated users can access specific API endpoints or data.
  • Email verification workflows: Set up comprehensive email validation flows to enhance account security and verify user identities during sign-up.
  • Managing session headers and redirects: Correctly handle cookie-based authentication and server-side redirects to avoid common security vulnerabilities and runtime errors.
nameauth-patterns
descriptionBetter-auth integration for authentication. Use when implementing login, registration, protected routes, or email verification.

Auth Patterns

Quick Pattern

// Login form
export const login = form(schema, async ({ email, password }) => {
	const event = getRequestEvent();
	await auth.api.signInEmail({
		body: { email, password },
		headers: event.request.headers,
	});
	redirect(303, '/dashboard'); // Outside try/catch
});

// Protected query
export const get_data = guarded_query(() => {
	return { message: 'Protected data' };
});

Core Principles

  • Use getRequestEvent() for headers (cookie access)
  • Redirect MUST be outside try/catch (throws error)
  • Use guarded_query/form/command for protected endpoints
  • Email verification required before login
  • Commands cannot redirect - use client-side goto()

Reference Files