1.19.2 • Published 2 months ago

@descope/web-js-sdk v1.19.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@descope/web-js-sdk

Descope JavaScript web SDK

Usage

Install the package

npm install @descope/web-js-sdk

Use it

import descopeSdk from '@descope/web-js-sdk';

const myProjectId = 'xxx';

const sdk = descopeSdk({
  /* Descope Project ID (Required) */
  projectId: myProjectId,
  /* Persist tokens that returned after successful authentication (e.g. sdk.otp.verify.email(...),
  sdk.refresh(...), flow.next(...), etc.) in browser storage. In addition, this will
  make `sdk.getSessionToken()` available, see usage bellow bellow */
  persistTokens: true,
  /* Pass `sessionTokenViaCookie: true` to store the session token in a cookie when using `persistTokens`. By default, the sdk will set the session token in the browser storage.
  Notes:
    - This option is relevant only when `persistTokens` is true.
    - The session token cookie is set as a [`Secure`](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.5) cookie. It will be sent only over HTTPS connections.
In addition, some browsers (e.g. Safari) may not store `Secure` cookie if the hosted page is running on an HTTP protocol.
    - If the cookie domain set on Descope configuration doesn't match, or is not a parent domain of the current domain, The cookie will be stored on the current domain that runs the code. Examples:
     - Project cookie domain is acme.com, current domain is app.acme.com - the domain will be set to app.acme.com
     - Project cookie domain is acme.com, current domain is my-app.com - the domain will be set to my-app.com
     */
  */
  sessionTokenViaCookie: false,
  /* Automatically schedule a call refresh session call after a successful authentication:
  Note: due to browser limitation, the maximum interval for the refresh has an upper bound of 2^32 - 1 milliseconds (approximately 24.8 days).
  */
  autoRefresh: true,
  /* Disable storing the last authenticated user details on the browser storage (default is true).
  Note: Disabling this flag may cause features related to the last-authenticated user to not function properly.
  */
  storeLastAuthenticatedUser: false, // default is true
  /* Do not clear the last authenticated user details from the browser storage after logout (default is false).
  Note: This flag is relevant only when `storeLastAuthenticatedUser` is true.
  */
  keepLastAuthenticatedUserAfterLogout: true // default is false
});

sdk.onSessionTokenChange((newSession, oldSession) => {
  // handle session token change...
});

sdk.onUserChange((newUser, oldUser) => {
  // handle user change...
});

/* For a case that the browser has a valid refresh token on storage/cookie,
the user should get a valid session token (e.i. user should be logged-in).
For that purpose, it is common to call the refresh function after sdk initialization.
Note: Refresh return a session token, so if the autoRefresh was provided, the sdk will
automatically continue to refresh the token */
sdk.refresh();

// Alternatively -  use the sdk's available authentication methods to authenticate the user
const userIdentifier = 'identifier';
let res = await sdk.otp.signIn.email(userIdentifier);
if (!res.ok) {
  throw Error('Failed to sign in');
}

// Get the one time code from email and verify it
const codeFromEmail = '1234';
res = await sdk.otp.verify.email(userIdentifier, codeFromEmail);
if (!res.ok) {
  throw Error('Failed to sign in');
}

// Get session token
// Can be used to pass token to server on header
const sessionToken = sdk.getSessionToken();

Run Example

To run the example:

  1. Install dependencies pnpm i
  2. Run the sample pnpm run start

The browser open a tab with directory tree of available examples. Click on the desire directory and follow the instruction.

NOTE: This package is a part of a monorepo. so if you make changes in a dependency, you will have to rerun npm run start (this is a temporary solution until we improve the process to fit to monorepo).

1.19.2

2 months ago

1.19.1

2 months ago

1.19.0

2 months ago

1.18.0

2 months ago

1.17.0

3 months ago

1.16.6

3 months ago

1.16.5

3 months ago

1.16.4

4 months ago

1.11.16

6 months ago

1.11.14

7 months ago

1.11.15

6 months ago

1.11.12

7 months ago

1.11.13

7 months ago

1.11.10

7 months ago

1.11.11

7 months ago

1.11.4

7 months ago

1.11.3

7 months ago

1.11.8

7 months ago

1.11.7

7 months ago

1.11.6

7 months ago

1.11.5

7 months ago

1.11.9

7 months ago

1.12.2

6 months ago

1.12.1

6 months ago

1.12.0

6 months ago

1.13.2

6 months ago

1.13.1

6 months ago

1.13.0

6 months ago

1.13.6

6 months ago

1.13.5

6 months ago

1.13.4

6 months ago

1.13.3

6 months ago

1.13.9

6 months ago

1.13.8

6 months ago

1.13.7

6 months ago

1.14.1

6 months ago

1.14.0

6 months ago

1.14.3

6 months ago

