auth-patterns

spences10 的头像作者 spences10

Better-auth 身份验证集成。在实现登录、注册、受保护的路由或邮箱验证时使用。

📖 中文导读:Better-auth 身份验证集成。在实现登录、注册、受保护的路由或邮箱验证时使用。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: auth-patterns

prettier-ignore

description: Better-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