@seaverse/auth-sdk 0.2.1 → 0.2.2

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 CHANGED
@@ -126,44 +126,73 @@ await client.resetPassword({
126
126
  });
127
127
  ```
128
128
 
129
- ### 3. OAuth 第三方登录
129
+ ### 3. OAuth 第三方登录 (Backend Proxy Mode)
130
130
 
131
- #### Google 登录
131
+ SDK 使用 Backend Proxy Mode,Client Secret 永不暴露给前端,安全性更高。
132
+
133
+ **优势**:
134
+ - ✅ Client Secret 从不暴露给前端
135
+ - ✅ 支持任意开发者域名(无需在 OAuth 平台配置)
136
+ - ✅ 内置 CSRF 防护
137
+ - ✅ 零配置,开箱即用
138
+
139
+ **工作流程**:
140
+ 1. 前端调用 `{provider}Authorize()` 获取 OAuth URL
141
+ 2. 前端重定向用户到 OAuth 提供商
142
+ 3. 用户授权后,OAuth 提供商回调到固定的 account-hub URL
143
+ 4. account-hub 处理 OAuth,创建 JWT token
144
+ 5. account-hub 302 重定向到 `return_url?token=xxx`
145
+ 6. 前端从 URL 提取 token 并存储
146
+
147
+ #### 使用示例
148
+
149
+ **方式1:使用默认 return_url(当前页面)**
132
150
 
133
151
  ```typescript
134
- // 步骤1: 获取Google授权码后,交换token
135
- const result = await client.googleCodeToToken({
136
- code: 'google-auth-code',
137
- redirectUri: 'https://yourdomain.com/auth/callback',
138
- });
152
+ // Google 登录
153
+ const { authorize_url } = await client.googleAuthorize();
154
+ window.location.href = authorize_url;
139
155
 
140
- localStorage.setItem('token', result.token);
156
+ // Discord 登录
157
+ const { authorize_url } = await client.discordAuthorize();
158
+ window.location.href = authorize_url;
141
159
 
142
- // 解绑Google账号
143
- await client.unlinkGoogle();
160
+ // GitHub 登录
161
+ const { authorize_url } = await client.githubAuthorize();
162
+ window.location.href = authorize_url;
144
163
  ```
145
164
 
146
- #### Discord 登录
165
+ **方式2:自定义 return_url**
147
166
 
148
167
  ```typescript
