Decap CMS UI Authentication Components
decap-cms-ui-auth is a specialized UI library providing the interactive authentication components for Decap CMS, an open-source content management system. Currently at version 3.3.0, it is an actively maintained package within the broader Decap CMS project, which follows a frequent release cadence, often seeing multiple patch and minor updates within weeks. This package is distinct from `decap-cms-lib-auth`, which handles the underlying authentication logic; `decap-cms-ui-auth` focuses solely on rendering the user interface for login, logout, and related authentication flows. It leverages React and Emotion for its component architecture and styling, ensuring a consistent look and feel with other Decap CMS UI elements. Its key differentiator is its tight integration with the Decap CMS ecosystem, making it the canonical choice for building custom Decap CMS admin interfaces that require authentication.
Common errors
-
Module not found: Error: Can't resolve 'decap-cms-ui-auth'
cause The package `decap-cms-ui-auth` is not installed or the import path is incorrect after the renaming from Netlify CMS.fixRun `npm install decap-cms-ui-auth` or `yarn add decap-cms-ui-auth`. Ensure all import statements use the correct package name. -
TypeError: Cannot read properties of undefined (reading 'authenticate') (in <AuthPage component>)
cause The `authenticator` prop passed to `AuthPage` is either missing, `undefined`, or does not have the expected methods from `decap-cms-lib-auth`.fixEnsure you correctly import and initialize `authenticator` from `decap-cms-lib-auth` and pass it as a prop: `<AuthPage authenticator={authenticatorInstance} />`. -
Invariant Violation: Minified React error #XXX; visit https://reactjs.org/docs/error-decoder.html?invariant=XXX for the full message or use the non-minified dev environment for full errors.
cause This often indicates a React version mismatch between your application's React and what `decap-cms-ui-auth` expects (e.g., trying to run with React 18 when Decap CMS requires React 19).fixVerify that your `react` and `react-dom` versions in `package.json` satisfy the peer dependency requirements of `decap-cms-ui-auth` (currently `^19.1.0`). Upgrade or adjust versions as needed. -
AuthPage is not a function/component (or similar 'is not a constructor' error with CommonJS)
cause Incorrect import syntax, often trying to use CommonJS `require()` with an ESM-only package or using `import AuthPage from '...' ` for a named export.fixEnsure you are using named imports for components: `import { AuthPage } from 'decap-cms-ui-auth';`. If using CommonJS, consider migrating your project to ESM or finding an older, compatible version of Decap CMS.
Warnings
- breaking The entire Netlify CMS project was renamed to Decap CMS starting with version 3.x. This package's name changed from `netlify-cms-ui-auth` to `decap-cms-ui-auth`.
- breaking Decap CMS version 3.x and its associated packages, including `decap-cms-ui-auth`, are primarily designed for ES Module (ESM) environments. Direct `require()` calls for CommonJS might lead to errors.
- gotcha `decap-cms-ui-auth` relies heavily on peer dependencies, including `react`, `@emotion/react`, `decap-cms-lib-auth`, and `decap-cms-ui-default`. These must be explicitly installed in your project.
- breaking The `react` peer dependency for `decap-cms-ui-auth@3.3.0` is `^19.1.0`. Using older React versions (e.g., React 17 or 18) might lead to incompatibility warnings or runtime errors.
Install
-
npm install decap-cms-ui-auth -
yarn add decap-cms-ui-auth -
pnpm add decap-cms-ui-auth
Imports
- AuthPage
const AuthPage = require('decap-cms-ui-auth').AuthPage;import { AuthPage } from 'decap-cms-ui-auth'; - AuthenticationPage
import AuthenticationPage from 'decap-cms-ui-auth/AuthenticationPage';
import { AuthenticationPage } from 'decap-cms-ui-auth'; - Authenticator (via decap-cms-lib-auth)
import { authenticator } from 'decap-cms-ui-auth';import { authenticator } from 'decap-cms-lib-auth';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import { AuthPage } from 'decap-cms-ui-auth';
// In a real application, you would initialize 'authenticator' from 'decap-cms-lib-auth'.
// For this quickstart, we'll use a mock object.
// Mock authenticator - in production, replace with a properly initialized instance from 'decap-cms-lib-auth'
const mockAuthenticator = {
authenticate: async (credentials) => {
console.log('Attempting authentication with:', credentials);
return { token: 'mock-token', user: { name: 'Mock User' } };
},
clear: () => console.log('Authenticator cleared'),
retrieve: async () => ({ token: 'mock-token', user: { name: 'Mock User' } }),
// Add other methods expected by AuthPage (e.g., 'authProviders')
authProviders: [], // No providers for this mock
};
function MyAuthApp() {
const handleLoginSuccess = (user) => {
console.log('Login successful!', user);
alert(`Welcome, ${user.name}!`);
// Typically, you'd redirect to the CMS dashboard here
};
const handleLoginError = (error) => {
console.error('Login error:', error.message);
alert(`Login failed: ${error.message}`);
};
return (
<div style={{ fontFamily: 'sans-serif', textAlign: 'center', marginTop: '50px' }}>
<h1>Welcome to Decap CMS</h1>
<p>Please log in to manage your content.</p>
<div style={{
maxWidth: '400px', margin: '20px auto', padding: '20px',
border: '1px solid #eee', borderRadius: '8px',
boxShadow: '0 2px 10px rgba(0,0,0,0.05)'
}}>
<AuthPage
authenticator={mockAuthenticator}
onLogin={handleLoginSuccess}
onError={handleLoginError}
inProgress={false} // Set to true during an active login attempt
config={{ backend: { name: 'git-gateway' } }} // Minimal config prop, AuthPage might inspect it
logoURL="https://decapcms.org/img/decap-logo.svg" // Optional: path to your CMS logo
/>
</div>
</div>
);
}
// To run this example:
// Ensure you have a 'root' div in your HTML file (e.g., <div id="root"></div>)
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyAuthApp />);