0.1.0 • Published 3 months ago

@byu-oit-sdk/session-svelte v0.1.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

@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:

OptionTypeDefaultDescription
storeSessionStoreStored in memoryThe store object that will be used to store and retrieve session information
namestring'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.
maxAgenumber1200The 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 }
}
0.1.0-beta.3

3 months ago

0.1.0

3 months ago

0.1.0-beta.2

4 months ago

0.1.0-beta.1

4 months ago

0.1.0-beta.0

4 months ago

0.0.1

8 months ago

0.0.1-beta.4

8 months ago

0.0.1-beta.3

8 months ago

0.0.1-beta.2

8 months ago

0.0.1-beta.1

9 months ago

0.0.1-beta.0

9 months ago