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

@sswahn/session

v1.0.0-beta.0

Published

This library provides a simple and efficient way to manage user sessions using AWS Cognito for authentication.

Downloads

3

Readme

Session · License npm version

This library provides a simple and efficient way to manage user sessions using AWS Cognito for authentication.

Features

  • Efficient Session Management: Simplify user session management in your AWS Cognito-authenticated applications.

  • Token Refresh Handling: The library seamlessly handles token refreshes, allowing you to update HTTP cookies when necessary.

  • Intuitive API: Provides a straightforward interface for managing sessions, making it easy to integrate into your Lambda functions.

  • Environment Variable Configuration: Utilize environment variables for flexible configuration.

  • DynamoDB Integration: Designed to work seamlessly with DynamoDB, the library assumes at least a minimal table design for storing session information.

Installation

Using npm.

npm install @sswahn/session

Usage

Import Session.

import session from '@sswahn/session'

Call the session function passing a cookie name and the Lambda event parameter.

const response = await session(cookieName, event)

Example

Note that if a token has been refreshed, you should set a new HTTP cookie. If a token has been refreshed, the session function's response will have a propety called cognito.

import session from '@sswahn/session'

export const handler = async event => {
  try {
    const data = await session('token', event)
    const { AccessToken } = data.cognito?.AuthenticationResult
    const cookie = AccessToken ? { 'Set-Cookie': `token=${AccessToken}; HttpOnly;` } : {}
    return {
      statusCode: 200,
      body: JSON.stringify({ message: 'Request successful' }),
      headers: {
        'Content-Type': 'application/json',
        ...cookie,
      }
    }
  } catch (error) {
    return {
      statusCode: 401,
      body: JSON.stringify({ error: 'Unauthorized' }),
      headers: {
        'Content-Type': 'application/json'
      }
    }
  }
}

Environmental Variables

Ensure the following environmental variables are set in your Lambda function:

  • SESSION_TABLE: The name of your DynamoDB table for storing session information.
  • PARTITION_KEY: The name of the partition key of your DynamoDB session table.
  • CLIENT_ID: The AWS Cognito client ID.

Database Design

This library assumes a DynamoDB table for storing session information.

  • The table's partition key must be the user's sub (subject) obtained from the decoded JWT token.
  • Session data should be stored when the user initially logs in, and removed when the user logs out.
  • The login and logout process is out of the scope of this library.
  • Then minimum requirements for the session table design should be as follows:
Table Name: your_session_table

Attributes:
- partition_key (String)
- token (String)
- timestamp (Number)

For the initial authentication with cognito you can implement your own system or use @sswahn/cognito.

License

Session is MIT Licensed