1.7.48 • Published 4 days ago

@ttoss/react-auth v1.7.48

Weekly downloads
-
License
-
Repository
github
Last release
4 days ago

@ttoss/react-auth

About

This module handles auth in your applications and other ttoss modules.

This module is intended to use with AWS Cognito. It uses AWS Amplify under the hood.

Amplify Auth configuration must be provided in your App to make Auth Module works properly.

ESM Only

This package is ESM only.

Getting Started

Install

$ yarn add @ttoss/auth @ttoss/react-notifications aws-amplify

Examples of use

Amplify config

import { Amplify, type ResourcesConfig } from 'aws-amplify';
import {
  CookieStorage,
  KeyValueStorageInterface,
  defaultStorage,
  sessionStorage,
} from 'aws-amplify/utils';
import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';

const authConfig: ResourcesConfig['Auth'] = {
  Cognito: {
    // REQUIRED Amazon Cognito User Pool Client ID (26-char alphanumeric string)
    userPoolClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3',

    // OPTIONAL - Amazon Cognito User Pool ID
    userPoolId: 'XX-XXXX-X_abcd1234',

    loginWith: {
      // OPTIONAL - Hosted UI configuration
      oauth: {
        domain: 'your_cognito_domain',
        scopes: [
          'phone',
          'email',
          'profile',
          'openid',
          'aws.cognito.signin.user.admin',
        ],
        redirectSignIn: ['http://localhost:3000/'],
        redirectSignOut: ['http://localhost:3000/'],
        responseType: 'code', // or 'token', note that REFRESH token will only be generated when the responseType is code
      },
    },
  },
};

Amplify.configure({
  Auth: authConfig,
});

// Browser Local Storage
// In Amplify the localStorage is the default storage mechanism. It saves the tokens in the browser's localStorage.
// This local storage will persist across browser sessions and tabs. You can explicitly set to this storage by calling:
cognitoUserPoolsTokenProvider.setKeyValueStorage(defaultStorage);

// Cookie Storage
// CookieStorage saves the tokens in the browser's Cookies. The cookies will persist across browser sessions and tabs.
// You can explicitly set to this storage by calling:
cognitoUserPoolsTokenProvider.setKeyValueStorage(
  new CookieStorage(
    // OPTIONAL - Configuration for cookie storage
    {
      // REQUIRED - Cookie domain (only required if cookieStorage is provided)
      domain: '.yourdomain.com',
      // OPTIONAL - Cookie path
      path: '/',
      // OPTIONAL - Cookie expiration in days
      expires: 365,
      // OPTIONAL - See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
      sameSite: 'strict' | 'lax' | 'none',
      // OPTIONAL - Cookie secure flag
      // Either true or false, indicating if the cookie transmission requires a secure protocol (https).
      secure: true,
    }
  )
);

// Browser Session Storage
// sessionStorage saves the tokens in the browser's sessionStorage and these tokens will clear when a tab is closed.
// The benefit to this storage mechanism is that the session only lasts as long as the browser is open and you
// can sign out users when they close the tab. You can update to this storage by calling:
cognitoUserPoolsTokenProvider.setKeyValueStorage(sessionStorage);

// Custom Storage
// You can implement your own custom storage mechanism by creating a class that implements the storage interface.
// Here is an example that uses memory storage:

class MyCustomStorage implements KeyValueStorageInterface {
  storageObject: Record<string, string> = {};
  async setItem(key: string, value: string): Promise<void> {
    this.storageObject[key] = value;
  }
  async getItem(key: string): Promise<string | null> {
    return this.storageObject[key];
  }
  async removeItem(key: string): Promise<void> {
    delete this.storageObject[key];
  }
  async clear(): Promise<void> {
    this.storageObject = {};
  }
}

cognitoUserPoolsTokenProvider.setKeyValueStorage(new MyCustomStorage());

PrivateRoute component

import { useAuth } from '@ttoss/react-auth';

const PrivateRoute = (props: any) => {
  const { isAuthenticated } = useAuth();

  if (!isAuthenticated) {
    return <Navigate to="/login" state={{ redirectTo: props.path || '/' }} />;
  }
  return <Route {...props} />;
};

Login Page

import { Auth, useAuth } from '@ttoss/react-auth';

const Login = () => {
  const auth = useAuth();

  const onSuccess = () => {
    // Navigate to logged-area
  };

  return (
    <div>
      <h1>Login Page</h1>

      <Auth onSignIn={onSuccess} />

      <button onClick={auth.signOut}>Logout</button>
    </div>
  );
};
export default Login;

