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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-cognito-auth-hook

v2.0.4

Published

Package for handling and managing user authentication in react applications

Readme

React Cognito Auth Hook

A React hook for handling AWS Cognito authentication flows with built-in token management and persistence.

Features

  • Complete AWS Cognito authentication flow
  • Token persistence using cookies
  • Automatic token refresh
  • TypeScript support
  • Handles login, signup, confirmation, and password changes
  • Built-in auth state management

Installation

npm install react-cognito-auth-hook

or

yarn add react-cognito-auth-hook

Prerequisites

Before using this package, you need:

  1. An AWS account with a configured Cognito User Pool
  2. A Cognito User Pool Client configured with the following:
    • Enabled USER_PASSWORD_AUTH, USER_AUTH authentication flows

You will need the following information from your AWS Cognito setup:

  • User Pool ID
  • User Pool Client ID
  • AWS Region

Configuration

First, create a configuration object with your AWS Cognito details:

const config = {
  region: "YOUR_AWS_REGION",
  userPoolId: "YOUR_USER_POOL_ID",
  clientId: "YOUR_CLIENT_ID",
};

Usage

Basic Authentication Flow

import { useCognitoAuth } from 'react-cognito-auth-hook';

function App() {
  const {
    authData,
    authState,
    login,
    initSignUp,
    confirmSignUp,
    signOut
  } = useCognitoAuth({ config });

  // Check authentication state
  if (authState === 'AUTHENTICATED') {
    return (
      <div>
        <h1>Welcome, {authData?.username}!</h1>
        <button onClick={signOut}>Sign Out</button>
      </div>
    );
  }

  return (
    <div>
      <h1>Please log in</h1>
      {/* Your login/signup forms here */}
    </div>
  );
}

Login

try {
  await login(username, password);
  // User is now logged in
} catch (error) {
  // Handle login error
}

Sign Up

try {
  // Initialize sign up
  await initSignUp(username, password);

  // Confirm sign up with code
  await confirmSignUp(username, confirmationCode);
  // User is now signed up and logged in
} catch (error) {
  // Handle signup error
}

Change Password

try {
  await changePassword(currentPassword, newPassword);
  // Password successfully changed
} catch (error) {
  // Handle password change error
}

Resend Confirmation Code

try {
  await resendConfirmationCode(username);
  // Confirmation code sent
} catch (error) {
  // Handle error
}

Forgot Password

try {
  // Initialize forgot password flow
  await initForgotPassword(username);
  // This will trigger Cognito to send a confirmation code

  // Confirm new password with code
  await confirmForgotPassword(username, newPassword, confirmationCode);
  // Password has been reset successfully
} catch (error) {
  // Handle forgot password error
}

Auth States

The hook provides the following authentication states:

  • INIT: Initial loading state
  • AUTHENTICATED: User is authenticated
  • UNAUTHENTICATED: User is not authenticated

API Reference

Hook Return Values

  • authData: Object containing the current authentication data (tokens, username)
  • authState: Current authentication state
  • login(username: string, password: string): Login with username and password
  • initSignUp(username: string, password: string, passwordConfirm: string): Start the signup process
  • confirmSignUp(username: string, code: string, callback?: Function): Confirm signup with verification code
  • resendConfirmationCode(username: string): Resend verification code
  • signOut(): Sign out the current user
  • changePassword(prevPassword: string, newPassword: string, newPasswordConfirm: string): Change user's password
  • initForgotPassword(username: string): Start the forgot password process
  • confirmForgotPassword(username: string, newPassword: string, code: string): Confirm new password with verification code

Security

This package handles authentication tokens securely by:

  • Storing tokens in HTTP-only cookies
  • Automatically refreshing tokens
  • Validating tokens before use
  • Clearing tokens on sign out

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.