@seaverse/auth-sdk 0.3.4 → 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
@@ -3750,7 +3750,8 @@ class AuthModal {
3750
3750
  async startOAuthFlow(provider) {
3751
3751
  try {
3752
3752
  // Get the return URL (where user should be redirected after OAuth)
3753
- 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;
3754
3755
  // Call backend to get OAuth authorization URL
3755
3756
  let authorizeUrl;
3756
3757
  switch (provider) {
@@ -3785,6 +3786,7 @@ class AuthModal {
3785
3786
  *
3786
3787
  * In Backend Proxy Mode, the backend redirects to returnUrl with token in query param.
3787
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.
3788
3790
  */
3789
3791
  static handleOAuthCallback(options) {
3790
3792
  const urlParams = new URLSearchParams(window.location.search);
@@ -3792,6 +3794,19 @@ class AuthModal {
3792
3794
  if (!token) {
3793
3795
  return null; // Not an OAuth callback
3794
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
+ }
3795
3810
  // Clean up URL (remove token from query string)
3796
3811
  const url = new URL(window.location.href);
3797
3812
  url.searchParams.delete('token');