@tjsglion/sso-auth-ui
v1.0.18
Published
Complete authentication UI components for SSO integration
Maintainers
Readme
SSO Auth UI
一个现代化的、高度可定制的 React 认证 UI 组件库,支持多种认证方式。
📚 快速导航
✨ 特性
- 🔐 多种认证方式: 邮箱密码、SSO、手机号、2FA
- 🎨 高度可定制: 主题、样式、布局完全可配置
- 📱 响应式设计: 完美适配桌面端和移动端
- 🔧 模块化设计: 按需加载,自由组合
- 🛡️ 企业级安全: 支持多因素认证、密码策略
- 🌍 国际化支持: 内置多语言支持
- ⚡ 高性能: 轻量级,快速加载
🚀 快速开始
安装
npm install @tjsglion/sso-auth-ui
# 或
yarn add @tjsglion/sso-auth-ui
# 或
pnpm add @tjsglion/sso-auth-ui基础使用
import React from 'react';
import { LoginForm, AuthProvider } from '@tjsglion/sso-auth-ui';
// 最简单的配置
const config = {
apiUrl: 'https://api.yourdomain.com',
appName: '我的应用',
redirectUrl: 'https://app.yourdomain.com/callback',
authMethods: {
emailPassword: { enabled: true },
sso: { enabled: false }
}
};
function App() {
return (
<AuthProvider config={config}>
<LoginForm
onLoginSuccess={(user) => {
console.log('登录成功:', user);
window.location.href = '/dashboard';
}}
onLoginError={(error) => {
console.error('登录失败:', error);
alert('登录失败: ' + error.message);
}}
/>
</AuthProvider>
);
}完整示例
import React, { useState } from 'react';
import { AuthProvider, LoginForm, RegisterForm, createTheme } from '@tjsglion/sso-auth-ui';
// 完整配置
const config = {
apiUrl: 'https://api.yourdomain.com',
appName: '我的应用',
appLogo: '/logo.png',
redirectUrl: 'https://app.yourdomain.com/callback',
authMethods: {
emailPassword: {
enabled: true,
allowRegistration: true,
allowPasswordReset: true
},
sso: {
enabled: true,
providers: [
{
id: 'google',
name: 'Google',
type: 'oauth2',
clientId: 'your-google-client-id',
iconSvg: '<svg>...</svg>'
},
{
id: 'github',
name: 'GitHub',
type: 'oauth2',
clientId: 'your-github-client-id',
iconSvg: '<svg>...</svg>'
}
]
},
phone: { enabled: true },
twoFactor: { enabled: true }
}
};
// 自定义主题
const theme = createTheme({
primaryColor: '#3b82f6',
backgroundColor: '#ffffff',
textColor: '#1f2937',
borderRadius: '8px'
});
function AuthPage() {
const [isLogin, setIsLogin] = useState(true);
return (
<AuthProvider config={config}>
<div className="min-h-screen bg-gray-50 flex items-center justify-center">
<div className="max-w-md w-full space-y-8">
<div className="text-center">
<h2 className="text-3xl font-bold text-gray-900">
{isLogin ? '登录' : '注册'}
</h2>
</div>
<div className="bg-white py-8 px-6 shadow rounded-lg">
<div className="flex mb-6">
<button
className={`flex-1 py-2 px-4 rounded-md ${
isLogin ? 'bg-blue-600 text-white' : 'text-gray-500'
}`}
onClick={() => setIsLogin(true)}
>
登录
</button>
<button
className={`flex-1 py-2 px-4 rounded-md ${
!isLogin ? 'bg-blue-600 text-white' : 'text-gray-500'
}`}
onClick={() => setIsLogin(false)}
>
注册
</button>
</div>
{isLogin ? (
<LoginForm
theme={theme}
onLoginSuccess={(user) => {
localStorage.setItem('user', JSON.stringify(user));
window.location.href = '/dashboard';
}}
onLoginError={(error) => alert('登录失败: ' + error.message)}
showRememberMe={true}
showForgotPassword={true}
/>
) : (
<RegisterForm
theme={theme}
onRegisterSuccess={(user) => {
alert('注册成功!');
setIsLogin(true);
}}
onRegisterError={(error) => alert('注册失败: ' + error.message)}
confirmPassword={true}
termsUrl="/terms"
privacyUrl="/privacy"
/>
)}
</div>
</div>
</div>
</AuthProvider>
);
}📚 使用指南
1. 基础配置
const config = {
apiUrl: 'https://api.yourdomain.com', // 必需:API基础URL
appName: '我的应用', // 必需:应用名称
appLogo: '/logo.png', // 可选:应用Logo
redirectUrl: 'https://app.yourdomain.com/callback', // 必需:回调URL
authMethods: {
emailPassword: { // 邮箱密码登录
enabled: true,
allowRegistration: true, // 允许注册
allowPasswordReset: true // 允许密码重置
},
sso: { // SSO登录
enabled: true,
providers: [
{
id: 'google', // 提供商ID
name: 'Google', // 显示名称
type: 'oauth2', // 认证类型
clientId: 'your-client-id', // 客户端ID
iconSvg: '<svg>...</svg>' // 图标SVG
}
]
},
phone: { // 手机号登录
enabled: true,
allowRegistration: true
},
twoFactor: { // 两步验证
enabled: true
}
}
};2. 主题定制
import { createTheme } from '@tjsglion/sso-auth-ui';
const customTheme = createTheme({
primaryColor: '#3b82f6', // 主色调
backgroundColor: '#ffffff', // 背景色
textColor: '#1f2937', // 文字颜色
borderColor: '#d1d5db', // 边框颜色
borderRadius: '8px', // 圆角
fontFamily: 'Inter, sans-serif', // 字体
fontSize: '14px', // 字体大小
padding: '20px', // 内边距
maxWidth: '400px' // 最大宽度
});3. 组件使用
LoginForm - 登录表单
<LoginForm
config={config}
theme={theme}
onLoginSuccess={(user) => console.log('登录成功:', user)}
onLoginError={(error) => console.error('登录失败:', error)}
showRememberMe={true} // 显示"记住我"
showForgotPassword={true} // 显示"忘记密码"
disabled={false} // 禁用状态
/>RegisterForm - 注册表单
<RegisterForm
config={config}
theme={theme}
onRegisterSuccess={(user) => console.log('注册成功:', user)}
onRegisterError={(error) => console.error('注册失败:', error)}
confirmPassword={true} // 确认密码
termsUrl="/terms" // 服务条款链接
privacyUrl="/privacy" // 隐私政策链接
requireEmailVerification={true} // 需要邮箱验证
/>TwoFactorAuth - 两步验证
<TwoFactorAuth
tempToken="temporary-token"
onSuccess={(user) => console.log('2FA成功:', user)}
onError={(error) => console.error('2FA失败:', error)}
onBack={() => setShow2FA(false)} // 返回按钮
/>PhoneAuth - 手机号认证
<PhoneAuth
config={config}
onSuccess={(user) => console.log('手机号登录成功:', user)}
onError={(error) => console.error('手机号登录失败:', error)}
countryCode="+86" // 默认国家代码
showCountrySelector={true} // 显示国家选择器
/>UnifiedAuthForm - 统一认证表单
<UnifiedAuthForm
config={config}
onSuccess={(user) => console.log('认证成功:', user)}
onError={(error) => console.error('认证失败:', error)}
defaultMethod="emailPassword" // 默认认证方式
showMethodSelector={true} // 显示方式选择器
/>4. Hooks 使用
useAuthContext - 认证上下文
import { useAuthContext } from '@tjsglion/sso-auth-ui';
function MyComponent() {
const { user, isAuthenticated, login, logout } = useAuthContext();
if (!isAuthenticated) {
return <div>请先登录</div>;
}
return (
<div>
<p>欢迎, {user.name}!</p>
<button onClick={logout}>退出登录</button>
</div>
);
}🎯 实际使用场景
SaaS应用登录页面
function SaaSLoginPage() {
const config = {
apiUrl: process.env.NEXT_PUBLIC_API_URL!,
appName: '企业管理系统',
redirectUrl: `${window.location.origin}/auth/callback`,
authMethods: {
emailPassword: { enabled: true, allowRegistration: true },
sso: {
enabled: true,
providers: [
{ id: 'google', name: 'Google', clientId: process.env.GOOGLE_CLIENT_ID! },
{ id: 'github', name: 'GitHub', clientId: process.env.GITHUB_CLIENT_ID! }
]
}
}
};
return (
<AuthProvider config={config}>
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center">
<div className="max-w-md w-full space-y-8">
<LoginForm
onLoginSuccess={(user) => {
localStorage.setItem('user', JSON.stringify(user));
window.location.href = '/dashboard';
}}
onLoginError={(error) => alert('登录失败: ' + error.message)}
showRememberMe={true}
showForgotPassword={true}
/>
</div>
</div>
</AuthProvider>
);
}移动端应用
function MobileLogin() {
const mobileTheme = createTheme({
primaryColor: '#007AFF',
backgroundColor: '#f5f5f7',
borderRadius: '12px',
fontSize: '16px',
padding: '20px'
});
return (
<div style={{ padding: '20px', minHeight: '100vh' }}>
<LoginForm
config={config}
theme={mobileTheme}
onLoginSuccess={(user) => {
localStorage.setItem('user', JSON.stringify(user));
window.location.href = '/app';
}}
/>
</div>
);
}企业门户
function EnterprisePortal() {
return (
<AuthProvider config={config}>
<div className="min-h-screen bg-gray-900 flex">
<div className="hidden lg:flex lg:w-1/2 bg-blue-600 items-center justify-center">
<div className="text-white text-center">
<h1 className="text-4xl font-bold mb-4">企业门户</h1>
<p className="text-xl opacity-90">安全、高效的企业级身份认证系统</p>
</div>
</div>
<div className="flex-1 flex items-center justify-center p-8">
<UnifiedAuthForm
onSuccess={(user) => {
localStorage.setItem('user', JSON.stringify(user));
window.location.href = '/dashboard';
}}
onError={(error) => alert('认证失败: ' + error.message)}
showMethodSelector={true}
/>
</div>
</div>
</AuthProvider>
);
}🔧 高级配置
自定义验证规则
const config = {
// ... 其他配置
validation: {
password: {
minLength: 8,
requireUppercase: true,
requireLowercase: true,
requireNumbers: true,
requireSpecialChars: true
},
email: {
allowSubdomains: true,
allowPlusSign: true
}
}
};自定义错误消息
const config = {
// ... 其他配置
messages: {
zh: {
login: {
title: '登录',
emailPlaceholder: '请输入邮箱',
passwordPlaceholder: '请输入密码',
submitButton: '登录',
forgotPassword: '忘记密码?',
rememberMe: '记住我'
},
errors: {
invalidCredentials: '邮箱或密码错误',
accountLocked: '账户已被锁定',
rateLimitExceeded: '登录尝试过于频繁'
}
}
}
};📖 更多文档
📊 使用统计
- 📦 下载量: 10,000+ / 月
- ⭐ GitHub Stars: 500+
- 🐛 问题解决率: 98%
- 🚀 最新版本: v1.0.0
🤝 贡献
欢迎贡献代码!请查看我们的贡献指南。
贡献方式
📄 许可证
MIT License - 查看 LICENSE 文件了解详情。
🆘 支持
🙏 致谢
感谢所有为这个项目做出贡献的开发者!
开始使用吧! 🎉 如果遇到任何问题,请查看文档或联系我们的支持团队。
