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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dtdot/cognito-auth

v2.2.3

Published

An opinionated implementation of login / auth with AWS cognito

Downloads

222

Readme

About

This package is an opinionated implementation of cognito login mainly for use in my own projects. It contains login/registration screens, a context, and some other building blocks to assist in wiring up your authentication flows.

Constraints

This package has only been tested with email-based cognito auth

Usage

Step 1: Create the cognito user pool

There are some constraints required when setting up the user pool

  • Choose email as the primary login method
  • De-select any/all standard required attributes
  • When generating an app client be sure to un-tick "generate client secret"

Step 2: Add the CognitoAuthProvider at a high level in your app and configure with the required info

There are two broad categories of handling the auth flows that are supported

  1. Route-based login/registration where you have an individual route for each of the pages. Bind navigation calls like seen below for this method
  2. Storing the flow in state in a single component. This implementation would look similar but have some stored state and conditional rendering of the components
const cognitoConfig = {
  region: 'xxxxx',
  userPoolId: 'xxxxx',
  userPoolWebClientId: 'xxxxx',
};

const handleLoginFinished = () => {
  const { redirect } = qs.parse(window.location.search, { ignoreQueryPrefix: true });
  navigate(redirect ? redirect : '/');
};

const handleRegisterFinished = () => {
  navigate('/get-started');
};

const handleLoginAction = () => {
  const params = qs.parse(window.location.search, { ignoreQueryPrefix: true });
  navigate('/login', false, params);
};

const handleRegisterAction = () => {
  const params = qs.parse(window.location.search, { ignoreQueryPrefix: true });
  navigate('/register', false, params);
};

const handleVerificationAction = () => {
  const params = qs.parse(window.location.search, { ignoreQueryPrefix: true });
  navigate('/verify', false, params);
};

const App = () => (
  <CognitoAuthProvider
    {...cognitoConfig}
    handleLoginFinished={handleLoginFinished}
    handleRegisterFinished={handleRegisterFinished}
    handleLoginAction={handleLoginAction}
    handleRegisterAction={handleRegisterAction}
    handleVerificationAction={handleVerificationAction}
  >
    <Router />
  </CognitoAuthProvider>
);

Step 3: Add the register and login screens to your router

const navigateToRoot = () => {
  navigate('/', true);
};

const routes = {
  '/login': () => <LoginPage />,
  '/register': () => <RegisterPage />,
  '/verify': () => <VerificationPage />,
}

Step 4: Use the GuardedRoute and UnguardedRoute to control auth flows within your application

const NavigateToLoginComponent = () => {
  const path = window.location.pathname;
  navigate('/login', false, { redirect: path });
};

const NavigateToRootComponent = () => {
  navigate('/', true);
};

const routes = {
  '/profile': () => <GuardedRoute component={ProfilePage} authRequiredComponent={NavigateToLoginComponent} />,
  '/login': () => <UnguardedRoute component={LoginPage} strict={true} strictRouteViolationComponent={NavigateToRootComponent} />,
  '/register': () => <UnguardedRoute component={RegisterPage} strict={true} strictRouteViolationComponent={NavigateToRootComponent} />,
  '/verify': () => <UnguardedRoute component={VerificationPage} strict={true} strictRouteViolationComponent={NavigateToRootComponent} />,
}
  • GuardedRoute: This component can be used to trigger an action when an un-authenticated user navigates to that page
  • UnguardedRoute: This component can be used to allow un-authenticated users to access the page. If the strict option is set, then authenticated users can be directed away