@descope/react-sdk 3.5.7 → 3.6.1

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
@@ -341,6 +341,100 @@ const App = () => {
341
341
  >
342
342
  > `isAuthenticated` is derived from the session's expiration, which the SDK receives in every token-storage mode. Read `sessionToken` only when you actually need the raw JWT (for example to attach it to an API request), and use `claims` to read claims out of the session.
343
343
 
344
+ ### SSO Single Logout
345
+
346
+ When the user signed in through a SAML SSO configuration that has single logout turned on, pass `ssoLogout: true` to `sdk.logout` (or `sdk.logoutAll`) to also log the user out of the identity provider. The Descope session ends either way. If there is a logout to do at the identity provider, the response has `ssoLogoutUrl`, and the app sends the browser to it. The identity provider then sends the browser back to `redirectUrl`, which must be in the project's approved domains (when empty, the SSO configuration's redirect URL is used).
347
+
348
+ ```js
349
+ const sdk = useDescope();
350
+
351
+ const handleLogout = useCallback(async () => {
352
+ const res = await sdk.logout(undefined, {
353
+ ssoLogout: true,
354
+ redirectUrl: 'https://app.example.com/signed-out',
355
+ });
356
+ if (res.data?.ssoLogoutUrl) {
357
+ window.location.href = res.data.ssoLogoutUrl;
358
+ }
359
+ }, [sdk]);
360
+ ```
361
+
362
+ ### Returning Users After an Idle Period
363
+
364
+ When the short-lived session token expires, a user with a valid refresh token does not need to sign in again. The SDK renews the session once `useSession()` runs. Two things keep that working: the login page waits for `useSession()` to finish loading before it shows the flow, and it returns the user to the page they asked for. Accept only in-app return paths, otherwise the redirect can send users to another site.
365
+
366
+ ```tsx
367
+ // src/components/ProtectedRoute.tsx
368
+ import { useSession } from '@descope/react-sdk';
369
+ import { Navigate, useLocation } from 'react-router-dom';
370
+
371
+ function ProtectedRoute({ children }: { children: React.ReactNode }) {
372
+ const { isAuthenticated, isSessionLoading } = useSession();
373
+ const location = useLocation();
374
+
375
+ if (isSessionLoading) return <div>Loading...</div>;
376
+ if (!isAuthenticated) {
377
+ // Keep the requested page so the login page can return the user to it.
378
+ return <Navigate to="/login" replace state={{ from: location.pathname + location.search }} />;
379
+ }
380
+
381
+ return <>{children}</>;
382
+ }
383
+ ```
384
+
385
+ ```tsx
386
+ // src/pages/Login.tsx
387
+ import { useEffect } from 'react';
388
+ import { Descope, useSession } from '@descope/react-sdk';
389
+ import { useLocation, useNavigate } from 'react-router-dom';
390
+
391
+ // Only allow paths inside this app, so the return path can't send users to another site.
392
+ function safeNext(value: unknown): string {
393
+ if (typeof value !== 'string' || !value) return '/';
394
+ try {
395
+ const base = 'http://app.invalid'; // placeholder origin, only used for parsing
396
+ const url = new URL(value, base);
397
+ if (url.origin !== base || url.pathname.startsWith('//') || url.pathname.startsWith('/login')) {
398
+ return '/';
399
+ }
400
+ return url.pathname + url.search + url.hash;
401
+ } catch {
402
+ return '/';
403
+ }
404
+ }
405
+
406
+ function LoginPage() {
407
+ const navigate = useNavigate();
408
+ const location = useLocation();
409
+ const next = safeNext((location.state as { from?: string } | null)?.from);
410
+ const { isAuthenticated, isSessionLoading } = useSession();
411
+
412
+ useEffect(() => {
413
+ if (isAuthenticated) navigate(next, { replace: true });
414
+ }, [isAuthenticated, next, navigate]);
415
+
416
+ // Let the SDK renew the session from the refresh token before showing the login flow.
417
+ if (isSessionLoading || isAuthenticated) return <div>Checking your session...</div>;
418
+
419
+ return <Descope flowId="sign-up-or-in" onSuccess={() => navigate(next, { replace: true })} onError={(e) => console.error('Auth failed:', e.detail)} />;
420
+ }
421
+ ```
422
+
423
+ Before an API call that may run right after the user returns to a background tab, wait for an expired session token to renew:
424
+
425
+ ```tsx
426
+ import { getSessionToken, isSessionTokenExpired, refresh } from '@descope/react-sdk';
427
+
428
+ export async function ensureFreshSession() {
429
+ const token = getSessionToken();
430
+ // getSessionToken() returns '' when Descope manages the token as an HttpOnly
431
+ // cookie. Then this does nothing and the SDK's background renewal applies.
432
+ if (token && isSessionTokenExpired(token)) {
433
+ await refresh().catch(() => {});
434
+ }
435
+ }
436
+ ```
437
+
344
438
  ### Trigger Auto Refresh
