@seaverse/auth-sdk 0.3.2 → 0.3.3

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,6 +126,11 @@ const loginResult = await client.login({
126
126
  // 保存token
127
127
  localStorage.setItem('token', loginResult.token);
128
128
 
129
+ // ⚠️ 注意:邀请码自动跳转
130
+ // 如果服务器返回 INVITE_CODE_REQUIRED 错误且包含 redirectUrl,
131
+ // SDK 会自动将用户重定向到该 URL(通常是邀请码输入页面)
132
+ // 无需额外处理,页面会自动跳转
133
+
129
134
  // 获取当前用户信息
130
135
  const user = await client.getCurrentUser();
131
136
  console.log('User:', user);
@@ -591,7 +596,14 @@ if (verifyToken) {
591
596
 
592
597
  #### 邀请码绑定(账户激活)
593
598
 
594
- 当使用外部邮箱注册或 OAuth 登录但未提供邀请码时,会创建临时账户并需要后续绑定邀请码激活。后端会重定向到 `frontend_url?error_code=INVITE_CODE_REQUIRED&user_id=xxx&email=xxx`。
599
+ 当使用外部邮箱注册或 OAuth 登录但未提供邀请码时,会创建临时账户并需要后续绑定邀请码激活。
600
+
601
+ **自动跳转机制**:
602
+ - 当 `login()` 返回 `INVITE_CODE_REQUIRED` 错误且包含 `redirectUrl` 时,SDK 会自动将页面重定向到该 URL
603
+ - redirectUrl 通常包含 `error_code=INVITE_CODE_REQUIRED&user_id=xxx&email=xxx` 参数
604
+ - 无需手动处理跳转逻辑,SDK 会自动完成
605
+
606
+ 对于其他场景(如 OAuth 登录后的重定向),后端会直接重定向到 `frontend_url?error_code=INVITE_CODE_REQUIRED&user_id=xxx&email=xxx`。
595
607
 
596
608
  **方式一:使用 AuthModal 自动处理(推荐)**
597
609
 
@@ -887,7 +899,7 @@ SDK支持以下环境:
887
899
  | 方法 | 参数 | 返回值 | 说明 |
888
900
  |------|------|--------|------|
889
901
  | `register()` | `{ email, password, username?, invitation_code?, frontend_url? }` | `RegisterResponse` | 注册新用户,frontend_url 为邮箱验证链接的前端URL,默认为 window.location.href |
890
- | `login()` | `{ email, password, frontend_url? }` | `LoginResponse` | 用户登录,frontend_url 用于未验证邮箱时发送验证邮件,默认为 https://seaverse.ai/ |
902
+ | `login()` | `{ email, password, frontend_url? }` | `LoginResponse` | 用户登录,frontend_url 用于未验证邮箱时发送验证邮件,默认为 https://seaverse.ai/。⚠️ 如果返回 INVITE_CODE_REQUIRED 且包含 redirectUrl,会自动跳转 |
891
903
  | `getCurrentUser()` | - | `User` | 获取当前用户 |
892
904
  | `logout()` | - | `SuccessResponse` | 登出 |
893
905
  | `verifyEmail()` | `verifyToken: string` | `EmailVerificationResponse` | 验证邮箱并返回自动登录 token |
@@ -1088,6 +1100,7 @@ ErrorCode.PASSWORD_TOO_WEAK // 密码强度不够
1088
1100
  // 邀请码相关错误
1089
1101
  ErrorCode.INVALID_INVITATION_CODE // 无效的邀请码
1090
1102
  ErrorCode.INVITATION_REQUIRED // 需要邀请码
1103
+ ErrorCode.INVITE_CODE_REQUIRED // 需要邀请码(会自动跳转到 redirectUrl)
1091
1104
 
1092
1105
  // Token 相关错误
1093
1106
  ErrorCode.INVALID_TOKEN // 无效的 token
package/dist/index.cjs CHANGED
@@ -1157,6 +1157,7 @@ exports.ErrorCode = void 0;
1157
1157
  // Invitation errors
1158
1158
  ErrorCode["INVALID_INVITATION_CODE"] = "INVALID_INVITATION_CODE";
1159
1159
  ErrorCode["INVITATION_REQUIRED"] = "INVITATION_REQUIRED";
1160
+ ErrorCode["INVITE_CODE_REQUIRED"] = "INVITE_CODE_REQUIRED";
1160
1161
  // Token errors
1161
1162
  ErrorCode["INVALID_TOKEN"] = "INVALID_TOKEN";
1162
1163
  ErrorCode["TOKEN_EXPIRED"] = "TOKEN_EXPIRED";
@@ -1346,17 +1347,36 @@ class SeaVerseBackendAPIClient {
1346
1347
  },
1347
1348
  ...options,
1348
1349
  };
1349
- const response = await this.httpClient.request(config);
1350
- // 兼容两种响应格式:
1351
- // 1. { data: { token, user }, success: true } (新格式)
1352
- // 2. { token, user } (旧格式)
1353
- const responseData = response.data;
1354
- if (responseData.data && typeof responseData.data === 'object') {
1355
- // 新格式: 解包 data 字段
1356
- return responseData.data;
1350
+ try {
1351
+ const response = await this.httpClient.request(config);
1352
+ // 兼容两种响应格式:
1353
+ // 1. { data: { token, user }, success: true } (新格式)
1354
+ // 2. { token, user } (旧格式)
1355
+ const responseData = response.data;
1356
+ if (responseData.data && typeof responseData.data === 'object') {
1357
+ // 新格式: 解包 data 字段
1358
+ return responseData.data;
1359
+ }
1360
+ // 旧格式: 直接返回
1361
+ return responseData;
1362
+ }
1363
+ catch (error) {
1364
+ // Handle error response
1365
+ const errorResponse = error.response?.data;
1366
+ // Check if error is INVITE_CODE_REQUIRED and has redirectUrl
1367
+ if (errorResponse?.code === 'INVITE_CODE_REQUIRED' && errorResponse?.data?.redirectUrl) {
1368
+ const redirectUrl = errorResponse.data.redirectUrl;
1369
+ // Only redirect in browser environment
1370
+ if (typeof window !== 'undefined') {
1371
+ console.log('[AuthSDK] INVITE_CODE_REQUIRED detected, redirecting to:', redirectUrl);
1372
+ window.location.href = redirectUrl;
1373
+ // Return a pending promise to prevent further execution
1374
+ return new Promise(() => { });
1375
+ }
1376
+ }
1377
+ // Re-throw the error if it's not INVITE_CODE_REQUIRED or no redirectUrl
1378
+ throw error;
1357
1379
  }
1358
- // 旧格式: 直接返回
1359
- return responseData;
1360
1380
  }
1361
1381
  /**
1362
1382
  * Get current user