{"id":20215532,"url":"https://github.com/casdoor/casdoor-js-sdk","last_synced_at":"2025-07-25T20:40:43.545Z","repository":{"id":39897812,"uuid":"384591531","full_name":"casdoor/casdoor-js-sdk","owner":"casdoor","description":"Javascript client SDK for Casdoor","archived":false,"fork":false,"pushed_at":"2024-10-30T15:54:16.000Z","size":209,"stargazers_count":38,"open_issues_count":2,"forks_count":31,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-18T09:25:28.639Z","etag":null,"topics":["auth","authentication","authn","casdoor","javascript","js","oauth","oidc","sso"],"latest_commit_sha":null,"homepage":"https://github.com/casdoor/casdoor","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casdoor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-10T02:15:13.000Z","updated_at":"2025-06-27T15:52:24.000Z","dependencies_parsed_at":"2024-05-07T10:26:41.124Z","dependency_job_id":"f73c314c-df37-49a4-ab08-5dd9cb9420b5","html_url":"https://github.com/casdoor/casdoor-js-sdk","commit_stats":{"total_commits":37,"total_committers":17,"mean_commits":2.176470588235294,"dds":0.7567567567567568,"last_synced_commit":"e25d27d0137616651f101e458924ba877702ac2e"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/casdoor/casdoor-js-sdk","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-js-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-js-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-js-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-js-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casdoor","download_url":"https://codeload.github.com/casdoor/casdoor-js-sdk/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-js-sdk/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267058523,"owners_count":24029039,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","authn","casdoor","javascript","js","oauth","oidc","sso"],"created_at":"2024-11-14T06:22:55.268Z","updated_at":"2025-07-25T20:40:43.483Z","avatar_url":"https://github.com/casdoor.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# casdoor-js-sdk\r\n\r\n[![NPM version][npm-image]][npm-url]\r\n[![NPM download][download-image]][download-url]\r\n[![codebeat badge](https://codebeat.co/badges/6f2ad052-7fc8-42e1-b40f-0ca2648530c2)](https://codebeat.co/projects/github-com-casdoor-casdoor-js-sdk-master)\r\n[![GitHub Actions](https://github.com/casdoor/casdoor-js-sdk/actions/workflows/release.yml/badge.svg)](https://github.com/casdoor/casdoor-js-sdk/actions/workflows/release.yml)\r\n[![GitHub Actions](https://github.com/casdoor/casdoor-js-sdk/actions/workflows/build.yml/badge.svg)](https://github.com/casdoor/casdoor-js-sdk/actions/workflows/build.yml)\r\n[![Coverage Status](https://codecov.io/gh/casdoor/casdoor-js-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/casdoor/casdoor-js-sdk)\r\n[![Release](https://img.shields.io/github/release/casdoor/casdoor-js-sdk.svg)](https://github.com/casdoor/casdoor-js-sdk/releases/latest)\r\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord\u0026label=discord\u0026color=5865F2)](https://discord.gg/5rPsrAzK7S)\r\n\r\n[npm-image]: https://img.shields.io/npm/v/casdoor-js-sdk.svg?style=flat-square\r\n[npm-url]: https://npmjs.com/package/casdoor-js-sdk\r\n\r\n[download-image]: https://img.shields.io/npm/dm/casdoor-js-sdk.svg?style=flat-square\r\n[download-url]: https://npmjs.com/package/casdoor-js-sdk\r\n\r\nThis is Casdoor's SDK for js will allow you to easily connect your application to the [Casdoor authentication system](https://casdoor.org) without having to implement it from scratch.\r\n\r\nCasdoor SDK is very simple to use. We will show you the steps below.\r\n\r\n## Usage in NPM environment\r\n\r\n### Installation\r\n\r\n~~~shell script\r\n# NPM\r\nnpm i casdoor-js-sdk\r\n\r\n# Yarn\r\nyarn add casdoor-js-sdk\r\n~~~\r\n\r\n### Init SDK\r\n\r\nInitialization requires 5 parameters, which are all string type:\r\n\r\n| Name (in order)  | Must | Description                                                                                    |\r\n|------------------|------|------------------------------------------------------------------------------------------------|\r\n| serverUrl        | Yes  | your Casdoor server URL                                                                        |\r\n| clientId         | Yes  | the Client ID of your Casdoor application                                                      |\r\n| appName          | Yes  | the name of your Casdoor application                                                           |\r\n| organizationName | Yes  | the name of the Casdoor organization connected with your Casdoor application                   |\r\n| redirectPath     | No   | the path of the redirect URL for your Casdoor application, will be `/callback` if not provided |\r\n| signinPath       | No   | the path of the signin URL for your Casdoor application, will be `/api/signin` if not provided |\r\n\r\n```typescript\r\nimport {SDK, SdkConfig} from 'casdoor-js-sdk'\r\n\r\nconst sdkConfig: SdkConfig = {\r\n    serverUrl: \"https://door.casbin.com\",\r\n    clientId: \"014ae4bd048734ca2dea\",\r\n    appName: \"app-casnode\",\r\n    organizationName: \"casbin\",\r\n    redirectPath: \"/callback\",\r\n    signinPath: \"/api/signin\",\r\n}\r\nconst sdk = new SDK(sdkConfig)\r\n// call sdk to handle\r\n```\r\n\r\n## Usage in vanilla Javascript\r\n\r\n### Import and init SDK\r\n\r\nInitialization parameters are consistent with the previous node.js section:\r\n\r\n```html\r\n\u003c!--init the SDK--\u003e\r\n\u003cscript type=\"module\"\u003e\r\n  //Import from cdn(you can choose the appropriate cdn source according to your needs), or just from the local(download the casdoor-js-sdk first)\r\n  import SDK from 'https://unpkg.com/casdoor-js-sdk@latest/lib/esm/sdk.js'\r\n  const sdkConfig = {\r\n    serverUrl: \"https://door.casbin.com\",\r\n    clientId: \"014ae4bd048734ca2dea\",\r\n    appName: \"app-casnode\",\r\n    organizationName: \"casbin\",\r\n    redirectPath: \"/callback\",\r\n    signinPath: \"/api/signin\",\r\n  }\r\n  window.sdk = new SDK(sdkConfig)\r\n\u003c/script\u003e\r\n```\r\n\r\n### Call functions in SDK\r\n\r\n```html\r\n\u003cscript type=\"text/javascript\"\u003e\r\n  function gotoSignUpPage() {\r\n    window.location.href = sdk.getSigninUrl()\r\n  }\r\n\u003c/script\u003e\r\n```\r\n\r\n## API reference interface\r\n\r\n#### Get sign up url\r\n\r\n```typescript\r\ngetSignupUrl(enablePassword)\r\n```\r\n\r\nReturn the casdoor url that navigates to the registration screen\r\n\r\n#### Get sign in url\r\n\r\n```typescript\r\ngetSigninUrl()\r\n```\r\n\r\nReturn the casdoor url that navigates to the login screen\r\n\r\n#### Get user profile page url\r\n\r\n```typescript\r\ngetUserProfileUrl(userName, account)\r\n```\r\n\r\nReturn the url to navigate to a specific user's casdoor personal page\r\n\r\n#### Get my profile page url\r\n\r\n```typescript\r\ngetMyProfileUrl(account)\r\n```\r\n\r\n#### Sign in\r\n\r\n```typescript\r\nsignin(serverUrl, signinPath)\r\n```\r\n\r\nHandle the callback url from casdoor, call the back-end api to complete the login process\r\n\r\n#### Determine whether silent sign-in is being used\r\n\r\n```typescript\r\nisSilentSigninRequested()\r\n```\r\n\r\nWe usually use this method to determine if silent login is being used. By default, if the silentSignin parameter is included in the URL and equals one, this method will return true. Of course, you can also use any method you prefer.\r\n\r\n#### silentSignin\r\n\r\n\r\n````typescript\r\nsilentSignin(onSuccess, onFailure)\r\n````\r\n\r\nFirst, let's explain the two parameters of this method, which are the callback methods for successful and failed login. Next, I will describe the execution process of this method. We will create a hidden \"iframe\" element to redirect to the login page for authentication, thereby achieving the effect of silent sign-in.\r\n\r\n#### popupSignin\r\n\r\n\r\n````typescript\r\npopupSignin(serverUrl, signinPath)\r\n````\r\nPopup a window to handle the callback url from casdoor, call the back-end api to complete the login process and store the token in localstorage, then reload the main window. See Demo: [casdoor-nodejs-react-example](https://github.com/casdoor/casdoor-nodejs-react-example).\r\n\r\n### OAuth2 PKCE flow sdk (for SPA without backend)\r\n\r\n#### Start the authorization process\r\n\r\nTypically, you just need to go to the authorization url to start the process. This example is something that might work in an SPA.\r\n\r\n```typescript\r\nsignin_redirect();\r\n```\r\n\r\nYou may add additional query parameters to the authorize url by using an optional second parameter:\r\n\r\n```typescript\r\nconst additionalParams = {test_param: 'testing'};\r\nsignin_redirect(additionalParams);\r\n```\r\n\r\n#### Trade the code for a token\r\n\r\nWhen you get back here, you need to exchange the code for a token.\r\n\r\n```typescript\r\nsdk.exchangeForAccessToken().then((resp) =\u003e {\r\n    const token = resp.access_token;\r\n    // Do stuff with the access token.\r\n});\r\n```\r\n\r\nAs with the authorizeUrl method, an optional second parameter may be passed to the exchangeForAccessToken method to send additional parameters to the request:\r\n\r\n```typescript\r\nconst additionalParams = {test_param: 'testing'};\r\n\r\nsdk.exchangeForAccessToken(additionalParams).then((resp) =\u003e {\r\n    const token = resp.access_token;\r\n    // Do stuff with the access token.\r\n});\r\n```\r\n\r\n#### Parse the access token\r\n\r\nOnce you have an access token, you can parse it into JWT header and payload.\r\n\r\n```typescript\r\nconst result = sdk.parseAccessToken(accessToken);\r\nconsole.log(\"JWT algorithm: \" + result.header.alg);\r\nconsole.log(\"User organization: \" + result.payload.owner);\r\nconsole.log(\"User name: \" + result.payload.name);\r\n```\r\n\r\n#### Get user info\r\n\r\nOnce you have an access token, you can use it to get user info.\r\n\r\n```typescript\r\ngetUserInfo(accessToken).then((resp) =\u003e {\r\n    const userInfo = resp;\r\n    // Do stuff with the user info.\r\n});\r\n```\r\n\r\n#### Refresh access token\r\n\r\nYou could use a refresh token, to get a new token from the oauth server when token expired.\r\n\r\n```typescript\r\nsdk.refreshAccessToken(refreshToken).then((resp) =\u003e {\r\n    const token = resp.access_token;\r\n    // Do stuff with new access token\r\n});\r\n```\r\n\r\n#### A note on Storage\r\nBy default, this package will use sessionStorage to persist the pkce_state. On (mostly) mobile devices there's a higher chance users are returning in a different browser tab. E.g. they kick off in a WebView \u0026 get redirected to a new tab. The sessionStorage will be empty there.\r\n\r\nIn this case it you can opt in to use localStorage instead of sessionStorage:\r\n\r\n```typescript\r\nimport {SDK, SdkConfig} from 'casdoor-js-sdk'\r\n\r\nconst sdkConfig = {\r\n  // ...\r\n  storage: localStorage, // any Storage object, sessionStorage (default) or localStorage\r\n}\r\n\r\nconst sdk = new SDK(sdkConfig)\r\n```\r\n\r\n## More examples\r\n\r\nTo see how to use casdoor frontend SDK with casdoor backend SDK, you can refer to examples below:\r\n\r\n[casnode](https://github.com/casbin/casnode): casdoor-js-sdk + casdoor-go-sdk\r\n\r\n[casdoor-python-vue-sdk-example](https://github.com/casdoor/casdoor-python-vue-sdk-example): casdoor-vue-sdk + casdoor-python-sdk\r\n\r\n\r\n\r\nA more detailed description can be moved to:[casdoor-sdk](https://casdoor.org/docs/how-to-connect/sdk)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasdoor%2Fcasdoor-js-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasdoor%2Fcasdoor-js-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasdoor%2Fcasdoor-js-sdk/lists"}