@seaverse/auth-sdk 0.3.2 → 0.3.4
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 +15 -2
- package/dist/index.cjs +41 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +14 -2
- package/dist/index.js +41 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
|
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,47 @@ class SeaVerseBackendAPIClient {
|
|
|
1346
1347
|
},
|
|
1347
1348
|
...options,
|
|
1348
1349
|
};
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
//
|
|
1356
|
-
|
|
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
|
+
// ⚠️ 检查响应体中的 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
|
+
}
|
|
1367
|
+
if (responseData.data && typeof responseData.data === 'object') {
|
|
1368
|
+
// 新格式: 解包 data 字段
|
|
1369
|
+
return responseData.data;
|
|
1370
|
+
}
|
|
1371
|
+
// 旧格式: 直接返回
|
|
1372
|
+
return responseData;
|
|
1373
|
+
}
|
|
1374
|
+
catch (error) {
|
|
1375
|
+
// Handle HTTP error response (4xx, 5xx status codes)
|
|
1376
|
+
const errorResponse = error.response?.data;
|
|
1377
|
+
// Check if error is INVITE_CODE_REQUIRED and has redirectUrl
|
|
1378
|
+
if (errorResponse?.code === 'INVITE_CODE_REQUIRED' && errorResponse?.data?.redirectUrl) {
|
|
1379
|
+
const redirectUrl = errorResponse.data.redirectUrl;
|
|
1380
|
+
// Only redirect in browser environment
|
|
1381
|
+
if (typeof window !== 'undefined') {
|
|
1382
|
+
console.log('[AuthSDK] INVITE_CODE_REQUIRED detected in error, redirecting to:', redirectUrl);
|
|
1383
|
+
window.location.href = redirectUrl;
|
|
1384
|
+
// Return a pending promise to prevent further execution
|
|
1385
|
+
return new Promise(() => { });
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
// Re-throw the error if it's not INVITE_CODE_REQUIRED or no redirectUrl
|
|
1389
|
+
throw error;
|
|
1357
1390
|
}
|
|
1358
|
-
// 旧格式: 直接返回
|
|
1359
|
-
return responseData;
|
|
1360
1391
|
}
|
|
1361
1392
|
/**
|
|
1362
1393
|
* Get current user
|