@nice2dev/auth
v1.4.64
Published
Nice2Dev Auth — Login, registration, OAuth, 2FA, captcha, password strength, token management for React
Maintainers
Readme
@nice2dev/auth
Authentication & authorization UI components for React — login, registration, OAuth, 2FA, captcha, password management, and token handling.
Installation
npm install @nice2dev/authPeer dependencies: react >= 17.0.0, react-dom >= 17.0.0
Import the stylesheet once, e.g. in your app's entry — the components' JavaScript does not load it:
import '@nice2dev/auth/style.css';Components
| Component | Description |
| ----------------------- | -------------------------------------------------------------------------------------- |
| NiceLoginForm | Login form with email/password, validation, remember me |
| NiceRegistrationForm | Registration form: fields required / optional / hidden, custom fields, password policy |
| NiceTwoFaSetup | TOTP 2FA setup with QR code display |
| NiceChangePassword | Change/reset password with strength meter and generator icons |
| NiceCaptcha | Captcha widget (pluggable providers) |
| NicePasswordStrength | Password field with an eye and a strength bar as wide as the input (or the bar alone) |
| NicePasswordGenerator | Password generator; iconsOnly dice + copy icons, bindTo fills any number of fields |
| NiceOAuthButtons | OAuth provider login buttons (Google, GitHub, Microsoft, etc.) |
| NiceAuthGuard | Protected route/section wrapper |
| NiceTokenManagement | API token CRUD management |
Password strength
scorePassword(password) returns { score, entropyBits, label, hints } — one scale for the meter,
the generator and the forms' minPasswordStrength. The score comes from the estimated entropy
(length × character pool), with common passwords, years, sequences (abc, 123, qwerty) and
repeats discounted:
| score | level | bits | random characters of all four kinds | | ----- | ----------- | ------- | ----------------------------------- | | 0 | Very weak | < 28 | | | 1 | Weak | 28 – 39 | 5 – 6 | | 2 | Fair | 40 – 55 | 7 – 8 | | 3 | Strong | 56 – 71 | 9 – 10 | | 4 | Very strong | 72 – 99 | 11 – 15 | | 5 | Excellent | ≥ 100 | 16 and more |
minPasswordStrength (default 2, Fair) takes this scale; levels 0 – 4 keep their old names.
calcPasswordStrength() (0 – 4) is deprecated: it is scorePassword with Excellent folded into 4.
Since 1.4.60 scorePassword, checkPasswordPolicy and their types live in @nice2dev/ui-core
(this package re-exports them), so NicePasswordInput (@nice2dev/ui, showStrength) and
NicePassphraseInput (@nice2dev/ui-security, minStrength) rate on the same scale.
Hooks
| Hook | Description |
| --------- | --------------------------------------------------- |
| useAuth | Auth state management (JWT, roles, refresh, logout) |
Usage
import { NiceLoginForm, useAuth } from '@nice2dev/auth';
function LoginPage() {
const { login, user } = useAuth({
loginFn: async (credentials) => {
const res = await fetch('/api/auth/login', {
method: 'POST',
body: JSON.stringify(credentials),
});
return res.json();
},
});
if (user) return <div>Welcome, {user.name}</div>;
return <NiceLoginForm onLogin={login} />;
}Types
Key TypeScript types exported: AuthUser, AuthTokens, AuthState, AuthActions, UseAuthConfig, CaptchaProvider, OAuthProvider, PasswordStrengthLevel.
License
MIT © NiceToDev
