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

@byu-oit-sdk/session-svelte

v0.1.0

Published

A set of svelte Handles for handling session authentication

Downloads

352

Readme

@byu-oit-sdk/session-svelte

Requirements:

  • Node.js 18+
    • or Node.js 10+ with fetch and crypto polyfills
  • npm v9+
  • Express v4

Install

npm i @byu-oit-sdk/session-svelte

Introduction

Use this module to facilitate session authentication in SvelteKit webapps, used primarily in conjunction with the @byu-oit-sdk/svelte authentication package.

Options

The only parameter passed into the SessionHandler function (see the example below) is an object where the following options can be set:

| Option | Type | Default | Description | |--------|--------------|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | store | SessionStore | Stored in memory | The store object that will be used to store and retrieve session information | | name | string | 'sessionId' | The name of the cookie used to store the session Id in the browser storage. If overriding the default and if you are also using @byu-oit-sdk/svelte, this value must be identical to the value passed in to @byu-oit-sdk/svelte constructor. | | maxAge | number | 1200 | The maximum age of the session, in seconds. |

None of the options are required to be overridden for testing, but store must be overridden for production use.

Usage

Register the return value of SessionHandler as a SvelteKit 'Handle' hook:

On its own, this package creates a session object that can be used to exports a handle that runs on every request that stores data in event.data.session

import { SessionHandler } from '@byu-oit-sdk/session-svelte'
import type { Handle } from '@sveltejs/kit'
import env from 'env-var'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoSessionStore } from '@byu-oit-sdk/session-dynamo'

export const handle = await (async () => {
  // this file runs (twice!) when running `vite build`, so if we are building, skip this code block.
  // See https://github.com/sveltejs/kit/issues/8795
  if (building) {
    return sequence()
  }
  // this is one example of how you could dynamically switch between using dynamo and in-memory stores for local and production use.
  const isProduction = env.get('NODE_ENV').default('development').asEnum(['production', 'development']) === 'production'

  let store // if store is undefined (e.g. for local development), an in-memory store will be used
  if (isProduction) {
    const client = new DynamoDBClient({
      region: env.get('AWS_REGION').required().asString()
    })
    store = new DynamoSessionStore({ client, tableName: 'sessions' })
  }

  const sessionHandle = await SessionHandler({ store })
  return await SessionHandler({ store })
})()

Data stored in the session will now be stored in event.locals.session in server-side SvelteKit functions. It will be saved back to the session store at the end of the request. Here is an example of how you could access and change some of the session data in a +layout.server.ts load function:

import type { LayoutServerLoad } from './$types'

export const load: LayoutServerLoad = (event) => {
  const oldLastPageLoaded = event.locals.session.get('last_page_visited')
  event.locals.session.set('last_page_visited', event.url.pathname)
  return { previousPage: oldLastPageLoaded }
}