
svelte-session-manager
Session store for svelte (currently only for JWT)
usage
import { derived } from 'svelte';
import { Session, login } from 'svelte-session-manager';
let session = new Session(localStorage);
if(!session.isValid) {
await login(session, 'https://mydomain.com/authenticate', 'a user', 'a secret');
}
session.isValid;
export const values = derived(
session,
($session, set) => {
if (!session.isValid) {
set([]);
} else {
fetch('https://mydomain.com/values', {
headers: {
...session.authorizationHeader
}
}).then(async data => set(await data.json()));
}
return () => {};
}
,[]);
run tests
export BROWSER=safari|chrome|...
npm|yarn test
API
Table of Contents
SessionData
Data as preserved in the backing store
Type: Object
Properties
username
string user name (id)
access_token
string JWT token
Session
User session
To create as session backed by browser local storage
let session = new Session(localStorage);
or session storage
let session = new Session(sessionStorage);
Parameters
Properties
save
persist into the packing store
http header suitable for fetch
Returns Object header The http header.
Returns string header.Authorization The Bearer access token.
isValid
As long as the expirationTimer is running we must be valid
Returns 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
login
Bring session into the valid state by calling the authorization endpoint
and asking for a access_token.
Executess a POST on the endpoint url providing username, and password as json
Parameters
session
Session to be opened
endpoint
string authorization url
username
string id of the user
password
string user credentials
install
With npm do:
npm install svelte-session-manager
license
BSD-2-Clause