ra-auth-cognito React-Admin Authentication Provider

2.0.0 · active · verified Wed Apr 22

ra-auth-cognito is an authentication provider designed specifically for React-Admin applications, integrating seamlessly with AWS Cognito user pools. The current stable version is 2.0.0, which introduced compatibility with React-Admin v5. The package typically releases new major versions aligned with React-Admin's major updates, alongside minor and patch releases for features and bug fixes. Key differentiators include robust support for username/password authentication, OAuth flows via AWS Hosted UI, and multi-factor authentication (MFA) including TOTP. It provides specialized components and hooks, such as `Login` and `useCognitoLogin`, to manage the initial login process for users with temporary passwords. This library simplifies common Cognito integration challenges within React-Admin, allowing developers to handle user identity, group-based permissions, and various authentication flows directly within their admin interfaces. Users must be pre-registered in Cognito with an email for the system to function correctly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `ra-auth-cognito` with `react-admin` for username/password authentication. It configures the `CognitoAuthProvider` using a `CognitoUserPool` instance and sets the custom `Login` component to handle initial logins with temporary passwords, common in Cognito setups.

import React from 'react';
import { Admin, Resource } from 'react-admin';
import { CognitoAuthProvider, Login } from 'ra-auth-cognito';
import { CognitoUserPool } from 'amazon-cognito-identity-js';

// Mock data provider and resources for demonstration
const dataProvider = {
    getList: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }], total: 1 }),
    getOne: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
    getMany: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }] }),
    getManyReference: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }], total: 1 }),
    update: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
    updateMany: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }] }),
    create: () => Promise.resolve({ data: { id: 1, title: 'New Post' } }),
    delete: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
    deleteMany: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }] })
};
const posts = { list: () => <div>Posts List</div>, edit: () => <div>Edit Post</div> };

// Ensure these environment variables are set in your .env file or build process
const userPoolId = process.env.REACT_APP_COGNITO_USERPOOL_ID ?? 'us-east-1_XXXXX';
const clientId = process.env.REACT_APP_COGNITO_APP_CLIENT_ID ?? 'XXXXXXXXXXXX';

const userPool = new CognitoUserPool({
    UserPoolId: userPoolId,
    ClientId: clientId
});

const authProvider = CognitoAuthProvider(userPool);

const App = () => {
    if (!userPoolId || !clientId) {
        console.error('Cognito UserPoolId or ClientId not set. Please check your environment variables.');
        return <div>Configuration Error: Cognito details missing.</div>;
    }
    return (
        <Admin
            authProvider={authProvider}
            dataProvider={dataProvider}
            title="Example Admin"
            loginPage={Login} // Use the provided Login component for temporary password handling
        >
            <Resource name="posts" {...posts} />
        </Admin>
    );
};

export default App;

view raw JSON →