@seaverse/auth-sdk 0.1.1 → 0.1.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 +24 -6
- package/dist/index.cjs +143 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +97 -2
- package/dist/index.js +141 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ SeaVerse 认证 SDK - 提供完整的用户认证功能和精美的登录注册
|
|
|
14
14
|
- 🎭 **主题支持** - 暗色/亮色主题切换
|
|
15
15
|
- ⚡ **TypeScript** - 完整的类型定义
|
|
16
16
|
- 🔒 **安全** - 内置 CSRF 保护
|
|
17
|
+
- 🌍 **多环境支持** - 一键切换生产/测试/开发环境
|
|
17
18
|
|
|
18
19
|
## 安装
|
|
19
20
|
|
|
@@ -25,13 +26,30 @@ npm install @seaverseai/auth-sdk
|
|
|
25
26
|
|
|
26
27
|
### 1. API 客户端使用
|
|
27
28
|
|
|
29
|
+
#### 方式 A:指定环境(推荐)
|
|
30
|
+
|
|
28
31
|
```typescript
|
|
29
32
|
import { SeaVerseBackendAPIClient } from '@seaverseai/auth-sdk';
|
|
30
33
|
|
|
34
|
+
// 正式环境
|
|
31
35
|
const client = new SeaVerseBackendAPIClient({
|
|
32
|
-
|
|
36
|
+
environment: 'production', // 'production' | 'staging' | 'development' | 'local'
|
|
33
37
|
});
|
|
34
38
|
|
|
39
|
+
// 或者测试环境
|
|
40
|
+
const client = new SeaVerseBackendAPIClient({
|
|
41
|
+
environment: 'staging',
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### 方式 B:自动检测环境
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { SeaVerseBackendAPIClient } from '@seaverseai/auth-sdk';
|
|
49
|
+
|
|
50
|
+
// SDK 会根据当前域名自动选择环境
|
|
51
|
+
const client = new SeaVerseBackendAPIClient();
|
|
52
|
+
|
|
35
53
|
// 注册
|
|
36
54
|
const registerResult = await client.register({
|
|
37
55
|
email: 'user@example.com',
|
|
@@ -61,7 +79,7 @@ import { SeaVerseBackendAPIClient, createAuthModal } from '@seaverseai/auth-sdk'
|
|
|
61
79
|
import '@seaverseai/auth-sdk/src/auth-modal.css';
|
|
62
80
|
|
|
63
81
|
const client = new SeaVerseBackendAPIClient({
|
|
64
|
-
baseURL: 'https://
|
|
82
|
+
baseURL: 'https://account-hub.sg.seaverse.dev',
|
|
65
83
|
});
|
|
66
84
|
|
|
67
85
|
const authModal = createAuthModal({
|
|
@@ -129,7 +147,7 @@ authModal.show('login');
|
|
|
129
147
|
import { SeaVerseBackendAPIClient, AuthModal } from '@seaverseai/auth-sdk';
|
|
130
148
|
|
|
131
149
|
const client = new SeaVerseBackendAPIClient({
|
|
132
|
-
baseURL: 'https://
|
|
150
|
+
baseURL: 'https://account-hub.sg.seaverse.dev',
|
|
133
151
|
});
|
|
134
152
|
|
|
135
153
|
// 自动处理 OAuth 回调
|
|
@@ -275,7 +293,7 @@ interface OAuthConfig {
|
|
|
275
293
|
import { SeaVerseBackendAPIClient, createAuthModal } from '@seaverseai/auth-sdk';
|
|
276
294
|
|
|
277
295
|
const client = new SeaVerseBackendAPIClient({
|
|
278
|
-
baseURL: 'https://
|
|
296
|
+
baseURL: 'https://account-hub.sg.seaverse.dev',
|
|
279
297
|
});
|
|
280
298
|
|
|
281
299
|
const authModal = createAuthModal({
|
|
@@ -315,7 +333,7 @@ function App() {
|
|
|
315
333
|
|
|
316
334
|
useEffect(() => {
|
|
317
335
|
const client = new SeaVerseBackendAPIClient({
|
|
318
|
-
baseURL: 'https://
|
|
336
|
+
baseURL: 'https://account-hub.sg.seaverse.dev',
|
|
319
337
|
});
|
|
320
338
|
|
|
321
339
|
const modal = createAuthModal({
|
|
@@ -349,7 +367,7 @@ function App() {
|
|
|
349
367
|
import { AuthFactory } from '@seaverseai/auth-sdk';
|
|
350
368
|
|
|
351
369
|
const client = new SeaVerseBackendAPIClient({
|
|
352
|
-
baseURL: 'https://
|
|
370
|
+
baseURL: 'https://account-hub.sg.seaverse.dev',
|
|
353
371
|
auth: AuthFactory.create({
|
|
354
372
|
type: 'jwt',
|
|
355
373
|
credentials: {
|
package/dist/index.cjs
CHANGED
|
@@ -1026,6 +1026,112 @@ class AuthFactory {
|
|
|
1026
1026
|
}
|
|
1027
1027
|
}
|
|
1028
1028
|
|
|
1029
|
+
/**
|
|
1030
|
+
* Environment Configuration for SeaVerse SDK
|
|
1031
|
+
* 环境配置模块 - 支持多环境部署
|
|
1032
|
+
*/
|
|
1033
|
+
/**
|
|
1034
|
+
* 预定义的环境配置
|
|
1035
|
+
*
|
|
1036
|
+
* 使用说明:
|
|
1037
|
+
* - production: 正式生产环境
|
|
1038
|
+
* - staging: 预发布环境(当前默认)
|
|
1039
|
+
* - development: 开发测试环境
|
|
1040
|
+
* - local: 本地开发环境
|
|
1041
|
+
*/
|
|
1042
|
+
const ENVIRONMENT_CONFIGS = {
|
|
1043
|
+
production: {
|
|
1044
|
+
name: 'production',
|
|
1045
|
+
baseURL: 'https://api.seaverse.com',
|
|
1046
|
+
wsURL: 'wss://api.seaverse.com',
|
|
1047
|
+
isProduction: true,
|
|
1048
|
+
},
|
|
1049
|
+
staging: {
|
|
1050
|
+
name: 'staging',
|
|
1051
|
+
baseURL: 'https://account-hub.sg.seaverse.dev',
|
|
1052
|
+
wsURL: 'wss://account-hub.sg.seaverse.dev',
|
|
1053
|
+
isProduction: false,
|
|
1054
|
+
},
|
|
1055
|
+
development: {
|
|
1056
|
+
name: 'development',
|
|
1057
|
+
baseURL: 'https://api-dev.seaverse.dev',
|
|
1058
|
+
wsURL: 'wss://api-dev.seaverse.dev',
|
|
1059
|
+
isProduction: false,
|
|
1060
|
+
},
|
|
1061
|
+
local: {
|
|
1062
|
+
name: 'local',
|
|
1063
|
+
baseURL: 'http://localhost:8001',
|
|
1064
|
+
wsURL: 'ws://localhost:8000',
|
|
1065
|
+
isProduction: false,
|
|
1066
|
+
},
|
|
1067
|
+
};
|
|
1068
|
+
/**
|
|
1069
|
+
* 根据当前运行环境自动检测应该使用的环境配置
|
|
1070
|
+
*
|
|
1071
|
+
* 检测规则:
|
|
1072
|
+
* 1. localhost/127.0.0.1 → local
|
|
1073
|
+
* 2. 包含 -dev 或 .dev → development
|
|
1074
|
+
* 3. 包含 staging → staging
|
|
1075
|
+
* 4. 其他 → production (默认)
|
|
1076
|
+
*
|
|
1077
|
+
* @returns 检测到的环境类型
|
|
1078
|
+
*/
|
|
1079
|
+
function detectEnvironment() {
|
|
1080
|
+
// Node.js 环境或非浏览器环境,默认返回 staging(安全起见)
|
|
1081
|
+
if (typeof window === 'undefined' || typeof window.location === 'undefined') {
|
|
1082
|
+
return 'staging';
|
|
1083
|
+
}
|
|
1084
|
+
const hostname = window.location.hostname.toLowerCase();
|
|
1085
|
+
// 本地开发环境
|
|
1086
|
+
if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '0.0.0.0') {
|
|
1087
|
+
return 'local';
|
|
1088
|
+
}
|
|
1089
|
+
// 开发环境 (包含 -dev 或 .dev)
|
|
1090
|
+
if (hostname.includes('-dev.') || hostname.includes('.dev') || hostname.endsWith('.dev')) {
|
|
1091
|
+
return 'development';
|
|
1092
|
+
}
|
|
1093
|
+
// Staging 环境
|
|
1094
|
+
if (hostname.includes('-staging.') || hostname.includes('staging.')) {
|
|
1095
|
+
return 'staging';
|
|
1096
|
+
}
|
|
1097
|
+
// 默认使用 production
|
|
1098
|
+
return 'production';
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* 获取环境配置
|
|
1102
|
+
*
|
|
1103
|
+
* @param env 环境类型,如果不提供则自动检测
|
|
1104
|
+
* @returns 环境配置对象
|
|
1105
|
+
*/
|
|
1106
|
+
function getEnvironmentConfig(env) {
|
|
1107
|
+
const targetEnv = env || detectEnvironment();
|
|
1108
|
+
return ENVIRONMENT_CONFIGS[targetEnv];
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* 解析用户配置,返回最终的 baseURL
|
|
1112
|
+
*
|
|
1113
|
+
* 优先级:
|
|
1114
|
+
* 1. 显式传入的 baseURL(最高优先级)
|
|
1115
|
+
* 2. environment 参数指定的环境
|
|
1116
|
+
* 3. 自动检测环境(最低优先级)
|
|
1117
|
+
*
|
|
1118
|
+
* @param options 用户配置选项
|
|
1119
|
+
* @returns 最终使用的 baseURL
|
|
1120
|
+
*/
|
|
1121
|
+
function resolveBaseURL(options) {
|
|
1122
|
+
// 优先级 1: 显式传入的 baseURL
|
|
1123
|
+
if (options.baseURL) {
|
|
1124
|
+
return options.baseURL;
|
|
1125
|
+
}
|
|
1126
|
+
// 优先级 2: environment 参数
|
|
1127
|
+
if (options.environment) {
|
|
1128
|
+
return ENVIRONMENT_CONFIGS[options.environment].baseURL;
|
|
1129
|
+
}
|
|
1130
|
+
// 优先级 3: 自动检测
|
|
1131
|
+
const detectedEnv = detectEnvironment();
|
|
1132
|
+
return ENVIRONMENT_CONFIGS[detectedEnv].baseURL;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1029
1135
|
/**
|
|
1030
1136
|
* Type definitions for SeaVerse Dispatcher API
|
|
1031
1137
|
* Generated from auth.yaml OpenAPI specification
|
|
@@ -1038,12 +1144,37 @@ var models = /*#__PURE__*/Object.freeze({
|
|
|
1038
1144
|
/**
|
|
1039
1145
|
* SeaVerse Backend API Client
|
|
1040
1146
|
* SeaVerse Dispatcher API - 云原生 AI Agent 平台的统一网关服务
|
|
1147
|
+
*
|
|
1041
1148
|
* @version 2.0.0
|
|
1149
|
+
*
|
|
1150
|
+
* @example
|
|
1151
|
+
* // 方式 1: 自动检测环境(推荐本地开发)
|
|
1152
|
+
* const client = new SeaVerseBackendAPIClient();
|
|
1153
|
+
*
|
|
1154
|
+
* @example
|
|
1155
|
+
* // 方式 2: 指定环境(推荐生产部署)
|
|
1156
|
+
* const client = new SeaVerseBackendAPIClient({
|
|
1157
|
+
* environment: 'production',
|
|
1158
|
+
* });
|
|
1159
|
+
*
|
|
1160
|
+
* @example
|
|
1161
|
+
* // 方式 3: 自定义 URL(特殊需求)
|
|
1162
|
+
* const client = new SeaVerseBackendAPIClient({
|
|
1163
|
+
* baseURL: 'https://custom-api.example.com',
|
|
1164
|
+
* });
|
|
1042
1165
|
*/
|
|
1043
1166
|
class SeaVerseBackendAPIClient {
|
|
1044
1167
|
constructor(options = {}) {
|
|
1168
|
+
// 使用智能配置解析,支持三种优先级:
|
|
1169
|
+
// 1. 显式 baseURL(最高)
|
|
1170
|
+
// 2. environment 参数
|
|
1171
|
+
// 3. 自动检测(默认)
|
|
1172
|
+
const finalBaseURL = resolveBaseURL({
|
|
1173
|
+
baseURL: options.baseURL,
|
|
1174
|
+
environment: options.environment,
|
|
1175
|
+
});
|
|
1045
1176
|
const httpOptions = {
|
|
1046
|
-
baseURL:
|
|
1177
|
+
baseURL: finalBaseURL,
|
|
1047
1178
|
timeout: options.timeout,
|
|
1048
1179
|
headers: options.headers,
|
|
1049
1180
|
auth: options.auth || this.getDefaultAuth(),
|
|
@@ -2296,9 +2427,10 @@ class AuthModal {
|
|
|
2296
2427
|
return;
|
|
2297
2428
|
}
|
|
2298
2429
|
// Store the current state (to verify callback)
|
|
2430
|
+
// ✅ 使用 localStorage 而不是 sessionStorage,避免跨页面跳转时丢失
|
|
2299
2431
|
const state = this.generateRandomState();
|
|
2300
|
-
|
|
2301
|
-
|
|
2432
|
+
localStorage.setItem('oauth_state', state);
|
|
2433
|
+
localStorage.setItem('oauth_provider', provider);
|
|
2302
2434
|
// Build authorization URL
|
|
2303
2435
|
const authUrl = this.buildAuthUrl(provider, config, state);
|
|
2304
2436
|
// Redirect to OAuth provider
|
|
@@ -2346,8 +2478,9 @@ class AuthModal {
|
|
|
2346
2478
|
async handleOAuthCallback(code, state) {
|
|
2347
2479
|
try {
|
|
2348
2480
|
// Verify state
|
|
2349
|
-
|
|
2350
|
-
const
|
|
2481
|
+
// ✅ 使用 localStorage 而不是 sessionStorage
|
|
2482
|
+
const storedState = localStorage.getItem('oauth_state');
|
|
2483
|
+
const provider = localStorage.getItem('oauth_provider');
|
|
2351
2484
|
if (!storedState || storedState !== state) {
|
|
2352
2485
|
throw new Error('Invalid state parameter - possible CSRF attack');
|
|
2353
2486
|
}
|
|
@@ -2355,8 +2488,8 @@ class AuthModal {
|
|
|
2355
2488
|
throw new Error('No provider stored in session');
|
|
2356
2489
|
}
|
|
2357
2490
|
// Clear stored state
|
|
2358
|
-
|
|
2359
|
-
|
|
2491
|
+
localStorage.removeItem('oauth_state');
|
|
2492
|
+
localStorage.removeItem('oauth_provider');
|
|
2360
2493
|
// Exchange code for token using the appropriate SDK method
|
|
2361
2494
|
let response;
|
|
2362
2495
|
switch (provider) {
|
|
@@ -2426,7 +2559,10 @@ exports.AuthFactory = AuthFactory;
|
|
|
2426
2559
|
exports.AuthModal = AuthModal;
|
|
2427
2560
|
exports.AuthProvider = AuthProvider;
|
|
2428
2561
|
exports.BuiltInHooks = BuiltInHooks;
|
|
2562
|
+
exports.ENVIRONMENT_CONFIGS = ENVIRONMENT_CONFIGS;
|
|
2429
2563
|
exports.SeaVerseBackendAPIClient = SeaVerseBackendAPIClient;
|
|
2430
2564
|
exports.createAuthModal = createAuthModal;
|
|
2565
|
+
exports.detectEnvironment = detectEnvironment;
|
|
2566
|
+
exports.getEnvironmentConfig = getEnvironmentConfig;
|
|
2431
2567
|
exports.models = models;
|
|
2432
2568
|
//# sourceMappingURL=index.cjs.map
|