Skip to content

Source code and demo for a React useAuth hook with AWS Cognito.

Notifications You must be signed in to change notification settings

kwhitejr/use-auth

Repository files navigation

UseAuth Hook

Source code and test for a React useAuth hook implemented with AWS Amplify Auth.

Usage

// AppRoot.jsx
import React from 'react';

import App from './app'; // uses <MyComponent />
import { ProvideAuth } from './use-auth';

return (
  <ProvideAuth>
    <App />
  </ProvideAuth>
);

// MyComponent.jsx
import React from 'react';

import { useAuth } from './use-auth';

function MyComponent() {
  const { isSignedIn, user, signIn, signOut } = useAuth();

  return (
    <div>
      <div>{`IsSignedIn: ${isSignedIn}`}</div>
      <div>{`Username: ${user?.username}`}</div>
      {isSignedIn ? (
        <button onClick={signOut} type="button">Sign Out</button>
      ) : (
        <button onClick={signIn} type="button">Sign In</button>
      )}
    </div>
  )
};

Run Tests

npm install && npm run test

About

Source code and demo for a React useAuth hook with AWS Cognito.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published