345
439
 
346
440
  `useSession` triggers a single request to the Descope backend to attempt to refresh the session. If you **don't** `useSession` in your app, the session will not be refreshed automatically. If your app does not require `useSession`, you can trigger the refresh manually by calling `refresh` from `useDescope` hook. Example:
@@ -1,2 +1,2 @@
1
- "use strict";const e="undefined"!=typeof window;exports.IS_BROWSER=e,exports.baseHeaders={"x-descope-sdk-name":"react","x-descope-sdk-version":"3.5.7"};
1
+ "use strict";const e="undefined"!=typeof window;exports.IS_BROWSER=e,exports.baseHeaders={"x-descope-sdk-name":"react","x-descope-sdk-version":"3.6.1"};
2
2
  //# sourceMappingURL=constants.js.map
@@ -1,2 +1,2 @@
1
- const e={"x-descope-sdk-name":"react","x-descope-sdk-version":"3.5.7"},d="undefined"!=typeof window;export{d as IS_BROWSER,e as baseHeaders};
1
+ const e={"x-descope-sdk-name":"react","x-descope-sdk-version":"3.6.1"},d="undefined"!=typeof window;export{d as IS_BROWSER,e as baseHeaders};
2
2
  //# sourceMappingURL=constants.js.map
package/dist/index.d.ts CHANGED
@@ -67,7 +67,7 @@ declare const createSdkWrapper: <P extends {
67
67
  refreshTokenViaCookie?: _descope_web_js_sdk.CookieConfig;
68
68
  }>(config: P) => (((({
69
69
  refresh: (token?: string, tryRefresh?: boolean) => Promise<_1.SdkResponse<_1.JWTResponse>>;
70
- logout: (token?: string) => Promise<_1.SdkResponse<never>>;
70
+ logout: (token?: string, options?: _1.LogoutOptions) => Promise<_1.SdkResponse<_1.LogoutResponse>>;
71
71
  flow: {
72
72
  start: (flowId: string, options?: Pick<_1.FlowStartOptions, "tenant" | "redirectUrl" | "redirectAuth" | "oidcIdpStateId" | "samlIdpStateId" | "wsfedIdpStateId" | "samlIdpUsername" | "ssoAppId" | "thirdPartyAppId" | "oidcLoginHint" | "preview" | "abTestingKey" | "client" | "locale" | "oidcPrompt" | "oidcErrorRedirectUri" | "oidcResource" | "nativeOptions" | "thirdPartyAppStateId" | "applicationScopes" | "outboundAppId" | "outboundAppScopes"> & {
73
73
  lastAuth?: Omit<{
@@ -824,7 +824,7 @@ declare const createSdkWrapper: <P extends {
824
824
  }>>;
825
825
  };
826
826
  selectTenant: (tenantId: string, token?: string) => Promise<_1.SdkResponse<_1.JWTResponse>>;
827
- logoutAll: (token?: string) => Promise<_1.SdkResponse<never>>;
827
+ logoutAll: (token?: string, options?: _1.LogoutOptions) => Promise<_1.SdkResponse<_1.LogoutResponse>>;
828
828
  me: (token?: string) => Promise<_1.SdkResponse<_1.UserResponse>>;
829
829
  myTenants: (tenants: true | string[], token?: string) => Promise<_1.SdkResponse<{
830
830
  tenants: {
@@ -842,7 +842,7 @@ declare const createSdkWrapper: <P extends {
842
842
  httpClient: _1.HttpClient;
843
843
  } | {
844
844
  refresh: (token?: string, tryRefresh?: boolean) => Promise<_1.SdkResponse<_1.JWTResponse>>;
845
- logout: (token?: string) => Promise<_1.SdkResponse<never>>;
845
+ logout: (token?: string, options?: _1.LogoutOptions) => Promise<_1.SdkResponse<_1.LogoutResponse>>;
846
846
  flow: {
847
847
  start: (flowId: string, options?: Pick<_1.FlowStartOptions, "tenant" | "redirectUrl" | "redirectAuth" | "oidcIdpStateId" | "samlIdpStateId" | "wsfedIdpStateId" | "samlIdpUsername" | "ssoAppId" | "thirdPartyAppId" | "oidcLoginHint" | "preview" | "abTestingKey" | "client" | "locale" | "oidcPrompt" | "oidcErrorRedirectUri" | "oidcResource" | "nativeOptions" | "thirdPartyAppStateId" | "applicationScopes" | "outboundAppId" | "outboundAppScopes"> & {
848
848
  lastAuth?: Omit<{
@@ -1599,7 +1599,7 @@ declare const createSdkWrapper: <P extends {
1599
1599
  }>>;
1600
1600
  };
1601
1601
  selectTenant: (tenantId: string, token?: string) => Promise<_1.SdkResponse<_1.JWTResponse>>;
1602
- logoutAll: (token?: string) => Promise<_1.SdkResponse<never>>;
1602
+ logoutAll: (token?: string, options?: _1.LogoutOptions) => Promise<_1.SdkResponse<_1.LogoutResponse>>;
1603
1603
  me: (token?: string) => Promise<_1.SdkResponse<_1.UserResponse>>;
1604
1604
  myTenants: (tenants: true | string[], token?: string) => Promise<_1.SdkResponse<{
1605
1605
  tenants: {
@@ -1617,7 +1617,7 @@ declare const createSdkWrapper: <P extends {
1617
1617
  httpClient: _1.HttpClient;
1618
1618
  } | {
1619
1619
  refresh: (token?: string, tryRefresh?: boolean) => Promise<_1.SdkResponse<_1.JWTResponse>>;
1620
- logout: (token?: string) => Promise<_1.SdkResponse<never>>;
1620
+ logout: (token?: string, options?: _1.LogoutOptions) => Promise<_1.SdkResponse<_1.LogoutResponse>>;
1621
1621
  flow: {
1622
1622
  start: (flowId: string, options?: Pick<_1.FlowStartOptions, "tenant" | "redirectUrl" | "redirectAuth" | "oidcIdpStateId" | "samlIdpStateId" | "wsfedIdpStateId" | "samlIdpUsername" | "ssoAppId" | "thirdPartyAppId" | "oidcLoginHint" | "preview" | "abTestingKey" | "client" | "locale" | "oidcPrompt" | "oidcErrorRedirectUri" | "oidcResource" | "nativeOptions" | "thirdPartyAppStateId" | "applicationScopes" | "outboundAppId" | "outboundAppScopes"> & {
1623
1623
  lastAuth?: Omit<{
@@ -2374,7 +2374,7 @@ declare const createSdkWrapper: <P extends {
2374
2374
  }>>;
2375
2375
  };
2376
2376
  selectTenant: (tenantId: string, token?: string) => Promise<_1.SdkResponse<_1.JWTResponse>>;
2377
- logoutAll: (token?: string) => Promise<_1.SdkResponse<never>>;
2377
+ logoutAll: (token?: string, options?: _1.LogoutOptions) => Promise<_1.SdkResponse<_1.LogoutResponse>>;
2378
2378
  me: (token?: string) => Promise<_1.SdkResponse<_1.UserResponse>>;
2379
2379
  myTenants: (tenants: true | string[], token?: string) => Promise<_1.SdkResponse<{
2380
2380
  tenants: {