1.14.2

6 months ago

1.15.0

6 months ago

1.15.4

5 months ago

1.15.3

6 months ago

1.15.2

6 months ago

1.15.1

6 months ago

1.15.8

5 months ago

1.15.7

5 months ago

1.15.6

5 months ago

1.15.5

5 months ago

1.15.9

5 months ago

1.16.3

4 months ago

1.16.2

4 months ago

1.16.1

5 months ago

1.16.0

5 months ago

1.13.10

6 months ago

1.11.2

7 months ago

1.11.1

7 months ago

1.11.0

7 months ago

1.10.49

7 months ago

1.10.48

7 months ago

1.10.47

7 months ago

1.10.46

8 months ago

1.10.45

8 months ago

1.10.44

8 months ago

1.10.43

8 months ago

1.10.42

8 months ago

1.10.41

8 months ago

1.10.40

8 months ago

1.10.38

8 months ago

1.10.39

8 months ago

1.10.37

8 months ago

1.10.36

8 months ago

1.10.35

8 months ago

1.10.34

8 months ago

1.10.33

8 months ago

1.10.32

9 months ago

1.10.31

9 months ago

1.10.30

9 months ago

1.10.29

9 months ago

1.10.28

9 months ago

1.10.27

9 months ago

1.10.26

9 months ago

1.10.25

9 months ago

1.10.24

9 months ago

1.10.23

9 months ago

1.10.22

9 months ago

1.10.20

9 months ago

1.10.21

9 months ago

1.10.15

9 months ago

1.10.16

9 months ago

1.10.14

9 months ago

1.10.19

9 months ago

1.10.17

9 months ago

1.10.18

9 months ago

1.10.13

9 months ago

1.10.11

9 months ago

1.10.12

9 months ago

1.10.10

9 months ago

1.10.9

9 months ago

1.10.8

9 months ago

1.10.7

9 months ago

1.10.6

9 months ago

1.10.5

10 months ago

1.10.4

10 months ago

1.10.3

10 months ago

1.10.2

10 months ago

1.10.1

10 months ago

1.10.0

11 months ago

1.9.5

11 months ago

1.9.4

11 months ago

1.9.3

11 months ago

1.9.1

11 months ago

1.9.2

11 months ago

1.9.0

12 months ago

1.8.5

12 months ago

1.8.4

12 months ago

1.8.2

12 months ago

1.8.3

12 months ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.2

1 year ago

1.7.1

1 year ago

1.6.4

1 year ago

1.6.3

1 year ago

1.6.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.5.0

1 year ago

1.6.3-alpha.0

1 year ago

1.6.7

1 year ago

1.6.6

1 year ago

1.6.5

1 year ago

1.4.3

1 year ago

1.3.12-alpha.1

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.12-alpha.2

1 year ago

1.3.10

1 year ago

1.3.13

1 year ago

1.3.14

1 year ago

1.3.11

1 year ago

1.3.12

1 year ago

1.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.7.0

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

2 years ago

1.3.1

2 years ago

1.2.10

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.2.9

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.0

2 years ago

0.1.4

2 years ago

0.1.0-alpha.30

2 years ago

0.1.0-alpha.18

2 years ago

0.1.0-alpha.17

2 years ago

0.1.0-alpha.19

2 years ago

0.1.0-alpha.21

2 years ago

0.1.0-alpha.20

2 years ago

0.1.0-alpha.23

2 years ago

0.1.0-alpha.22

2 years ago

0.1.0-alpha.25

2 years ago

0.1.0-alpha.24

2 years ago

0.1.0-alpha.27

2 years ago

0.1.0-alpha.26

2 years ago

0.1.0-alpha.29

2 years ago

0.1.0-alpha.28

2 years ago

0.1.1-alpha.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.3

2 years ago

0.1.0-alpha.10

2 years ago

0.1.0-alpha.12

2 years ago

0.1.0-alpha.11

2 years ago

0.1.0-alpha.14

2 years ago

0.1.0-alpha.13

2 years ago

0.1.0-alpha.16

2 years ago

0.1.0-alpha.15

2 years ago

0.1.0-alpha.9

2 years ago

0.1.0-alpha.8

2 years ago

0.1.0-alpha.7

2 years ago

0.1.0-alpha.6

2 years ago

0.1.0-alpha.5

2 years ago

0.1.0-alpha.4

2 years ago

0.1.0-alpha.3

2 years ago

0.1.0

2 years ago

0.1.0-alpha.2

2 years ago

0.0.1-alpha.7

2 years ago

0.0.1-alpha.6

2 years ago

0.0.1-alpha.5

2 years ago

0.0.1-alpha.4

2 years ago

0.0.1-alpha.3

2 years ago

0.0.1-alpha.2

2 years ago

0.0.1-alpha.1

2 years ago