149
- const result = await client.discordCodeToToken({
150
- code: 'discord-auth-code',
151
- redirectUri: 'https://yourdomain.com/auth/callback',
168
+ // 登录后跳转到 dashboard
169
+ const { authorize_url } = await client.googleAuthorize({
170
+ return_url: 'https://mygame.com/dashboard'
152
171
  });
172
+ window.location.href = authorize_url;
173
+ ```
153
174
 
154
- // 解绑Discord账号
155
- await client.unlinkDiscord();
175
+ **在回调页面提取 token**:
176
+
177
+ ```typescript
178
+ // URL: https://mygame.com/?token=eyJhbGc...
179
+ const token = new URLSearchParams(window.location.search).get('token');
180
+ if (token) {
181
+ localStorage.setItem('token', token);
182
+ // 登录成功,跳转或更新 UI
183
+ }
156
184
  ```
157
185
 
158
- #### GitHub 登录
186
+ #### OAuth 账号解绑
159
187
 
160
188
  ```typescript
161
- const result = await client.githubCodeToToken({
162
- code: 'github-auth-code',
163
- redirectUri: 'https://yourdomain.com/auth/callback',
164
- });
189
+ // 解绑 Google 账号
190
+ await client.unlinkGoogle();
165
191
 
166
- // 解绑GitHub账号
192
+ // 解绑 Discord 账号
193
+ await client.unlinkDiscord();
194
+
195
+ // 解绑 GitHub 账号
167
196
  await client.unlinkGithub();
168
197
  ```
169
198
 
@@ -189,7 +218,7 @@ const client = new SeaVerseBackendAPIClient({
189
218
  environment: 'production',
190
219
  });
191
220
 
192
- // 创建登录弹窗(深色主题)
221
+ // 创建登录弹窗
193
222
  const authModal = new AuthModal({
194
223
  client,
195
224
  theme: 'dark', // 'dark' | 'light' - 默认为 'dark'
@@ -211,20 +240,12 @@ const authModal = new AuthModal({
211
240
  console.error('认证错误:', error.message);
212
241
  },
213
242
 
214
- // OAuth配置(可选)
215
- oauthConfig: {
216
- google: {
217
- clientId: 'YOUR_GOOGLE_CLIENT_ID', // 必填
218
- redirectUri: window.location.origin, // 可选,默认为 window.location.origin
219
- },
220
- discord: {
221
- clientId: 'YOUR_DISCORD_CLIENT_ID', // 必填
222
- redirectUri: window.location.origin, // 可选,默认为 window.location.origin
223
- },
224
- github: {
225
- clientId: 'YOUR_GITHUB_CLIENT_ID', // 必填
226
- redirectUri: window.location.origin, // 可选,默认为 window.location.origin
227
- },
243
+ // OAuth 配置(可选)
244
+ returnUrl: 'https://mygame.com/', // OAuth 登录后返回的 URL,可选,默认为 window.location.origin
245
+ enableOAuth: {
246
+ google: true, // 启用 Google 登录
247
+ discord: true, // 启用 Discord 登录
248
+ github: true, // 启用 GitHub 登录
228
249
  },
229
250
  });
230
251
 
@@ -239,64 +260,98 @@ authModal.hide();
239
260
 
240
261
  // 销毁弹窗
241
262
  authModal.destroy();
263
+ ```
264
+
242
265
  #### OAuth 配置说明
243
266
 
244
- `oauthConfig` 参数是**完全可选的**:
267
+ `enableOAuth` 参数是**完全可选的**:
245
268
 
246
- - 如果**不提供** `oauthConfig`,则不会显示任何第三方登录按钮
247
- - 如果**部分配置**(如只配置 Google),则只显示已配置的按钮
269
+ - 如果**不提供** `enableOAuth`,则不会显示任何第三方登录按钮
270
+ - 如果**部分配置**(如只启用 Google),则只显示已启用的按钮
248
271
  - 如果**完整配置**所有平台,则显示所有第三方登录按钮
249
272
 
250
273
  **配置字段说明**:
251
- - `clientId`:**必填** - OAuth 应用的客户端 ID
252
- - `redirectUri`:**可选** - OAuth 回调地址,不填则默认为 `window.location.origin`
253
- - `scope`:**可选** - OAuth 权限范围,每个平台有默认值
274
+ - `returnUrl`:**可选** - OAuth 登录后返回的 URL,不填则默认为 `window.location.origin`
275
+ - `enableOAuth.google`:是否启用 Google 登录
276
+ - `enableOAuth.discord`:是否启用 Discord 登录
277
+ - `enableOAuth.github`:是否启用 GitHub 登录
254
278
 
255
279
  ```typescript
256
280
  // 示例1:无OAuth按钮
257
- const client1 = new SeaVerseBackendAPIClient({ appId: 'your app id' });
258
281
  const authModal1 = new AuthModal({
259
- client: client1,
282
+ client,
260
283
  theme: 'dark',
261
- // 不传 oauthConfig,不显示任何OAuth按钮
284
+ // 不传 enableOAuth,不显示任何OAuth按钮
262
285
  });
263
286
 
264
287
  // 示例2:只显示Google登录
265
- const client2 = new SeaVerseBackendAPIClient({ appId: 'your app id' });
266
288
  const authModal2 = new AuthModal({
267
- client: client2,
289
+ client,
268
290
  theme: 'light',
269
- oauthConfig: {
270
- google: {
271
- clientId: 'YOUR_GOOGLE_CLIENT_ID',
272
- // redirectUri 可选,不填则默认为 window.location.origin
273
- },
274
- // Discord 和 GitHub 未配置,不会显示这些按钮
291
+ returnUrl: 'https://mygame.com/dashboard',
292
+ enableOAuth: {
293
+ google: true,
294
+ // Discord GitHub 未启用,不会显示这些按钮
275
295
  },
276
296
  });
277
297
 
278
- // 示例3:显示所有OAuth按钮(自定义 redirectUri)
279
- const client3 = new SeaVerseBackendAPIClient({ appId: 'your app id' });
298
+ // 示例3:显示所有OAuth按钮
280
299
  const authModal3 = new AuthModal({
281
- client: client3,
300
+ client,
282
301
  theme: 'dark',
283
- oauthConfig: {
284
- google: {
285
- clientId: '...',
286
- redirectUri: 'https://yourdomain.com/auth/callback' // 自定义回调地址
287
- },
288
- discord: {
289
- clientId: '...'
290
- // redirectUri 可选,不填则默认为 window.location.origin
291
- },
292
- github: {
293
- clientId: '...'
294
- // redirectUri 可选,不填则默认为 window.location.origin
295
- },
302
+ enableOAuth: {
303
+ google: true,
304
+ discord: true,
305
+ github: true,
296
306
  },
297
307
  });
298
308
  ```
299
309
 
310
+ #### 处理 OAuth 回调
311
+
312
+ 在 Backend Proxy Mode 下,OAuth 登录后会重定向到 `returnUrl?token=xxx`。在页面加载时检查并处理token:
313
+
314
+ ```typescript
315
+ // 在页面加载时自动处理 OAuth 回调
316
+ const result = AuthModal.handleOAuthCallback({
317
+ client,
318
+ onLoginSuccess: (token) => {
319
+ localStorage.setItem('token', token);
320
+ console.log('OAuth 登录成功');
321
+
322
+ // 现在可以直接调用需要认证的接口
323
+ // handleOAuthCallback 已自动调用 client.setToken()
324
+ client.getCurrentUser()
325
+ .then(user => console.log('用户信息:', user));
326
+ },
327
+ });
328
+
329
+ if (result) {
330
+ console.log('处理了 OAuth 回调,token:', result.token);
331
+ }
332
+ ```
333
+
334
+ **重要说明**:
335
+ - `handleOAuthCallback()` 会自动调用 `client.setToken(token)` 来更新 client 的认证配置
336
+ - 这意味着在 `onLoginSuccess` 回调之后,所有需要认证的 API(如 `getCurrentUser()`、`logout()` 等)都会自动带上 `Authorization` header
337
+
338
+ #### 手动设置 Token
339
+
340
+ 如果你不使用 `AuthModal.handleOAuthCallback()`,也可以手动设置 token:
341
+
342
+ ```typescript
343
+ // 从 URL 提取 token
344
+ const token = new URLSearchParams(window.location.search).get('token');
345
+ if (token) {
346
+ // 手动设置 token
347
+ client.setToken(token);
348
+ localStorage.setItem('token', token);
349
+
350
+ // 现在可以调用需要认证的接口
351
+ const user = await client.getCurrentUser();
352
+ }
353
+ ```
354
+
300
355
  ### 5. 容器管理
301
356
 
302
357
  ```typescript
@@ -462,15 +517,16 @@ SDK支持以下环境:
462
517
  | `logout()` | - | `SuccessResponse` | 登出 |
463
518
  | `forgotPassword()` | `{ email }` | `SuccessResponse` | 忘记密码 |
464
519
  | `resetPassword()` | `{ token, new_password }` | `SuccessResponse` | 重置密码 |
520
+ | `setToken()` | `token: string` | `void` | 设置认证 token(OAuth 登录后使用) |
465
521
  | `getApiServiceToken()` | - | `ApiServiceTokenResponse` | 获取API Token |
466
522
 
467
523
  ### OAuth相关
468
524
 
469
525
  | 方法 | 参数 | 返回值 | 说明 |
470
526
  |------|------|--------|------|
471
- | `googleCodeToToken()` | `{ code, redirectUri }` | `OAuthTokenResponse` | Google登录 |
472
- | `discordCodeToToken()` | `{ code, redirectUri }` | `OAuthTokenResponse` | Discord登录 |
473
- | `githubCodeToToken()` | `{ code, redirectUri }` | `OAuthTokenResponse` | GitHub登录 |
527
+ | `googleAuthorize()` | `{ return_url? }` | `OAuthAuthorizeResponse` | Google OAuth 授权 URL |
528
+ | `discordAuthorize()` | `{ return_url? }` | `OAuthAuthorizeResponse` | Discord OAuth 授权 URL |
529
+ | `githubAuthorize()` | `{ return_url? }` | `OAuthAuthorizeResponse` | GitHub OAuth 授权 URL |
474
530
  | `unlinkGoogle()` | - | `SuccessResponse` | 解绑Google |
475
531
  | `unlinkDiscord()` | - | `SuccessResponse` | 解绑Discord |
476
532
  | `unlinkGithub()` | - | `SuccessResponse` | 解绑GitHub |
@@ -547,25 +603,11 @@ interface AuthModalOptions {
547
603
  onLoginSuccess?: (token: string, user: any) => void;
548
604
  onSignupSuccess?: (token: string, user: any) => void;
549
605
  onError?: (error: Error) => void;
550
- oauthConfig?: OAuthConfig;
551
- }
552
-
553
- // OAuth配置
554
- interface OAuthConfig {
555
- google?: {
556
- clientId: string; // 必填
557
- redirectUri?: string; // 可选,默认为 window.location.origin
558
- scope?: string; // 可选,默认为 'openid email profile'
559
- };
560
- discord?: {
561
- clientId: string; // 必填
562
- redirectUri?: string; // 可选,默认为 window.location.origin
563
- scope?: string; // 可选,默认为 'identify email'
564
- };
565
- github?: {
566
- clientId: string; // 必填
567
- redirectUri?: string; // 可选,默认为 window.location.origin
568
- scope?: string; // 可选,默认为 'read:user user:email'
606
+ returnUrl?: string; // OAuth 登录后返回的 URL,可选,默认为 window.location.origin
607
+ enableOAuth?: {
608
+ google?: boolean; // 启用 Google 登录
609
+ discord?: boolean; // 启用 Discord 登录
610
+ github?: boolean; // 启用 GitHub 登录
569
611
  };
570
612
  }
571
613
  ```