npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-session-manager

v3.0.16

Published

Session store for svelte (currently only for JWT)

Downloads

3,434

Readme

Svelte v5 npm License bundlejs downloads GitHub Issues Build Status Styled with prettier Commitizen friendly Known Vulnerabilities Coverage Status Tested with TestCafe

svelte-session-manager

Session store for svelte (currently only for JWT)

usage

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

run tests

export BROWSER=safari|chrome|...
npm|yarn test

The test runs the following requests against the server

  • successful auth
curl -X POST -d '{"username":"user","password":"secret"}' 'http://[::]:5000/api/login'
{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbnRpdGxlbWVudHMiOiJhLGIsYyIsImlhdCI6MTYwNDY2NDI0NywiZXhwIjoxNjA0NjY0MjYyfQ.qyjeoCuXO0iyYwSxM2sM02_BVhaZobRmEWam1M8Hzkx51nbsAuTR8G1rNgz1COo_KvbCU7LwZt7qnSEFB1tcwyDA1eBxwc2Wb7JxWgQ50m1IWkr2JCgY1seWRJRcwZBXiTRtiPqhzofP-l3S-CBluzU48cd4yzoPayczLkKuPK4"}
  • invalid credentials
curl -X POST -d '{"username":"user","password":"wrong"}' 'http://[::]:5000/api/login'
{"message":"Unauthorized"}

Live Example

live example

API

Table of Contents

login

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

Parameters

  • session Session to be opened
  • endpoint string authorization url
  • username string id of the user
  • password string user credentials
  • tokenmap Object token names in response to internal known values (optional, default defaultTokenMap)

Returns Promise<(string | undefined)> error message in case of failure or undefined on success

handleFailedResponse

Extract error description from response.

Parameters

Returns Promise<string>

SessionData

Data as preserved in the backing store.

Type: Object

Properties

msecsRequiredForRefresh

Time required to execute a refresh

Type: number

supportedTokenTypes

Session

User session. To create as session backed by browser local storage.

let session = new Session(localStorage);

or by browser session storage

let session = new Session(sessionStorage);

Parameters

Properties

  • entitlements Set<string>
  • subscriptions Set<Object> store subscriptions
  • expirationDate Date when the access token expires
  • access_token string token itself
  • refresh_token string refresh token

update

Consume auth response data and reflect internal state.

Parameters

refresh

Refresh with refresh_token.

Returns Promise<boolean> true if refresh was succcessfull false otherwise

authorizationHeader

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.

hasEntitlement

Check presence of an entitlement.

Parameters

  • name string of the entitlement

Returns boolean true if the named entitlement is present

subscribe

Fired when the session changes.

Parameters

decode

Extract and decode the payload.

Parameters

Returns Object payload object

install

With npm do:

npm install svelte-session-manager

license

BSD-2-Clause