@55387.ai/uniauth-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # @55387.ai/uniauth-client
2
+
3
+ UniAuth 前端 SDK,支持手机、邮箱、社交登录和跨域 SSO。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @55387.ai/uniauth-client
9
+ # or
10
+ pnpm add @55387.ai/uniauth-client
11
+ ```
12
+
13
+ ## 快速开始
14
+
15
+ ```typescript
16
+ import { UniAuthClient } from '@55387.ai/uniauth-client';
17
+
18
+ const auth = new UniAuthClient({
19
+ baseUrl: 'https://sso.55387.xyz',
20
+ });
21
+
22
+ // 发送验证码
23
+ await auth.sendCode('+8613800138000');
24
+
25
+ // 验证码登录
26
+ const result = await auth.loginWithCode('+8613800138000', '123456');
27
+
28
+ // 检查登录状态
29
+ if (auth.isAuthenticated()) {
30
+ const user = await auth.getCurrentUser();
31
+ console.log('已登录:', user);
32
+ }
33
+ ```
34
+
35
+ ## SSO 跨域登录
36
+
37
+ ```typescript
38
+ // 配置 SSO
39
+ auth.configureSso({
40
+ ssoUrl: 'https://sso.55387.xyz',
41
+ clientId: 'my-app',
42
+ redirectUri: 'https://my-app.com/auth/callback',
43
+ });
44
+
45
+ // 发起 SSO 登录
46
+ auth.loginWithSSO();
47
+
48
+ // 在回调页面处理
49
+ if (auth.isSSOCallback()) {
50
+ const result = await auth.handleSSOCallback();
51
+ if (result) {
52
+ navigate('/dashboard');
53
+ }
54
+ }
55
+ ```
56
+
57
+ ## MFA 多因素认证
58
+
59
+ ```typescript
60
+ const result = await auth.loginWithCode(phone, code);
61
+
62
+ if (result.mfa_required) {
63
+ const mfaCode = prompt('请输入验证器应用中的验证码:');
64
+ const finalResult = await auth.verifyMFA(result.mfa_token!, mfaCode);
65
+ }
66
+ ```
67
+
68
+ ## 社交登录
69
+
70
+ ```typescript
71
+ // 获取可用的 OAuth 提供商
72
+ const providers = await auth.getOAuthProviders();
73
+
74
+ // 发起社交登录
75
+ auth.startSocialLogin('google');
76
+ ```
77
+
78
+ ## 认证状态监听
79
+
80
+ ```typescript
81
+ const unsubscribe = auth.onAuthStateChange((user, isAuthenticated) => {
82
+ if (isAuthenticated) {
83
+ console.log('用户已登录:', user);
84
+ } else {
85
+ console.log('用户已登出');
86
+ }
87
+ });
88
+
89
+ // 取消监听
90
+ unsubscribe();
91
+ ```
92
+
93
+ ## API 参考
94
+
95
+ ### 初始化选项
96
+
97
+ ```typescript
98
+ interface UniAuthConfig {
99
+ baseUrl: string; // API 地址
100
+ appKey?: string; // 应用密钥
101
+ clientId?: string; // OAuth 客户端 ID
102
+ storage?: 'localStorage' | 'sessionStorage' | 'memory';
103
+ onTokenRefresh?: (tokens) => void;
104
+ onAuthError?: (error) => void;
105
+ enableRetry?: boolean; // 启用重试 (默认 true)
106
+ timeout?: number; // 请求超时 (默认 30000ms)
107
+ }
108
+ ```
109
+
110
+ ### 核心方法
111
+
112
+ | 方法 | 说明 |
113
+ |------|------|
114
+ | `sendCode(phone, type?)` | 发送手机验证码 |
115
+ | `sendEmailCode(email, type?)` | 发送邮箱验证码 |
116
+ | `loginWithCode(phone, code)` | 手机验证码登录 |
117
+ | `loginWithEmailCode(email, code)` | 邮箱验证码登录 |
118
+ | `loginWithEmail(email, password)` | 邮箱密码登录 |
119
+ | `registerWithEmail(email, password, nickname?)` | 邮箱注册 |
120
+ | `verifyMFA(mfaToken, code)` | MFA 验证 |
121
+ | `getCurrentUser()` | 获取当前用户 |
122
+ | `updateProfile(updates)` | 更新用户资料 |
123
+ | `logout()` | 登出 |
124
+ | `logoutAll()` | 全设备登出 |
125
+
126
+ ### SSO 方法
127
+
128
+ | 方法 | 说明 |
129
+ |------|------|
130
+ | `configureSso(config)` | 配置 SSO |
131
+ | `loginWithSSO(options?)` | 发起 SSO 登录 |
132
+ | `isSSOCallback()` | 检测是否为 SSO 回调 |
133
+ | `handleSSOCallback()` | 处理 SSO 回调 |
134
+
135
+ ### 社交登录方法
136
+
137
+ | 方法 | 说明 |
138
+ |------|------|
139
+ | `getOAuthProviders()` | 获取 OAuth 提供商列表 |
140
+ | `startSocialLogin(provider, redirectUri?)` | 发起社交登录 |
141
+
142
+ ### 状态方法
143
+
144
+ | 方法 | 说明 |
145
+ |------|------|
146
+ | `isAuthenticated()` | 检查是否已登录 |
147
+ | `isTokenValid()` | 检查 Token 是否有效 |
148
+ | `getAccessToken()` | 获取 Token (异步,自动刷新) |
149
+ | `getAccessTokenSync()` | 获取 Token (同步) |
150
+ | `getCachedUser()` | 获取缓存的用户信息 |
151
+ | `onAuthStateChange(callback)` | 监听认证状态变更 |
152
+
153
+ ## 错误处理
154
+
155
+ ```typescript
156
+ import { UniAuthError, AuthErrorCode } from '@55387.ai/uniauth-client';
157
+
158
+ try {
159
+ await auth.loginWithCode(phone, code);
160
+ } catch (error) {
161
+ if (error instanceof UniAuthError) {
162
+ switch (error.code) {
163
+ case AuthErrorCode.MFA_REQUIRED:
164
+ // 需要 MFA 验证
165
+ break;
166
+ case AuthErrorCode.VERIFY_FAILED:
167
+ // 验证码错误
168
+ break;
169
+ default:
170
+ console.error(error.message);
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ ## License
177
+
178
+ MIT