seaverse-auth-sdk 0.1.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,399 @@
1
+ # @seaverseai/auth-sdk
2
+
3
+ SeaVerse 认证 SDK - 提供完整的用户认证功能和精美的登录注册 UI 组件
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@seaverseai/auth-sdk.svg)](https://www.npmjs.com/package/@seaverseai/auth-sdk)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## 功能特性
9
+
10
+ - 🔐 **邮箱登录/注册** - 传统用户名密码认证
11
+ - 🌐 **OAuth 第三方登录** - 支持 Google、Discord、GitHub
12
+ - 🎨 **精美登录 UI** - 开箱即用的登录弹窗组件
13
+ - 📱 **完全响应式** - 适配移动端/平板/桌面
14
+ - 🎭 **主题支持** - 暗色/亮色主题切换
15
+ - ⚡ **TypeScript** - 完整的类型定义
16
+ - 🔒 **安全** - 内置 CSRF 保护
17
+
18
+ ## 安装
19
+
20
+ ```bash
21
+ npm install @seaverseai/auth-sdk
22
+ ```
23
+
24
+ ## 快速开始
25
+
26
+ ### 1. API 客户端使用
27
+
28
+ ```typescript
29
+ import { SeaVerseBackendAPIClient } from '@seaverseai/auth-sdk';
30
+
31
+ const client = new SeaVerseBackendAPIClient({
32
+ baseURL: 'https://web.seaverse.dev',
33
+ });
34
+
35
+ // 注册
36
+ const registerResult = await client.register({
37
+ email: 'user@example.com',
38
+ password: 'password123',
39
+ });
40
+
41
+ // 登录
42
+ const loginResult = await client.login({
43
+ email: 'user@example.com',
44
+ password: 'password123',
45
+ });
46
+
47
+ // 保存 token
48
+ localStorage.setItem('token', loginResult.token);
49
+
50
+ // 获取当前用户
51
+ const user = await client.getCurrentUser();
52
+
53
+ // 登出
54
+ await client.logout();
55
+ ```
56
+
57
+ ### 2. 使用登录弹窗 UI
58
+
59
+ ```typescript
60
+ import { SeaVerseBackendAPIClient, createAuthModal } from '@seaverseai/auth-sdk';
61
+ import '@seaverseai/auth-sdk/src/auth-modal.css';
62
+
63
+ const client = new SeaVerseBackendAPIClient({
64
+ baseURL: 'https://web.seaverse.dev',
65
+ });
66
+
67
+ const authModal = createAuthModal({
68
+ client,
69
+ theme: 'dark', // 'dark' 或 'light'
70
+
71
+ onLoginSuccess: (token, user) => {
72
+ localStorage.setItem('token', token);
73
+ console.log('登录成功:', user);
74
+ },
75
+
76
+ onSignupSuccess: (token, user) => {
77
+ localStorage.setItem('token', token);
78
+ console.log('注册成功:', user);
79
+ },
80
+
81
+ onError: (error) => {
82
+ console.error('错误:', error.message);
83
+ },
84
+ });
85
+
86
+ // 显示登录界面
87
+ authModal.show('login');
88
+
89
+ // 显示注册界面
90
+ authModal.show('signup');
91
+ ```
92
+
93
+ ## OAuth 第三方登录
94
+
95
+ ### 1. 配置 OAuth
96
+
97
+ ```typescript
98
+ const authModal = createAuthModal({
99
+ client,
100
+
101
+ oauthConfig: {
102
+ google: {
103
+ clientId: 'YOUR_GOOGLE_CLIENT_ID',
104
+ redirectUri: 'https://yourdomain.com/auth/callback',
105
+ },
106
+ discord: {
107
+ clientId: 'YOUR_DISCORD_CLIENT_ID',
108
+ redirectUri: 'https://yourdomain.com/auth/callback',
109
+ },
110
+ github: {
111
+ clientId: 'YOUR_GITHUB_CLIENT_ID',
112
+ redirectUri: 'https://yourdomain.com/auth/callback',
113
+ },
114
+ },
115
+
116
+ onLoginSuccess: (token, user) => {
117
+ localStorage.setItem('token', token);
118
+ },
119
+ });
120
+
121
+ authModal.show('login');
122
+ ```
123
+
124
+ ### 2. 处理 OAuth 回调
125
+
126
+ 在你的回调页面(如 `/auth/callback`)添加:
127
+
128
+ ```typescript
129
+ import { SeaVerseBackendAPIClient, AuthModal } from '@seaverseai/auth-sdk';
130
+
131
+ const client = new SeaVerseBackendAPIClient({
132
+ baseURL: 'https://web.seaverse.dev',
133
+ });
134
+
135
+ // 自动处理 OAuth 回调
136
+ AuthModal.handleOAuthCallbackFromUrl(client, {
137
+ onLoginSuccess: (token, user) => {
138
+ localStorage.setItem('token', token);
139
+ window.location.href = '/dashboard';
140
+ },
141
+ onError: (error) => {
142
+ console.error('登录失败:', error);
143
+ window.location.href = '/';
144
+ },
145
+ });
146
+ ```
147
+
148
+ ### 3. 获取 OAuth Client ID
149
+
150
+ | 平台 | 配置地址 |
151
+ |------|---------|
152
+ | Google | https://console.cloud.google.com/ → 创建 OAuth 客户端 ID |
153
+ | Discord | https://discord.com/developers/applications → OAuth2 设置 |
154
+ | GitHub | https://github.com/settings/developers → OAuth Apps |
155
+
156
+ **重要**:在 OAuth 应用中配置的 Redirect URI 必须与代码中的 `redirectUri` 完全一致。
157
+
158
+ ## API 参考
159
+
160
+ ### 认证方法
161
+
162
+ ```typescript
163
+ // 注册
164
+ await client.register({ email, password, invitationCode? })
165
+
166
+ // 登录
167
+ await client.login({ email, password })
168
+
169
+ // 获取当前用户
170
+ await client.getCurrentUser()
171
+
172
+ // 登出
173
+ await client.logout()
174
+
175
+ // 忘记密码
176
+ await client.forgotPassword({ email })
177
+
178
+ // 重置密码
179
+ await client.resetPassword({ token, newPassword })
180
+ ```
181
+
182
+ ### OAuth 方法
183
+
184
+ ```typescript
185
+ // Google 登录
186
+ await client.googleCodeToToken({ code })
187
+
188
+ // Discord 登录
189
+ await client.discordCodeToToken({ code })
190
+
191
+ // GitHub 登录
192
+ await client.githubCodeToToken({ code })
193
+
194
+ // 解绑账号
195
+ await client.unlinkGoogle()
196
+ await client.unlinkDiscord()
197
+ await client.unlinkGithub()
198
+ ```
199
+
200
+ ### UI 方法
201
+
202
+ ```typescript
203
+ // 显示登录/注册
204
+ authModal.show('login') // 或 'signup'
205
+
206
+ // 隐藏弹窗
207
+ authModal.hide()
208
+
209
+ // 销毁弹窗
210
+ authModal.destroy()
211
+ ```
212
+
213
+ ## 类型定义
214
+
215
+ ```typescript
216
+ // 用户信息
217
+ interface User {
218
+ id?: string;
219
+ email?: string;
220
+ username?: string;
221
+ emailVerified?: boolean;
222
+ googleId?: string;
223
+ discordId?: string;
224
+ githubId?: string;
225
+ }
226
+
227
+ // 登录响应
228
+ interface LoginResponse {
229
+ token: string;
230
+ user: User;
231
+ }
232
+
233
+ // 注册响应
234
+ interface RegisterResponse {
235
+ success: boolean;
236
+ message: string;
237
+ userId: string;
238
+ }
239
+
240
+ // OAuth 配置
241
+ interface OAuthConfig {
242
+ google?: {
243
+ clientId: string;
244
+ redirectUri: string;
245
+ scope?: string; // 默认: 'openid email profile'
246
+ };
247
+ discord?: {
248
+ clientId: string;
249
+ redirectUri: string;
250
+ scope?: string; // 默认: 'identify email'
251
+ };
252
+ github?: {
253
+ clientId: string;
254
+ redirectUri: string;
255
+ scope?: string; // 默认: 'read:user user:email'
256
+ };
257
+ }
258
+ ```
259
+
260
+ ## 完整示例
261
+
262
+ ```html
263
+ <!DOCTYPE html>
264
+ <html>
265
+ <head>
266
+ <meta charset="UTF-8">
267
+ <title>登录示例</title>
268
+ <link rel="stylesheet" href="node_modules/@seaverseai/auth-sdk/src/auth-modal.css">
269
+ </head>
270
+ <body>
271
+ <h1>欢迎</h1>
272
+ <button id="loginBtn">登录</button>
273
+
274
+ <script type="module">
275
+ import { SeaVerseBackendAPIClient, createAuthModal } from '@seaverseai/auth-sdk';
276
+
277
+ const client = new SeaVerseBackendAPIClient({
278
+ baseURL: 'https://web.seaverse.dev',
279
+ });
280
+
281
+ const authModal = createAuthModal({
282
+ client,
283
+ theme: 'dark',
284
+
285
+ oauthConfig: {
286
+ google: {
287
+ clientId: 'YOUR_GOOGLE_CLIENT_ID',
288
+ redirectUri: window.location.origin + '/callback.html',
289
+ },
290
+ },
291
+
292
+ onLoginSuccess: (token, user) => {
293
+ localStorage.setItem('token', token);
294
+ alert('登录成功: ' + user.email);
295
+ },
296
+ });
297
+
298
+ document.getElementById('loginBtn').onclick = () => {
299
+ authModal.show('login');
300
+ };
301
+ </script>
302
+ </body>
303
+ </html>
304
+ ```
305
+
306
+ ## React 集成
307
+
308
+ ```tsx
309
+ import { useEffect, useState } from 'react';
310
+ import { SeaVerseBackendAPIClient, createAuthModal } from '@seaverseai/auth-sdk';
311
+ import '@seaverseai/auth-sdk/src/auth-modal.css';
312
+
313
+ function App() {
314
+ const [authModal, setAuthModal] = useState(null);
315
+
316
+ useEffect(() => {
317
+ const client = new SeaVerseBackendAPIClient({
318
+ baseURL: 'https://web.seaverse.dev',
319
+ });
320
+
321
+ const modal = createAuthModal({
322
+ client,
323
+ onLoginSuccess: (token) => {
324
+ localStorage.setItem('token', token);
325
+ },
326
+ });
327
+
328
+ setAuthModal(modal);
329
+ return () => modal?.destroy();
330
+ }, []);
331
+
332
+ return (
333
+ <button onClick={() => authModal?.show('login')}>
334
+ 登录
335
+ </button>
336
+ );
337
+ }
338
+ ```
339
+
340
+ ## 常见问题
341
+
342
+ ### OAuth redirect_uri_mismatch 错误?
343
+
344
+ 确保 OAuth 应用配置中的重定向 URI 与代码中完全一致(包括协议、域名、端口、路径)。
345
+
346
+ ### 如何自动携带 Token?
347
+
348
+ ```typescript
349
+ import { AuthFactory } from '@seaverseai/auth-sdk';
350
+
351
+ const client = new SeaVerseBackendAPIClient({
352
+ baseURL: 'https://web.seaverse.dev',
353
+ auth: AuthFactory.create({
354
+ type: 'jwt',
355
+ credentials: {
356
+ type: 'jwt',
357
+ token: localStorage.getItem('token'),
358
+ },
359
+ }),
360
+ });
361
+ ```
362
+
363
+ ### 如何只启用部分 OAuth 登录?
364
+
365
+ 只配置需要的平台即可,未配置的平台按钮点击时会提示错误:
366
+
367
+ ```typescript
368
+ oauthConfig: {
369
+ google: { /* ... */ },
370
+ // 不配置 discord 和 github
371
+ }
372
+ ```
373
+
374
+ ### 本地开发如何测试 OAuth?
375
+
376
+ 大多数 OAuth 提供商允许 `http://localhost:PORT` 作为开发环境的重定向 URI。
377
+
378
+ ## 开发
379
+
380
+ ```bash
381
+ # 安装依赖
382
+ pnpm install
383
+
384
+ # 构建
385
+ pnpm run build
386
+
387
+ # 测试
388
+ pnpm test
389
+ ```
390
+
391
+ ## License
392
+
393
+ MIT © SeaVerse Team
394
+
395
+ ## 技术支持
396
+
397
+ - 📧 Email: support@seaverse.com
398
+ - 🐛 Issues: https://github.com/seaverseai/sv-sdk/issues
399
+ - 📖 Docs: https://docs.seaverse.dev