svelte-session-manager
Advanced tools
Comparing version
{ | ||
"name": "svelte-session-manager", | ||
"version": "1.0.1", | ||
"module": "src/index.mjs", | ||
"svelte": "src/index.svelte", | ||
"description": "Session store for svelte (currently only for JWT)", | ||
"version": "1.0.0", | ||
"module": "src/index.svelte", | ||
"svelte": "src/index.svelte", | ||
"keywords": [ | ||
@@ -13,17 +13,20 @@ "svelte", | ||
"scripts": { | ||
"docs": "documentation readme src/index.mjs --section=API", | ||
"lint": "documentation lint src/index.mjs", | ||
"start": "rollup -c example/rollup.config.js -w", | ||
"test": "testcafe chromium:headless tests/*-test.js -s test-results --app-init-delay 500 --app \"rollup -c example/rollup.config.js -w\"" | ||
"test": "testcafe $BROWSER:headless tests/*-test.js -s test-results --app-init-delay 500 --app \"rollup -c example/rollup.config.js -w\"" | ||
}, | ||
"devDependencies": { | ||
"documentation": "^12.1.2", | ||
"jsonwebtoken": "^8.5.1", | ||
"rollup": "^1.21.2", | ||
"rollup": "^1.23.1", | ||
"rollup-plugin-dev": "1.0.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-svelte": "^5.1.0", | ||
"semantic-release": "^15.13.24", | ||
"svelte": "^3.12.0", | ||
"testcafe": "^1.4.3" | ||
"semantic-release": "^15.13.25", | ||
"svelte": "^3.12.1", | ||
"testcafe": "^1.5.0" | ||
}, | ||
"engines": { | ||
"node": ">=12.10.0" | ||
"node": ">=12.11.1" | ||
}, | ||
@@ -30,0 +33,0 @@ "repository": { |
162
README.md
@@ -0,2 +1,162 @@ | ||
[](http://travis-ci.org/arlac77/svelte-session-manager) | ||
[](http://codecov.io/github/arlac77/svelte-session-manager?branch=master) | ||
[](http://commitizen.github.io/cz-cli/) | ||
[](https://coveralls.io/r/arlac77/svelte-session-manager) | ||
[](https://david-dm.org/arlac77/svelte-session-manager) | ||
[](https://david-dm.org/arlac77/svelte-session-manager#info=devDependencies) | ||
[](http://inch-ci.org/github/arlac77/svelte-session-manager) | ||
[](https://npmjs.org/package/svelte-session-manager) | ||
[](https://github.com/arlac77/svelte-session-manager/issues) | ||
[](https://greenkeeper.io/) | ||
[](https://snyk.io/test/github/arlac77/svelte-session-manager) | ||
[](https://bundlephobia.com/result?p=svelte-session-manager) | ||
[](https://www.npmjs.com/package/svelte-session-manager) | ||
[](https://github.com/arlac77/svelte-session-manager) | ||
[](https://github.com/prettier/prettier) | ||
[](https://greenkeeper.io/) | ||
# svelte-session-manager | ||
Session store for svelte (currently only for JWT) | ||
# usage | ||
```js | ||
import { derived } from 'svelte'; | ||
import { Session, login } from 'svelte-session-manager'; | ||
// use localStorage as backng store | ||
let session = new Session(localStorage); | ||
// session may still be valid | ||
if(!session.isValid) { | ||
await login(session, 'https://mydomain.com/authenticate', 'a user', 'a secret'); | ||
} | ||
session.isValid; // true when auth was ok | ||
export const values = derived( | ||
session, | ||
($session, set) => { | ||
if (!session.isValid) { | ||
set([]); // session has expired no more data | ||
} else { | ||
fetch('https://mydomain.com/values', { | ||
headers: { | ||
...session.authorizationHeader | ||
} | ||
}).then(async data => set(await data.json())); | ||
} | ||
return () => {}; | ||
} | ||
); | ||
// $values contains fetch result as long as session has not expired | ||
``` | ||
## run tests | ||
```sh | ||
export BROWSER=safari|chrome|... | ||
npm|yarn test | ||
``` | ||
# API | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
### Table of Contents | ||
- [SessionData](#sessiondata) | ||
- [Properties](#properties) | ||
- [Session](#session) | ||
- [Parameters](#parameters) | ||
- [Properties](#properties-1) | ||
- [save](#save) | ||
- [authorizationHeader](#authorizationheader) | ||
- [isValid](#isvalid) | ||
- [invalidate](#invalidate) | ||
- [subscribe](#subscribe) | ||
- [Parameters](#parameters-1) | ||
- [login](#login) | ||
- [Parameters](#parameters-2) | ||
## SessionData | ||
Data as preserved in the backing store | ||
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | ||
### Properties | ||
- `username` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** user name (id) | ||
- `access_token` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** JWT token | ||
## Session | ||
User session | ||
To create as session backed by browser local storage | ||
```js | ||
let session = new Session(localStorage); | ||
``` | ||
### Parameters | ||
- `data` **[SessionData](#sessiondata)** | ||
### Properties | ||
- `entitlements` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** | ||
- `expirationDate` **[Date](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date)** | ||
- `store` **[SessionData](#sessiondata)** backing store to use for save same as data param | ||
### save | ||
persist into the packing store | ||
### authorizationHeader | ||
http header suitable for fetch | ||
### isValid | ||
As long as the expirationTimer is running we must be valid | ||
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if session is valid (not expired) | ||
### invalidate | ||
remove all tokens from the session and the backing store | ||
### subscribe | ||
Fired when the session changes | ||
#### Parameters | ||
- `subscription` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** | ||
## login | ||
Bring session into the valid state by callinf the authorization endpoint | ||
and asking for a access_token | ||
### Parameters | ||
- `session` **[Session](#session)** to be opened | ||
- `endpoint` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** authorization url | ||
- `username` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** id of the user | ||
- `password` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** user credentials | ||
# install | ||
With [npm](http://npmjs.org) do: | ||
```shell | ||
npm install svelte-session-manager | ||
``` | ||
# license | ||
BSD-2-Clause |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18376
63.91%9
12.5%173
25.36%163
5333.33%9
12.5%