Session store for svelte (currently only for JWT)
npm install svelte-session-manager










Session store for svelte (currently only for JWT)
``js
import { derived } from 'svelte';
import { Session, login } from 'svelte-session-manager';
// use localStorage as backing 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 or localStorage token is still valid
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
`
`sh`
export BROWSER=safari|chrome|...
npm|yarn test
The test runs the following requests against the server
* successful auth
`sh`
curl -X POST -d '{"username":"user","password":"secret"}' 'http://[::]:5000/api/login'
{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbnRpdGxlbWVudHMiOiJhLGIsYyIsImlhdCI6MTYwNDY2NDI0NywiZXhwIjoxNjA0NjY0MjYyfQ.qyjeoCuXO0iyYwSxM2sM02_BVhaZobRmEWam1M8Hzkx51nbsAuTR8G1rNgz1COo_KvbCU7LwZt7qnSEFB1tcwyDA1eBxwc2Wb7JxWgQ50m1IWkr2JCgY1seWRJRcwZBXiTRtiPqhzofP-l3S-CBluzU48cd4yzoPayczLkKuPK4"}
* invalid credentials
`sh`
curl -X POST -d '{"username":"user","password":"wrong"}' 'http://[::]:5000/api/login'
{"message":"Unauthorized"}
* login
* Parameters
* handleFailedResponse
* Parameters
* SessionData
* Properties
* msecsRequiredForRefresh
* supportedTokenTypes
* Session
* Parameters
* Properties
* update
* Parameters
* refresh
* authorizationHeader
* isValid
* invalidate
* hasEntitlement
* Parameters
* subscribe
* Parameters
* decode
* Parameters
Bring session into the valid state by calling the authorization endpoint
and asking for a access\_token.
Executes a POST on the endpoint url expecting username, and password as json
* session Session to be openedendpoint
* string authorization urlusername
* string id of the userpassword
* string user credentialstokenmap
* Object token names in response to internal known values (optional, default defaultTokenMap)
* tokenmap.access_token string
Returns Promise<(string | undefined)> error message in case of failure or undefined on success
Extract error description from response.
* response Response
Data as preserved in the backing store.
Type: Object
* username string user name (id)access_token
* string JWT tokenrefresh_token
* string JWT token
Time required to execute a refresh
Type: number
User session.
To create as session backed by browser local storage.
`js`
let session = new Session(localStorage);
or by browser session storage
`js`
let session = new Session(sessionStorage);
* store SessionData (optional, default localStorage)
* entitlements Set<string> subscriptions
* Set<Object> store subscriptionsexpirationDate
* Date when the access token expiresaccess_token
* string token itselfrefresh_token
* string refresh token
Consume auth response data and reflect internal state.
#### Parameters
* data Object
* data.entitlements string? data.token_type
* string? data.expires_in
* number? data.access_token
* string?
Refresh with refresh\_token.
Returns Promise<boolean> true if refresh was succcessfull false otherwise
Http header suitable for fetch.
Returns ({Authorization: string} | {}) The Bearer access token.
As long as the expirationTimer is running we must be valid.
Returns boolean true if session is valid (not expired)
Remove all tokens from the session and the backing store.
Check presence of an entitlement.
#### Parameters
* name string of the entitlement
Returns boolean true if the named entitlement is present
Fired when the session changes.
#### Parameters
* subscription Function
Extract and decode the payload.
* token string
Returns (Object | undefined) payload object
With npm do:
`shell``
npm install svelte-session-manager
BSD-2-Clause