tom-yang-auth-sdk
v1.1.3
Published
A comprehensive authentication SDK supporting email/phone verification, registration, login, and password management
Maintainers
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('请求过于频繁,请稍后再试')
}
},
},
})注意事项
- 确保在使用前加载GeeTest脚本
- 验证码倒计时会自动管理,无需手动处理
- 所有API调用都是异步的,需要适当的错误处理
- SDK会自动处理cookie和重定向逻辑
- 支持自定义配置和回调函数
迁移指南
从现有代码迁移到SDK:
- 替换现有的验证码发送逻辑
- 使用SDK的注册方法替代原有的注册流程
- 利用React Hook简化状态管理
- 统一错误处理机制
