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

@yourstruly-de/tuic-amplify-storage

v2.0.0

Published

A specialized AWS Amplify Auth Storage solution that splits Cognito Token storage between cookies and local storage based on the token type.

Downloads

22

Readme

TUIC Mixed Amplify Auth Storage

A specialized AWS Amplify Auth Storage solution that splits Cognito Token storage between cookies and local storage based on the token type.

Why?

This library was developed as an improved alternative to Cognito's default cookie storage approach. With Cognito's standard method, the generated cookies tend to grow substantially in size, often approaching the limits imposed by servers. Such large cookies can impact performance and lead to unexpected issues.

TUIC Mixed Amplify Auth Storage addresses this challenge by partitioning the data storage:

  • Cookies: Primarily reserved for the refresh token and the last authenticated user values. Storing these crucial pieces of data in cookies ensures accessibility even when local storage might not be available, such as during server-side rendering or in specific browser scenarios.
  • Local Storage: Used for storing all other values. Local storage, being client-side and not transmitted with every HTTP request, is an ideal place for bulkier data, ensuring the website's cookies remain lean.

By adopting this split-storage approach, websites can significantly reduce both the number and the overall size of cookies, leading to optimized server communication and better overall site performance.

Installation

Installing the library directly from npm:

$ npm i -S @yourstruly-de/tuic-amplify-storage

Usage

After installation, make sure to properly configure the library and supply it to amplify's auth storage config.

const cookieHeaders = {};

// Refer to `Configuration Options` for comprehensive configuration details.
const mixedStore = createMixedStore({
  debug: false,

  cookies: {
    // Provide the relevant cookie headers, typically `request.cookies` or `request.headers.cookie`.
    // Without these headers, the store won't be able to read the cookies, affecting its functionality.
    headers: cookieHeaders,
    setOptions: {
      domain: '.example.com',
      maxAge: 30 * 24 * 60 * 60, // 30 days
      path: '/',
    },
  },
});

const authConfig = {
  identityPoolId: '<your-id>',
  region: '<your-region>',
  userPoolId: '<your-user-pool-id>',
  userPoolWebClientId: '<your-user-pool-web-client-id>',
  // ... other configurations

  storage: mixedStore,
};

Amplify.configure({
  Auth: {
    ...authConfig,
  },
  ssr: true,
});

Configuration Options

Delve into the full set of configuration options.

  • debug (Optional)

    • Type: boolean
    • Description: Indicates if debugging is enabled. If true, additional logging and diagnostics related to store operations may be emitted.
  • cookies (Optional)

    • Type: CreateCookieStoreConfig

    • Description: Configuration options for the cookie store.

    • headers (Optional)

      • Type: CookieHeaders
      • Description: Headers related to the cookie. It can be a string or an object mapping keys to string values.
    • setOptions (Optional)

      • Type: CookieSetOptions

      • Description: Configuration options when setting a cookie.

      • path (Optional)

        • Description: Specifies the path for the cookie.
      • expires (Optional)

        • Description: Sets the expiry date for the cookie.
      • maxAge (Optional)

        • Description: Sets the maximum age in seconds for the cookie.
      • domain (Optional)

        • Description: Specifies the domain for the cookie.
      • secure (Optional)

        • Description: Indicates if the cookie transmission requires a secure protocol.
      • httpOnly (Optional)

        • Description: Specifies if the cookie is HTTP only.
      • sameSite (Optional)

        • Description: Sets the SameSite attribute for the cookie. Can be true, false, 'none', 'lax', or 'strict'.
      • encode (Optional)

        • Description: A function to encode the value of the cookie.
  • storeFactories (Optional)

    • Type: Partial<Record<StoreName, () => Store>>
    • Description: An object mapping store names to factory functions that create stores. This allows for custom implementations or configurations for specific types of stores.

Usage in Browser

If you need to use the MixedStore directly in a browser where you don't have access to a bundler, load browser.bundle.min.js in your script tag:

<script src="node_modules/@yourstruly-de/tuic-amplify-storage/dist/browser.bundle.min.js"></script>

<script>
  const mixedStore = window._yourstruly_de_tuic_amplify_storage.createMixedStore({
    debug: false,
    cookies: {
      setOptions: {
        domain: '.example.com',
        maxAge: 30 * 24 * 60 * 60, // 30 days
        path: '/',
      },
    },
  });

  const authConfig = {
    identityPoolId: '<your-id>',
    region: '<your-region>',
    userPoolId: '<your-user-pool-id>',
    userPoolWebClientId: '<your-user-pool-web-client-id>',
    // ... other configurations

    storage: mixedStore,
  };

  Amplify.configure({
    Auth: {
      ...authConfig,
    },
  });
</script>