Auth with Progressbar

import { AuthProvider } from '@ttoss/react-auth';
import { NotificationsProvider } from '@ttoss/react-notifications';

ReactDOM.render(
  <React.StrictMode>
    <NotificationsProvider>
      <AuthProvider>
        <App />
      </AuthProvider>
    </NotificationsProvider>
  </React.StrictMode>,
  document.getElementById('root')
);
1.7.47

4 days ago

1.7.48

4 days ago

1.7.45

12 days ago

1.7.46

12 days ago

1.7.44

20 days ago

1.7.43

30 days ago

1.7.42

1 month ago

1.7.41

1 month ago

1.7.40

1 month ago

1.7.39

1 month ago

1.7.37

1 month ago

1.7.38

1 month ago

1.7.36

1 month ago

1.7.35

1 month ago

1.7.34

2 months ago

1.7.33

2 months ago

1.7.32

2 months ago

1.7.31

3 months ago

1.7.28

3 months ago

1.7.29

3 months ago

1.7.30

3 months ago

1.7.27

4 months ago

1.7.25

4 months ago

1.7.26

4 months ago

1.7.24

4 months ago

1.7.23

4 months ago

1.7.22

5 months ago

1.7.21

5 months ago

1.7.20

5 months ago

1.7.19

5 months ago

1.7.18

5 months ago

1.7.14

6 months ago

1.7.15

6 months ago

1.7.16

6 months ago

1.7.17

5 months ago

1.6.40

9 months ago

1.6.42

9 months ago

1.7.10

6 months ago

1.6.41

9 months ago

1.7.11

6 months ago

1.7.12

6 months ago

1.7.13

6 months ago

1.7.9

7 months ago

1.7.8

7 months ago

1.7.7

7 months ago

1.7.6

7 months ago

1.7.5

7 months ago

1.7.4

7 months ago

1.6.26

11 months ago

1.6.28

11 months ago

1.6.27

11 months ago

1.6.29

11 months ago

1.6.31

10 months ago

1.6.30

10 months ago

1.6.33

10 months ago

1.6.32

10 months ago

1.6.35

9 months ago

1.6.34

9 months ago

1.6.37

9 months ago

1.6.36

9 months ago

1.6.39

9 months ago

1.6.38

9 months ago

1.7.3

8 months ago

1.7.2

8 months ago

1.7.1

9 months ago

1.7.0

9 months ago

1.6.20

11 months ago

1.6.22

11 months ago

1.6.24

11 months ago

1.6.23

11 months ago

1.6.25

11 months ago

1.6.4

1 year ago

1.4.6

1 year ago

1.6.3

1 year ago

1.4.5

1 year ago

1.6.2

1 year ago

1.4.4

1 year ago

1.6.1

1 year ago

1.4.3

1 year ago

1.6.0

1 year ago

1.4.2

1 year ago

1.6.11

12 months ago

1.6.10

12 months ago

1.6.13

12 months ago

1.6.12

12 months ago

1.6.15

12 months ago

1.6.14

12 months ago

1.6.17

12 months ago

1.6.16

12 months ago

1.6.19

12 months ago

1.6.18

12 months ago

1.5.1

1 year ago

1.5.0

1 year ago

1.6.9

12 months ago

1.6.8

12 months ago

1.6.6

1 year ago

1.6.5

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.24

1 year ago

1.3.25

1 year ago

1.3.22

1 year ago

1.3.28

1 year ago

1.3.29

1 year ago

1.3.26

1 year ago

1.3.27

1 year ago

1.3.31

1 year ago

1.3.32

1 year ago

1.3.30

1 year ago

1.3.35

1 year ago

1.3.36

1 year ago

1.3.33

1 year ago

1.3.34

1 year ago

1.3.37

1 year ago

1.3.38

1 year ago

1.3.10

1 year ago

1.3.13

1 year ago

1.3.14

1 year ago

1.3.11

1 year ago

1.3.12

1 year ago

1.3.17

1 year ago

1.3.18

1 year ago

1.3.15

1 year ago

1.3.16

1 year ago

1.3.19

1 year ago

1.3.20

1 year ago

1.3.21

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.2.19

1 year ago

1.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.23

1 year ago

1.2.24

1 year ago

1.2.21

1 year ago

1.2.22

1 year ago

1.2.18

1 year ago

1.2.16

1 year ago

1.2.17

1 year ago

1.2.15

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.11

1 year ago

1.2.14

1 year ago

1.2.9

1 year ago

1.2.10

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago