npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

tom-yang-auth-sdk

v1.1.3

Published

A comprehensive authentication SDK supporting email/phone verification, registration, login, and password management

Readme

Auth SDK

一个用于处理登录注册、防刷验证码发送和注册流程的SDK。

功能特性

  • 🔐 支持邮箱和手机号登录注册
  • 🛡️ 集成GeeTest验证码防刷机制
  • ⏱️ 验证码倒计时防重复发送
  • 🌍 多语言和地区支持
  • 📱 手机号格式验证
  • 🔄 完整的注册流程处理
  • ⚡ React Hook支持
  • 🎯 TypeScript类型安全

安装使用

基本使用

import { createAuthSDK, AuthSDKConfig } from '@/sdk/auth-sdk'

const config: AuthSDKConfig = {
  apiBaseUrl: 'https://api.example.com/',
  verificationApiUrl: '/v-api/?method=verification-code',
  accountCreateApiUrl: '/v-api/?method=account/create',
  geetestConfig: {
    captchaId: 'your-captcha-id',
    product: 'bind',
    language: 'eng',
  },
  companyId: 48,
  defaultCountryCode: '852',
  callbacks: {
    onSuccess: (data) => console.log('Success:', data),
    onError: (error) => console.error('Error:', error),
    onLoadingChange: (loading) => console.log('Loading:', loading),
  },
}

const authSDK = createAuthSDK(config)

// 发送验证码
await authSDK.sendVerificationCode({
  type: 'email',
  value: '[email protected]',
  source: 'register',
})

// 注册用户
await authSDK.register({
  type: 'email',
  email: '[email protected]',
  verificationCode: '123456',
  codeId: 'code-id',
  country: 'US',
})

React Hook使用

// React 适配(可选):
// import { useAuthSDK } from '@/sdk/auth-sdk-adapters/react/useAuthSDK'

function LoginForm() {
  const {
    loading,
    countdown,
    sendVerificationCode,
    validateVerificationCode,
    register,
    login,
    validateEmail,
    validatePhone,
    error,
    clearError,
  } = useAuthSDK({
    config: {
      apiBaseUrl: 'https://api.example.com/',
      verificationApiUrl: '/v-api/?method=verification-code',
      accountCreateApiUrl: '/v-api/?method=account/create',
      geetestConfig: {
        captchaId: 'your-captcha-id',
        product: 'bind',
      },
      companyId: 48,
      defaultCountryCode: '852',
    },
  })

  const handleSendCode = async () => {
    try {
      await sendVerificationCode({
        type: 'email',
        value: '[email protected]',
        source: 'register',
      })
    } catch (error) {
      console.error('Send code failed:', error)
    }
  }

  return (
    <div>
      {error && <div className="error">{error.message}</div>}
      <button 
        onClick={handleSendCode}
        disabled={loading || countdown > 0}
      >
        {countdown > 0 ? `Resend in ${countdown}s` : 'Send Code'}
      </button>
    </div>
  )
}

API 参考

AuthSDKConfig

interface AuthSDKConfig {
  apiBaseUrl: string
  verificationApiUrl: string
  accountCreateApiUrl: string
  geetestConfig: {
    captchaId: string
    product: string
    language?: string
    timeout?: number
  }
  companyId: number
  defaultCountryCode: string
  callbacks?: {
    onSuccess?: (data: any) => void
    onError?: (error: Error) => void
    onLoadingChange?: (loading: boolean) => void
  }
}

主要方法

sendVerificationCode(request: VerificationRequest)

发送验证码

interface VerificationRequest {
  type: 'email' | 'phone'
  value: string
  countryCode?: string
  source: 'register' | 'login' | 'forget'
}

register(request: RegistrationRequest)

用户注册

interface RegistrationRequest {
  type: 'email' | 'phone'
  email?: string
  phone?: string
  password?: string
  areaCode?: string
  verificationCode: string
  codeId: string
  country: string
  currency?: string
  leverage?: number
  termsAndConditions?: boolean
  [key: string]: any
}

login(request: LoginRequest)

用户登录

interface LoginRequest {
  type: 'email' | 'phone'
  email?: string
  phone?: string
  password?: string
  verificationCode?: string
  codeId?: string
  areaCode?: string
}

错误处理

SDK提供了完整的错误处理机制:

const authSDK = createAuthSDK({
  ...config,
  callbacks: {
    onError: (error) => {
      // 处理错误
      console.error('Auth error:', error.message)
      
      // 显示用户友好的错误消息
      if (error.message.includes('verification')) {
        showToast('验证码错误')
      } else if (error.message.includes('rate_limit')) {
        showToast('请求过于频繁,请稍后再试')
      }
    },
  },
})

注意事项

  1. 确保在使用前加载GeeTest脚本
  2. 验证码倒计时会自动管理,无需手动处理
  3. 所有API调用都是异步的,需要适当的错误处理
  4. SDK会自动处理cookie和重定向逻辑
  5. 支持自定义配置和回调函数

迁移指南

从现有代码迁移到SDK:

  1. 替换现有的验证码发送逻辑
  2. 使用SDK的注册方法替代原有的注册流程
  3. 利用React Hook简化状态管理
  4. 统一错误处理机制