@seaverse/auth-sdk 0.3.3 → 0.3.5

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
@@ -353,6 +353,7 @@ const authModal = new AuthModal({
353
353
 
354
354
  **配置字段说明**:
355
355
  - `returnUrl`:**可选** - OAuth 登录后返回的 URL,不填则默认为 `window.location.href`
356
+ - `oauthDesktopURL`:**可选** - 桌面应用 OAuth 回调 URL,如果提供则优先使用此 URL(优先级:`oauthDesktopURL` > `returnUrl` > `window.location.href`)
356
357
  - `enableOAuth.google`:是否启用 Google 登录
357
358
  - `enableOAuth.discord`:是否启用 Discord 登录
358
359
  - `enableOAuth.github`:是否启用 GitHub 登录
@@ -386,6 +387,19 @@ const authModal3 = new AuthModal({
386
387
  github: true,
387
388
  },
388
389
  });
390
+
391
+ // 示例4:桌面应用 OAuth 回调(优先级最高)
392
+ const authModal4 = new AuthModal({
393
+ client,
394
+ theme: 'dark',
395
+ oauthDesktopURL: 'myapp://oauth/callback', // 桌面应用自定义协议 URL
396
+ enableOAuth: {
397
+ google: true,
398
+ discord: true,
399
+ github: true,
400
+ },
401
+ });
402
+ // 登录成功后会跳转到 myapp://oauth/callback?token=xxx
389
403
  ```
390
404
 
391
405
  #### 处理 OAuth 回调
package/dist/index.cjs CHANGED
@@ -1353,6 +1353,17 @@ class SeaVerseBackendAPIClient {
1353
1353
  // 1. { data: { token, user }, success: true } (新格式)
1354
1354
  // 2. { token, user } (旧格式)
1355
1355
  const responseData = response.data;
1356
+ // ⚠️ 检查响应体中的 INVITE_CODE_REQUIRED 错误 (HTTP 200 + 错误响应体)
1357
+ if (responseData?.code === 'INVITE_CODE_REQUIRED' && responseData?.data?.redirectUrl) {
1358
+ const redirectUrl = responseData.data.redirectUrl;
1359
+ // Only redirect in browser environment
1360
+ if (typeof window !== 'undefined') {
1361
+ console.log('[AuthSDK] INVITE_CODE_REQUIRED detected in response, redirecting to:', redirectUrl);
1362
+ window.location.href = redirectUrl;
1363
+ // Return a pending promise to prevent further execution
1364
+ return new Promise(() => { });
1365
+ }
1366
+ }
1356
1367
  if (responseData.data && typeof responseData.data === 'object') {
1357
1368
  // 新格式: 解包 data 字段
1358
1369
  return responseData.data;
@@ -1361,14 +1372,14 @@ class SeaVerseBackendAPIClient {
1361
1372
  return responseData;
1362
1373
  }
1363
1374
  catch (error) {
1364
- // Handle error response
1375
+ // Handle HTTP error response (4xx, 5xx status codes)
1365
1376
  const errorResponse = error.response?.data;
1366
1377
  // Check if error is INVITE_CODE_REQUIRED and has redirectUrl
1367
1378
  if (errorResponse?.code === 'INVITE_CODE_REQUIRED' && errorResponse?.data?.redirectUrl) {
1368
1379
  const redirectUrl = errorResponse.data.redirectUrl;
1369
1380
  // Only redirect in browser environment
1370
1381
  if (typeof window !== 'undefined') {
1371
- console.log('[AuthSDK] INVITE_CODE_REQUIRED detected, redirecting to:', redirectUrl);
1382
+ console.log('[AuthSDK] INVITE_CODE_REQUIRED detected in error, redirecting to:', redirectUrl);
1372
1383
  window.location.href = redirectUrl;
1373
1384
  // Return a pending promise to prevent further execution
1374
1385
  return new Promise(() => { });
@@ -3739,7 +3750,8 @@ class AuthModal {
3739
3750
  async startOAuthFlow(provider) {
3740
3751
  try {
3741
3752
  // Get the return URL (where user should be redirected after OAuth)
3742
- const return_url = this.options.returnUrl || window.location.href;
3753
+ // Priority: oauthDesktopURL > returnUrl > window.location.href
3754
+ const return_url = this.options.oauthDesktopURL || this.options.returnUrl || window.location.href;
3743
3755
  // Call backend to get OAuth authorization URL
3744
3756
  let authorizeUrl;
3745
3757
  switch (provider) {
@@ -3774,6 +3786,7 @@ class AuthModal {
3774
3786
  *
3775
3787
  * In Backend Proxy Mode, the backend redirects to returnUrl with token in query param.
3776
3788
  * This static method checks if current URL has a token and processes it.
3789
+ * If oauthDesktopURL is provided, it will redirect to that URL with the token.
3777
3790
  */
3778
3791
  static handleOAuthCallback(options) {
3779
3792
  const urlParams = new URLSearchParams(window.location.search);
@@ -3781,6 +3794,19 @@ class AuthModal {
3781
3794
  if (!token) {
3782
3795
  return null; // Not an OAuth callback
3783
3796
  }
3797
+ // If oauthDesktopURL is provided, redirect to it with the token
3798
+ if (options.oauthDesktopURL) {
3799
+ // Construct the desktop URL with token parameter
3800
+ const desktopUrl = new URL(options.oauthDesktopURL);
3801
+ desktopUrl.searchParams.set('token', token);
3802
+ // Redirect to desktop app
3803
+ window.location.href = desktopUrl.toString();
3804
+ // Return result (though redirect will happen immediately)
3805
+ return {
3806
+ success: true,
3807
+ token,
3808
+ };
3809
+ }
3784
3810
  // Clean up URL (remove token from query string)
3785
3811
  const url = new URL(window.location.href);
3786
3812
  url.searchParams.delete('token');