Documentation
    Preparing search index...

    Module Session Svelte

    @byu-oit-sdk/session-svelte

    Requirements:

    • Node.js 18+
      • or Node.js 10+ with fetch and crypto polyfills
    • npm v9+
    • Express v4
    npm i @byu-oit-sdk/session-svelte
    

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

    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.

    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:

    Example
    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 }
    }

    Interfaces

    SvelteSessionOptions

    Functions

    SessionHandler