返回 Skill 列表
extension
分类: 开发与工程无需 API Key

auth-schema-generator

生成更好的Auth用户模式配置,包括自定义的额外字段以用于用户资料。在实现身份验证、用户资料或使用Better Auth扩展用户数据模型时使用。自动生TypeScript类型和数据库模式。

person作者: jakexiaohubgithub

Auth Schema Generator

Generate Better Auth user schema configurations with custom additional fields for authentication systems.

When to Use

Use this skill when:

  • Implementing Better Auth in a new project
  • Adding custom user profile fields (background info, preferences, settings)
  • Extending user data model with questionnaire responses
  • Setting up authentication with personalized user data

Instructions

Step 1: Identify Required Fields

Determine what additional user data you need:

  • User preferences (theme, language, notifications)
  • Profile information (job title, department, bio)
  • Background data (experience levels, skills, interests)
  • System metadata (onboarding status, last login)

Step 2: Generate Schema Configuration

Create Better Auth configuration in src/lib/auth.ts:

export const auth = betterAuth({
  database: pool,
  emailAndPassword: {
    enabled: true,
  },
  user: {
    additionalFields: {
      // Generated fields based on requirements
      fieldName: {
        type: "string",
        required: true,
        defaultValue: "default",
      },
    },
  },
});

Step 3: Generate TypeScript Types

Export inferred types for type safety:

export type Session = typeof auth.$Infer.Session;
export type User = typeof auth.$Infer.User;

Step 4: Create Interface Definitions

Generate TypeScript interfaces for use in components:

interface UserProfile {
  [fieldName]: string;
  // Additional fields...
}

Example: Background Questionnaire

For a learning platform with user background profiling:

Input: Need to track software experience, AI/ML familiarity, hardware knowledge, learning goals

Generated Schema:

user: {
  additionalFields: {
    softwareExperience: {
      type: "string",
      required: true,
      defaultValue: "beginner"
    },
    aiMlFamiliarity: {
      type: "string",
      required: true,
      defaultValue: "none"
    },
    hardwareExperience: {
      type: "string",
      required: true,
      defaultValue: "none"
    },
    learningGoals: {
      type: "string",
      required: true,
      defaultValue: "hobby"
    },
    programmingLanguages: {
      type: "string",
      required: false,
      defaultValue: ""
    }
  }
}

Generated Interface:

interface BackgroundProfile {
  softwareExperience: string;
  aiMlFamiliarity: string;
  hardwareExperience: string;
  learningGoals: string;
  programmingLanguages?: string;
}

Field Types Supported

  • string: Text fields
  • number: Numeric values
  • boolean: True/false flags
  • date: Timestamps

Best Practices

  1. Use Descriptive Names: softwareExperience not exp
  2. Set Sensible Defaults: Provide default values for required fields
  3. Mark Optional Fields: Use required: false for optional data
  4. Type Safety: Always export TypeScript types
  5. Validation: Validate field values in your forms before submission

Files Modified

This skill typically modifies:

  • src/lib/auth.ts - Better Auth configuration
  • src/types/auth.ts - TypeScript type definitions (if separate file)
  • src/hooks/useAuth.ts - Custom auth hook to use typed data

Security Considerations

  • Never store sensitive data in additional fields (use separate encrypted storage)
  • Validate all user input before saving to database
  • Use appropriate field types for the data being stored
  • Consider GDPR/privacy implications for profile data