{"id":17500679,"url":"https://github.com/shimondoodkin/keycloak-js-util","last_synced_at":"2025-09-08T11:07:03.787Z","repository":{"id":151953903,"uuid":"625053624","full_name":"shimondoodkin/keycloak-js-util","owner":"shimondoodkin","description":"a utility wrapper for keycloak-js, it maintains a token refreshed, works with errors, easy to use authorized fetch functions. ","archived":false,"fork":false,"pushed_at":"2023-05-04T06:28:28.000Z","size":1931,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-04T12:58:10.354Z","etag":null,"topics":["authentication","keycloak","keycloak-js","login","microservies"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shimondoodkin.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":"2023-04-08T00:00:32.000Z","updated_at":"2023-04-08T19:51:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"63beff3e-c18e-4c0e-9816-bc95413805da","html_url":"https://github.com/shimondoodkin/keycloak-js-util","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/shimondoodkin/keycloak-js-util","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimondoodkin%2Fkeycloak-js-util","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimondoodkin%2Fkeycloak-js-util/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimondoodkin%2Fkeycloak-js-util/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimondoodkin%2Fkeycloak-js-util/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shimondoodkin","download_url":"https://codeload.github.com/shimondoodkin/keycloak-js-util/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimondoodkin%2Fkeycloak-js-util/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274174268,"owners_count":25235203,"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-09-08T02:00:09.813Z","response_time":121,"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":["authentication","keycloak","keycloak-js","login","microservies"],"created_at":"2024-10-19T18:47:08.942Z","updated_at":"2025-09-08T11:07:03.758Z","avatar_url":"https://github.com/shimondoodkin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# keycloack-js-util\n\nUser frindly js library for authentication with Keycloak.\n\n```\nnpm install keycloak-js-util\n```\n\n## ⭐️ Features\n\n- async library\n- basic login\n- easy to use async fetch wrappers that add authorization bearer token header\n- tested\n- Correct keyclock CORS and CSP(content security policy) configuration: \n  https://github.com/shimondoodkin/keycloak-js-util/tree/master/example/config\n- Correct krakend (CORS) configuration:\n  https://github.com/shimondoodkin/keycloak-js-util/blob/master/example/krakend.json\n  \n## ✅ running the example:\n\nthere is an example at https://github.com/shimondoodkin/keycloak-js-util/tree/master/example\n- you need to have doker installed.\n- git clone https://github.com/shimondoodkin/keycloak-js-util.git\n- in the example folder:\n- run all bat files:\n  - run-krakend-docker.bat\n  - run-keycloack-docker.bat\n  - run-server.bat or python3 server.py or node server.js\n  - run-vue-app.bat or enter folder: example\\vue-app and run:  npm run dev\n  - run-krakend-revoker-docker.bat - run it after krakend already started\n- configure keycloak like described in https://github.com/shimondoodkin/keycloak-js-util/tree/master/example/config ( describes how to overcome CORS and content security policy issues with keycloak) open key cloak at  http://localhost:8080\n- try login to http://localhost:8081\n\n##   Example 1\n\n```javascript\n\nimport {authInit,setOnRefreshTokenError,getKeycloak} from 'keycloak-js-util'\n\ntry {\n  await authInit({ \n    url: 'http://localhost:8080/', \n    realm: 'keycloak-demo', \n    clientId: 'app-vue',\n    //  onLoad:'login-required',\n    onLoad: 'check-sso',\n    silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html' \n  });\n\n  //optionally respont to authenticaation right away with  login\n  if(!getKeycloak().authenticated) \n  {\n    // if authentication is failed,  try to re-authenticate. don't load the application.\n    //  \n    //   if onLoad:'login-required' is used then maybe refresh window.location.reload().\n    //   or call getKeycloak().login() \n\n    console.log('not authorized')\n    getKeycloak().login()    \n\n  }\n  else\n  {\n    // authenticated successfuly, load the application\n\n    // add handler for refresh token error\n    setOnRefreshTokenError((error) =\u003e { \n      // for example:\n      // alert(\"dispaly message you have been disconnected.\\n\"+error.message);\n      // window.location.reload();\n    });\n\n    // load application here\n    // for example\n    //  createApp(App).mount('#app');\n  }\n}\ncatch(e)\n{\n  console.error(e)\n  // display error message\n  // for example\n  //  createApp(ErrorPage,{\n  //    title: 'Application initialization error:',\n  //    message: e.message,\n  //    returnUrl: '/',\n  //  }).mount('#app');\n}\n```\n\n\n\nSilent SSO:\nTo enable the silent check-sso, you have to provide a silentCheckSsoRedirectUri attribute in the init method. This URI needs to be a valid endpoint in the application (and of course it must be configured as a valid redirect for the client in the Keycloak Admin Console):\n```javascript\n  const authenticated=await authInit({\n        url: 'http://localhost:8080/',\n        realm: 'keycloak-demo', clientId: 'app-vue',\n        onLoad: 'check-sso',\n        silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html' });\n```\n\nThe page at the silent check-sso redirect uri is loaded in the iframe after successfully checking your authentication state and retrieving the tokens from the Keycloak server. It has no other task than sending the received tokens to the main application and should only look like this:\n\n```html\n\u003chtml\u003e\n\u003cbody\u003e\n    \u003cscript\u003e\n        parent.postMessage(location.href, location.origin)\n    \u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n\n\n\n\n##   Example 2\n\n```javascript\n\n\nimport {authFetchText,authFetchJSON,authFetch} from 'keycloak-js-util'\n\nconst data = await authFetchText('http://localhost:8082/v1/test.txt')\nconst data = await authFetchJSON('http://localhost:8082/v1/test.json')\nconst data = await authFetchBlob('http://localhost:8082/v1/test.jpg')\n\n\nconst response = await authFetch('http://localhost:8082/v1/test')\nif(!response.ok)\n  throw new Error('Http status code='+response.code)\nconst data = await response.json()\n\n```\n\n\n##   Example 3 - revoke and logout\n\n```javascript\n\n    async revokeLogout() {\n      stopTokenRefreshTimer();\n      await authFetchText('http://localhost:8082/v1/jwt-revoke')\n      window.keycloak.logout();\n    }\n```\n\n\n## 🗒 notes about the revoker\n\n- My modified simple revoker is at:\n  https://github.com/shimondoodkin/KrakenD-playground-community/blob/master/images/jwt-revoker/main.go#L38\n - I have added the addheader method. the idea was simply by calling the url it would revoke the token. The propogate claims gives the jti header to the revoker.\n - The krakend configuration for revoker is at: https://github.com/shimondoodkin/keycloak-js-util/blob/master/example/krakend.json#L56\n - The simple revoker has minor problems: https://github.com/shimondoodkin/KrakenD-playground-community/blob/master/images/jwt-revoker/usage.md#problems\n\n    \n## keycloak-js documentation:\n\nIn keycloak-js there are many useful functions like ` await window.keycloak.loadUserProfile() ` [example](example/vue-app/src/components/KeycloakExample.vue#L110)\n\n- The documentation for keycloak-js is at:\n  https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter\n\n- The reference for keycloak-js is at:\n  https://www.keycloak.org/docs/latest/securing_apps/index.html#javascript-adapter-reference\n\n- The source code of keycloak-js: https://github.com/keycloak/keycloak/tree/main/js/libs/keycloak-js\n\n- The npm pakcage: https://www.npmjs.com/package/keycloak-js\n\n\n```javascript\n// keycloak-js:\n//   reference: https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter\n//   source code of keycloak-js: https://github.com/keycloak/keycloak/tree/main/js/libs/keycloak-js\n//   npm pakcage: https://www.npmjs.com/package/keycloak-js\n```\n\nlist of methods of keycloak instance:\n\n```javascript\n  keycloak.init(options) // redirects if onLoad:'login-required'\n  keycloak.login(options) // redirects\n  keycloak.createLoginUrl(options)\n  keycloak.logout(options) // redirects\n  keycloak.createLogoutUrl(options)\n  keycloak.register(options) // redirects\n  keycloak.createRegisterUrl(options)\n  keycloak.accountManagement() // redirects\n  keycloak.createAccountUrl(options)\n  keycloak.hasRealmRole(role)\n  keycloak.hasResourceRole(role, resource)\n  keycloak.loadUserProfile()\n  keycloak.isTokenExpired(minValidity)\n  keycloak.updateToken(minValidity)\n  keycloak.clearToken()\n```\n\nlist of properties of keycloak instance:\n```javascript\n  keycloak.authenticated\n  keycloak.token\n  keycloak.tokenParsed\n  keycloak.subject\n  keycloak.idToken\n  keycloak.idTokenParsed\n  keycloak.realmAccess\n  keycloak.resourceAccess\n  keycloak.refreshToken\n  keycloak.refreshTokenParsed\n  keycloak.timeSkew\n  keycloak.responseMode\n  keycloak.flow\n  keycloak.adapter\n  keycloak.responseType\n```\n\nWritten by Shimon Doodkin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshimondoodkin%2Fkeycloak-js-util","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshimondoodkin%2Fkeycloak-js-util","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshimondoodkin%2Fkeycloak-js-util/lists"}