{"id":13538284,"url":"https://github.com/arlac77/svelte-session-manager","last_synced_at":"2025-04-12T22:27:46.799Z","repository":{"id":35111285,"uuid":"201442335","full_name":"arlac77/svelte-session-manager","owner":"arlac77","description":"Session store for svelte (currently only for JWT)","archived":false,"fork":false,"pushed_at":"2025-04-09T19:11:39.000Z","size":15292,"stargazers_count":105,"open_issues_count":9,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T19:45:38.582Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"0bsd","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arlac77.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":"2019-08-09T10:03:44.000Z","updated_at":"2025-04-09T19:11:42.000Z","dependencies_parsed_at":"2024-02-05T10:53:15.255Z","dependency_job_id":"e0e567ae-f2de-4277-9e0a-2d94a1dbafb9","html_url":"https://github.com/arlac77/svelte-session-manager","commit_stats":{"total_commits":1737,"total_committers":11,"mean_commits":157.9090909090909,"dds":"0.26827864133563617","last_synced_commit":"7e7f395614f36c7c5f40410796154ef9b08bada9"},"previous_names":[],"tags_count":112,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arlac77%2Fsvelte-session-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arlac77%2Fsvelte-session-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arlac77%2Fsvelte-session-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arlac77%2Fsvelte-session-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arlac77","download_url":"https://codeload.github.com/arlac77/svelte-session-manager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248639180,"owners_count":21137794,"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","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":[],"created_at":"2024-08-01T09:01:09.255Z","updated_at":"2025-04-12T22:27:46.773Z","avatar_url":"https://github.com/arlac77.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Svelte v5](https://img.shields.io/badge/svelte-v5-orange.svg)](https://svelte.dev)\n[![npm](https://img.shields.io/npm/v/svelte-session-manager.svg)](https://www.npmjs.com/package/svelte-session-manager)\n[![License](https://img.shields.io/badge/License-0BSD-blue.svg)](https://spdx.org/licenses/0BSD.html)\n[![bundlejs](https://deno.bundlejs.com/?q=svelte-session-manager\\\u0026badge=detailed)](https://bundlejs.com/?q=svelte-session-manager)\n[![downloads](http://img.shields.io/npm/dm/svelte-session-manager.svg?style=flat-square)](https://npmjs.org/package/svelte-session-manager)\n[![GitHub Issues](https://img.shields.io/github/issues/arlac77/svelte-session-manager.svg?style=flat-square)](https://github.com/arlac77/svelte-session-manager/issues)\n[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Farlac77%2Fsvelte-session-manager%2Fbadge\\\u0026style=flat)](https://actions-badge.atrox.dev/arlac77/svelte-session-manager/goto)\n[![Styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![Known Vulnerabilities](https://snyk.io/test/github/arlac77/svelte-session-manager/badge.svg)](https://snyk.io/test/github/arlac77/svelte-session-manager)\n[![Coverage Status](https://coveralls.io/repos/arlac77/svelte-session-manager/badge.svg)](https://coveralls.io/github/arlac77/svelte-session-manager)\n[![Tested with TestCafe](https://img.shields.io/badge/tested%20with-TestCafe-2fa4cf.svg)](https://github.com/DevExpress/testcafe)\n\n# svelte-session-manager\n\nSession store for svelte (currently only for JWT)\n\n# usage\n\n```js\nimport { derived } from 'svelte';\nimport { Session, login } from 'svelte-session-manager';\n\n// use localStorage as backing store\nlet session = new Session(localStorage);\n\n// session may still be valid\nif(!session.isValid) {\n  await login(session, 'https://mydomain.com/authenticate', 'a user', 'a secret');\n}\n\nsession.isValid; // true when auth was ok or localStorage token is still valid\n\n\nexport const values = derived(\n  session,\n  ($session, set) =\u003e {\n    if (!session.isValid) {\n      set([]); // session has expired no more data\n    } else {\n      fetch('https://mydomain.com/values', {\n        headers: {\n          ...session.authorizationHeader\n        }\n      }).then(async data =\u003e set(await data.json()));\n    }\n    return () =\u003e {};\n  }\n,[]);\n\n// $values contains fetch result as long as session has not expired\n```\n\n## run tests\n\n```sh\nexport BROWSER=safari|chrome|...\nnpm|yarn test\n```\n\nThe test runs the following requests against the server\n\n*   successful auth\n\n```sh\ncurl -X POST -d '{\"username\":\"user\",\"password\":\"secret\"}' 'http://[::]:5000/api/login'\n```\n\n    {\"access_token\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbnRpdGxlbWVudHMiOiJhLGIsYyIsImlhdCI6MTYwNDY2NDI0NywiZXhwIjoxNjA0NjY0MjYyfQ.qyjeoCuXO0iyYwSxM2sM02_BVhaZobRmEWam1M8Hzkx51nbsAuTR8G1rNgz1COo_KvbCU7LwZt7qnSEFB1tcwyDA1eBxwc2Wb7JxWgQ50m1IWkr2JCgY1seWRJRcwZBXiTRtiPqhzofP-l3S-CBluzU48cd4yzoPayczLkKuPK4\"}\n\n*   invalid credentials\n\n```sh\ncurl -X POST -d '{\"username\":\"user\",\"password\":\"wrong\"}' 'http://[::]:5000/api/login'\n```\n\n    {\"message\":\"Unauthorized\"}\n\n## Live Example\n\n[live example](https://svelte-session-manager.pages.dev/)\n\n# API\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n### Table of Contents\n\n*   [login](#login)\n    *   [Parameters](#parameters)\n*   [handleFailedResponse](#handlefailedresponse)\n    *   [Parameters](#parameters-1)\n*   [SessionData](#sessiondata)\n    *   [Properties](#properties)\n*   [msecsRequiredForRefresh](#msecsrequiredforrefresh)\n*   [supportedTokenTypes](#supportedtokentypes)\n*   [Session](#session)\n    *   [Parameters](#parameters-2)\n    *   [Properties](#properties-1)\n    *   [update](#update)\n        *   [Parameters](#parameters-3)\n    *   [refresh](#refresh)\n    *   [authorizationHeader](#authorizationheader)\n    *   [isValid](#isvalid)\n    *   [invalidate](#invalidate)\n    *   [hasEntitlement](#hasentitlement)\n        *   [Parameters](#parameters-4)\n    *   [subscribe](#subscribe)\n        *   [Parameters](#parameters-5)\n*   [decode](#decode)\n    *   [Parameters](#parameters-6)\n\n## login\n\nBring session into the valid state by calling the authorization endpoint\nand asking for a access\\_token.\nExecutes a POST on the endpoint url expecting username, and password as json\n\n### Parameters\n\n*   `session` **[Session](#session)** to be opened\n*   `endpoint` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** authorization url\n*   `username` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** id of the user\n*   `password` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** user credentials\n*   `tokenmap` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** token names in response to internal known values (optional, default `defaultTokenMap`)\n\nReturns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u003c([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))\u003e** error message in case of failure or undefined on success\n\n## handleFailedResponse\n\nExtract error description from response.\n\n### Parameters\n\n*   `response` **[Response](https://developer.mozilla.org/docs/Web/Guide/HTML/HTML5)**\u0026#x20;\n\nReturns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u003c[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)\u003e**\u0026#x20;\n\n## SessionData\n\nData as preserved in the backing store.\n\nType: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)\n\n### Properties\n\n*   `username` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** user name (id)\n*   `access_token` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** JWT token\n*   `refresh_token` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** JWT token\n\n## msecsRequiredForRefresh\n\nTime required to execute a refresh\n\nType: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)\n\n## supportedTokenTypes\n\n## Session\n\nUser session.\nTo create as session backed by browser local storage.\n\n```js\nlet session = new Session(localStorage);\n```\n\nor by browser session storage\n\n```js\nlet session = new Session(sessionStorage);\n```\n\n### Parameters\n\n*   `store` **[SessionData](#sessiondata)**  (optional, default `localStorage`)\n\n### Properties\n\n*   `entitlements` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\u003c[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)\u003e**\u0026#x20;\n*   `subscriptions` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\u003c[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** store subscriptions\n*   `expirationDate` **[Date](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date)** when the access token expires\n*   `access_token` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** token itself\n*   `refresh_token` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** refresh token\n\n### update\n\nConsume auth response data and reflect internal state.\n\n#### Parameters\n\n*   `data` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**\u0026#x20;\n\n### refresh\n\nRefresh with refresh\\_token.\n\nReturns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u003c[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)\u003e** true if refresh was succcessfull false otherwise\n\n### authorizationHeader\n\nHttp header suitable for fetch.\n\nReturns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** header The http header.\n\nReturns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** header.Authorization The Bearer access token.\n\n### isValid\n\nAs long as the expirationTimer is running we must be valid.\n\nReturns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if session is valid (not expired)\n\n### invalidate\n\nRemove all tokens from the session and the backing store.\n\n### hasEntitlement\n\nCheck presence of an entitlement.\n\n#### Parameters\n\n*   `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** of the entitlement\n\nReturns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if the named entitlement is present\n\n### subscribe\n\nFired when the session changes.\n\n#### Parameters\n\n*   `subscription` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**\u0026#x20;\n\n## decode\n\nExtract and decode the payload.\n\n### Parameters\n\n*   `token` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\u0026#x20;\n\nReturns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** payload object\n\n# install\n\nWith [npm](http://npmjs.org) do:\n\n```shell\nnpm install svelte-session-manager\n```\n\n# license\n\nBSD-2-Clause\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farlac77%2Fsvelte-session-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farlac77%2Fsvelte-session-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farlac77%2Fsvelte-session-manager/lists"}