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

cognito-auth-storage

v1.1.2

Published

Replace AWS Cognito default client-side storage with a custom javascript object

Downloads

8

Readme

cognito-auth-storage

AWS Amplify Auth (Cognito) lets you replace the default Token storage class (which uses localStorage) with your own. This repo has two such classes, one for in-memory storage and another for IndexedDb storage.

AuthStorageMemory.js

AuthStorageMemory is a simple class that stores the Tokens in memory in JavaScript, instead of in localStorage. This class is mainly for illustration purposes. Keeping the Tokens in memory does decrease their visibility somewhat, but functionality is also impacted, since they will be destroyed when the session is closed. The effect of the Refresh Token is largely mooted, unless the User keeps the browser session open for multiple days.

AuthStorageIDB.js

AuthStorageIDB is a more useful class, which stores the Tokens in IndexedDB instead of in localStorage. This makes them accessible to Service Workers, so for instance if you have a BackgroundSync process (on Chrome) that hits your REST API, the Tokens will be available to you. Be aware that IndexedDB has an asynchronous interface, while AWS Amplify expects Cognito Auth storage to be synchronous. Since they are both operating locally within the same browser this is not a severe issue, but you will probably want to manually invoke the sync() method immediately when the user connects, to see if IndexedDB says that they are already logged in. The rest of the time, for the most part this class directly uses in-memory storage for its source-of-truth, and mirrors the in-memory activity with appropriate writes back to IndexedDB.

Usage

To use either class typically you will import the storage class you want to use:

import {AuthStorageMemory} from 'cognito-auth-storage';

And also import your aws-exports.js (created by the amplify cli) like this:

import myconfig from './aws-exports';

Then if you are using AuthStorageMemory, customize the storage class with:

myconfig.storage = new AuthStorageMemory();

If you are using AuthStorageIDB, it's a little more involved after importing. First step:

AuthStorageIDB.init(errorCallback, successCallback);

The callbacks are for initial setup. The errorCallback receives one argument, which is simply the error message. The successCallback receives one argument, which is an object that is suitable for use as myconfig.storage. The reason that AuthStorageIDB works this way is because it is not 100% certain that IndexedDB will be available, depending on your browser. If not, this approach provides a means to delete the myconfig.storage property, which in turn signals to Amplify to use its own default storage (localStorage). So in the successCallback, to complete the setup you must do (second step):

myconfig.storage = argReceivedByTheSuccessCallback;

or in the errorCallback you would do (alternate second step);

delete myconfig.storage;

to fall back to localStorage.

Regardless which class you are using, make sure you set the correct storage property within the myconfig object. For instance AWS Amplify in its terminology calls S3 "Storage" but it is not that Storage that we are dealing with here. You want the one that belongs to "Auth" (which is AWS Amplify's name for Cognito). Depending on how many features of Amplify you are using, and how you set up your configuration, this may vary. For instance if you are using a myconfig file with multiple sections, i.e., containing JSON object properties such as API, Auth, etc., you want the ".storage" property of Auth.

Finally, near the end of your callback don't forget the statement to actually use your myconfig object:

Amplify.configure(myconfig);

Notes

The AuthStorageIDB class has a dependency on the npm 'idb' module, which is a very thin wrapper over IndexedDB.

AWS does not really publish a spec for the storage class. The slightly different interface implementations in our two classes (e.g., static/non-static methods, sync() method, etc.) seem to be satisfactory to Amplify.