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

@marzee/react-auth-amplify

v1.0.3

Published

A React.js-Amplify Auth integration

Downloads

15

Readme

@marzee/react-auth-amplify

A React.js-Amplify Auth integration

@marzee/react-auth-amplify provides a straightforward integration for email and password authentication with AWS cognito. Optionally, it integrates with Next.js.

Usage

  1. Create a file called lib/auth.ts and export the following object:
import { type AmplifyAuthConfig as Config } from "@marzee/react-auth-amplify";
export const amplifyAuthConfig: Config = {
  Auth: {
    identityPoolId: process.env.IDENTITY_POOL_ID,
    region: process.env.REGION,
    userPoolId: process.env.USER_POOL_ID,
    userPoolWebClientId: process.env.USER_POOL_WEB_CLIENT_ID
  }
  ssr: true // only if you plan on using SSR (with Next.js), otherwise simply omit it.
};
  1. Wrap your app using AuthProvider
import { AuthProvider } from "@marzeelabs/react-auth-amplify";
import { amplifyAuthConfig } from "lib/auth";

export function AppWrapper() {
  return (
    <AuthProvider
      config={amplifyAuthConfig}
      events={{
        signIn: (data) => {
          // do what you want after the signIn event has been fired (e.g., redirect to a page)
        }
      }}>
        <App />
    </AuthProvider>
  )
}
  1. Use the signIn/signOut/etc... functions in combination with your components to control the authentication flow (see API section below for all available functions)
import { useForm } from 'react-hook-form';
import { signIn } from '@marzeelabs/react-auth-amplify';

type Input = Parameters<typeof signIn>[0];

export const SignInComponent = () => {
  const { register, handleSubmit } = useForm<Input>();

  return (
    <form onSubmit={handleSubmit(signIn)}>
      <label htmlFor='email'>
        Email
        <input type='email' name='username' {...register('username')} />
      </label>
      <label htmlFor='password'>
        password
        <input
          type='password'
          name='password'
          autoComplete='current-password'
          {...register('password')}
        />
      </label>

      <button type='submit'>Sign In</button>
    </form>
  );
}
  1. Get the current user using the useAuthContext hook
import { useAuthContext } from '@marzeelabs/react-auth-amplify';


export const Component = () => {
  const { currentUser } = useAuthContext();
  return <div>...</div>
}
  1. If you want to extend the type of the currentUser you can do so by using module augmentation
declare module '@marzeelabs/react-auth-amplify' {
  export interface UserAttributes {
    // add your custom attributes here
  }
}
  1. If you to use this package in Next.js:
    1. you need to set the ssr option to true in the lib/auth.ts file (see below)
    2. Use the getServerAuth function to get the current user in the getServerSideProps function of your page
// your other imports...
import { amplifyAuthConfig } from "lib/auth";

export const getServerSideProps: GetServerSideProps = async (ctx) => {
  const currentUser = await getServerAuth({
    req: ctx.req,
    config: amplifyAuthConfig
  });
  return {
    props: {}
  };
};

// the rest of your page...

API

This API provides a convenient wrapper around the AWS Amplify Auth API. For reference see:

The following functions are available:

| Function Name | Description | | --- | --- | | signUp | Signs up a user with AWS Cognito using the provided username, password, and user attributes. | | confirmSignUp | Confirms a user's sign up with AWS Cognito using the provided username and confirmation code. | | signIn | Signs in a user with AWS Cognito using the provided username and password. | | signOut | Signs out the current user from AWS Cognito. | | changePassword | Changes the current user's password in AWS Cognito using the provided old and new passwords. | | forgotPassword | Initiates the process of resetting a user's password in AWS Cognito using the provided username. | | confirmNewPasswordAfterForgotPassword | Confirms a new password for a user after they have forgotten their password in AWS Cognito using the provided confirmation code, new password, and username. | | confirmUserEmail | Confirms a user's email address in AWS Cognito using the provided verification code. | | updateUserAttributes | Updates the current user's attributes in AWS Cognito using the provided partial user attributes. | | deleteUserAttributes | Deletes the specified attributes from the current user's attributes in AWS Cognito. | | deleteUser | Deletes the current user from AWS Cognito. | | getServerAuth | Returns the currently authenticated user in an SSR context. |

Install

npm i @marzee/react-auth-amplify aws-amplify@^5
pnpm i @marzee/react-auth-amplify aws-amplify@^5
yarn i @marzee/react-auth-amplify aws-amplify@^5

License

ISC