@seaverse/auth-sdk 0.1.4 → 0.2.0

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/dist/index.js CHANGED
@@ -1125,23 +1125,32 @@ var models = /*#__PURE__*/Object.freeze({
1125
1125
  * @version 2.0.0
1126
1126
  *
1127
1127
  * @example
1128
- * // 方式 1: 自动检测环境(推荐本地开发)
1129
- * const client = new SeaVerseBackendAPIClient();
1128
+ * // 方式 1: SeaVerse 平台应用(自动检测环境)
1129
+ * const client = new SeaVerseBackendAPIClient({
1130
+ * appId: 'your-app-id',
1131
+ * });
1130
1132
  *
1131
1133
  * @example
1132
- * // 方式 2: 指定环境(推荐生产部署)
1134
+ * // 方式 2: 第三方应用(指定环境)
1133
1135
  * const client = new SeaVerseBackendAPIClient({
1136
+ * appId: 'game-abc123',
1134
1137
  * environment: 'production',
1135
1138
  * });
1136
1139
  *
1137
1140
  * @example
1138
1141
  * // 方式 3: 自定义 URL(特殊需求)
1139
1142
  * const client = new SeaVerseBackendAPIClient({
1143
+ * appId: 'my-app',
1140
1144
  * baseURL: 'https://custom-api.example.com',
1141
1145
  * });
1142
1146
  */
1143
1147
  class SeaVerseBackendAPIClient {
1144
- constructor(options = {}) {
1148
+ constructor(options) {
1149
+ // 验证必需的 appId 参数
1150
+ if (!options.appId) {
1151
+ throw new Error('appId is required. Please provide an appId when initializing SeaVerseBackendAPIClient.');
1152
+ }
1153
+ this.appId = options.appId;
1145
1154
  // 使用智能配置解析,支持三种优先级:
1146
1155
  // 1. 显式 baseURL(最高)
1147
1156
  // 2. environment 参数
@@ -1150,10 +1159,15 @@ class SeaVerseBackendAPIClient {
1150
1159
  baseURL: options.baseURL,
1151
1160
  environment: options.environment,
1152
1161
  });
1162
+ // 合并用户提供的 headers 和必需的 X-App-ID header
1163
+ const headers = {
1164
+ 'X-App-ID': this.appId,
1165
+ ...options.headers,
1166
+ };
1153
1167
  const httpOptions = {
1154
1168
  baseURL: finalBaseURL,
1155
1169
  timeout: options.timeout,
1156
- headers: options.headers,
1170
+ headers,
1157
1171
  auth: options.auth || this.getDefaultAuth(),
1158
1172
  hooks: options.hooks || this.getDefaultHooks(),
1159
1173
  };
@@ -1216,7 +1230,7 @@ class SeaVerseBackendAPIClient {
1216
1230
  async register(data, options) {
1217
1231
  const config = {
1218
1232
  method: 'POST',
1219
- url: `/api/auth/register`,
1233
+ url: `/sdk/v1/auth/register`,
1220
1234
  data,
1221
1235
  headers: {
1222
1236
  'X-Operation-Id': 'register',
@@ -1234,7 +1248,7 @@ class SeaVerseBackendAPIClient {
1234
1248
  async login(data, options) {
1235
1249
  const config = {
1236
1250
  method: 'POST',
1237
- url: `/api/auth/login`,
1251
+ url: `/sdk/v1/auth/login`,
1238
1252
  data,
1239
1253
  headers: {
1240
1254
  'X-Operation-Id': 'login',
@@ -1252,7 +1266,7 @@ class SeaVerseBackendAPIClient {
1252
1266
  async getCurrentUser(options) {
1253
1267
  const config = {
1254
1268
  method: 'GET',
1255
- url: `/api/auth/me`,
1269
+ url: `/sdk/v1/auth/me`,
1256
1270
  headers: {
1257
1271
  'X-Operation-Id': 'getCurrentUser',
1258
1272
  ...options?.headers,
@@ -1269,7 +1283,7 @@ class SeaVerseBackendAPIClient {
1269
1283
  async logout(options) {
1270
1284
  const config = {
1271
1285
  method: 'POST',
1272
- url: `/api/auth/logout`,
1286
+ url: `/sdk/v1/auth/logout`,
1273
1287
  headers: {
1274
1288
  'X-Operation-Id': 'logout',
1275
1289
  ...options?.headers,
@@ -1286,7 +1300,7 @@ class SeaVerseBackendAPIClient {
1286
1300
  async forgotPassword(data, options) {
1287
1301
  const config = {
1288
1302
  method: 'POST',
1289
- url: `/api/auth/forgot-password`,
1303
+ url: `/sdk/v1/auth/forgot-password`,
1290
1304
  data,
1291
1305
  headers: {
1292
1306
  'X-Operation-Id': 'forgotPassword',
@@ -1304,7 +1318,7 @@ class SeaVerseBackendAPIClient {
1304
1318
  async resetPassword(data, options) {
1305
1319
  const config = {
1306
1320
  method: 'POST',
1307
- url: `/api/auth/reset-password`,
1321
+ url: `/sdk/v1/auth/reset-password`,
1308
1322
  data,
1309
1323
  headers: {
1310
1324
  'X-Operation-Id': 'resetPassword',
@@ -1342,7 +1356,7 @@ class SeaVerseBackendAPIClient {
1342
1356
  async googleCodeToToken(data, options) {
1343
1357
  const config = {
1344
1358
  method: 'POST',
1345
- url: `/api/auth/google/code2token`,
1359
+ url: `/sdk/v1/auth/google/code2token`,
1346
1360
  data,
1347
1361
  headers: {
1348
1362
  'X-Operation-Id': 'googleCodeToToken',
@@ -1351,9 +1365,9 @@ class SeaVerseBackendAPIClient {
1351
1365
  ...options,
1352
1366
  };
1353
1367
  const response = await this.httpClient.request(config);
1354
- const apiData = response.data;
1355
- // 适配实际的API响应格式
1356
- if (!apiData.token) {
1368
+ // API返回格式: { data: { token, tempUserId, ... }, success: true }
1369
+ const apiData = response.data?.data || response.data;
1370
+ if (!apiData || !apiData.token) {
1357
1371
  throw new Error('No token in API response');
1358
1372
  }
1359
1373
  return {
@@ -1374,7 +1388,7 @@ class SeaVerseBackendAPIClient {
1374
1388
  async unlinkGoogle(options) {
1375
1389
  const config = {
1376
1390
  method: 'POST',
1377
- url: `/api/auth/google/unlink`,
1391
+ url: `/sdk/v1/auth/google/unlink`,
1378
1392
  headers: {
1379
1393
  'X-Operation-Id': 'unlinkGoogle',
1380
1394
  ...options?.headers,
@@ -1390,7 +1404,7 @@ class SeaVerseBackendAPIClient {
1390
1404
  async discordCodeToToken(data, options) {
1391
1405
  const config = {
1392
1406
  method: 'POST',
1393
- url: `/api/auth/discord/code2token`,
1407
+ url: `/sdk/v1/auth/discord/code2token`,
1394
1408
  data,
1395
1409
  headers: {
1396
1410
  'X-Operation-Id': 'discordCodeToToken',
@@ -1399,9 +1413,9 @@ class SeaVerseBackendAPIClient {
1399
1413
  ...options,
1400
1414
  };
1401
1415
  const response = await this.httpClient.request(config);
1402
- const apiData = response.data;
1403
- // 适配实际的API响应格式
1404
- if (!apiData.token) {
1416
+ // API返回格式: { data: { token, tempUserId, ... }, success: true }
1417
+ const apiData = response.data?.data || response.data;
1418
+ if (!apiData || !apiData.token) {
1405
1419
  throw new Error('No token in API response');
1406
1420
  }
1407
1421
  return {
@@ -1421,7 +1435,7 @@ class SeaVerseBackendAPIClient {
1421
1435
  async unlinkDiscord(options) {
1422
1436
  const config = {
1423
1437
  method: 'POST',
1424
- url: `/api/auth/discord/unlink`,
1438
+ url: `/sdk/v1/auth/discord/unlink`,
1425
1439
  headers: {
1426
1440
  'X-Operation-Id': 'unlinkDiscord',
1427
1441
  ...options?.headers,
@@ -1437,7 +1451,7 @@ class SeaVerseBackendAPIClient {
1437
1451
  async githubCodeToToken(data, options) {
1438
1452
  const config = {
1439
1453
  method: 'POST',
1440
- url: `/api/auth/github/code2token`,
1454
+ url: `/sdk/v1/auth/github/code2token`,
1441
1455
  data,
1442
1456
  headers: {
1443
1457
  'X-Operation-Id': 'githubCodeToToken',
@@ -1445,9 +1459,17 @@ class SeaVerseBackendAPIClient {
1445
1459
  },
1446
1460
  ...options,
1447
1461
  };
1462
+ console.log('🔐 [SDK Client] Calling githubCodeToToken with config:', config);
1448
1463
  const response = await this.httpClient.request(config);
1449
- const apiData = response.data;
1450
- if (!apiData.token) {
1464
+ console.log('📡 [SDK Client] Raw response.data:', response.data);
1465
+ // ✅ API返回格式: { data: { token, tempUserId, email, ... }, success: true }
1466
+ // 需要访问 response.data.data 而不是 response.data
1467
+ const apiData = response.data?.data || response.data;
1468
+ console.log('📡 [SDK Client] Extracted apiData:', apiData);
1469
+ console.log('📡 [SDK Client] apiData.token:', apiData?.token);
1470
+ if (!apiData || !apiData.token) {
1471
+ console.error('❌ [SDK Client] No token found!');
1472
+ console.error('❌ [SDK Client] response.data:', JSON.stringify(response.data, null, 2));
1451
1473
  throw new Error('No token in API response');
1452
1474
  }
1453
1475
  // 构造符合SDK期望的响应格式
@@ -1471,7 +1493,7 @@ class SeaVerseBackendAPIClient {
1471
1493
  async unlinkGithub(options) {
1472
1494
  const config = {
1473
1495
  method: 'POST',
1474
- url: `/api/auth/github/unlink`,
1496
+ url: `/sdk/v1/auth/github/unlink`,
1475
1497
  headers: {
1476
1498
  'X-Operation-Id': 'unlinkGithub',
1477
1499
  ...options?.headers,
@@ -1917,24 +1939,38 @@ class AuthModal {
1917
1939
  submitBtn.appendChild(btnText);
1918
1940
  submitBtn.appendChild(btnLoader);
1919
1941
  form.appendChild(submitBtn);
1920
- // Divider
1921
- const divider = document.createElement('div');
1922
- divider.className = 'divider';
1923
- divider.textContent = 'OR SIGN IN WITH';
1924
- form.appendChild(divider);
1925
- // Social buttons grid (Google + GitHub)
1926
- const socialGrid = document.createElement('div');
1927
- socialGrid.className = 'social-buttons-grid';
1928
- // Google button
1929
- const googleBtn = this.createSocialButton('google', 'Google', 'login');
1930
- socialGrid.appendChild(googleBtn);
1931
- // GitHub button
1932
- const githubBtn = this.createSocialButton('github', 'Github', 'login');
1933
- socialGrid.appendChild(githubBtn);
1934
- form.appendChild(socialGrid);
1935
- // Discord button (full width)
1936
- const discordBtn = this.createSocialButton('discord', 'Discord', 'login', true);
1937
- form.appendChild(discordBtn);
1942
+ // OAuth buttons - only show if configured
1943
+ const hasOAuth = this.options.oauthConfig &&
1944
+ (this.options.oauthConfig.google || this.options.oauthConfig.discord || this.options.oauthConfig.github);
1945
+ if (hasOAuth) {
1946
+ // Divider
1947
+ const divider = document.createElement('div');
1948
+ divider.className = 'divider';
1949
+ divider.textContent = 'OR SIGN IN WITH';
1950
+ form.appendChild(divider);
1951
+ // Social buttons grid (Google + GitHub)
1952
+ const socialGrid = document.createElement('div');
1953
+ socialGrid.className = 'social-buttons-grid';
1954
+ // Google button - only if configured
1955
+ if (this.options.oauthConfig?.google) {
1956
+ const googleBtn = this.createSocialButton('google', 'Google', 'login');
1957
+ socialGrid.appendChild(googleBtn);
1958
+ }
1959
+ // GitHub button - only if configured
1960
+ if (this.options.oauthConfig?.github) {
1961
+ const githubBtn = this.createSocialButton('github', 'Github', 'login');
1962
+ socialGrid.appendChild(githubBtn);
1963
+ }
1964
+ // Only add grid if it has buttons
1965
+ if (socialGrid.children.length > 0) {
1966
+ form.appendChild(socialGrid);
1967
+ }
1968
+ // Discord button (full width) - only if configured
1969
+ if (this.options.oauthConfig?.discord) {
1970
+ const discordBtn = this.createSocialButton('discord', 'Discord', 'login', true);
1971
+ form.appendChild(discordBtn);
1972
+ }
1973
+ }
1938
1974
  container.appendChild(form);
1939
1975
  return container;
1940
1976
  }