react-unified-auth 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -91
- package/dist/components/Button.d.ts +8 -0
- package/dist/components/DynamicForm.d.ts +9 -0
- package/dist/components/LoginForm.d.ts +6 -0
- package/dist/components/SocialLogin.d.ts +15 -0
- package/dist/components/UserInfoBox.d.ts +8 -0
- package/dist/context/AuthContext.d.ts +8 -0
- package/dist/hooks/useAuth.d.ts +10 -0
- package/dist/hooks/useDynamicForm.d.ts +6 -0
- package/dist/index.css +438 -0
- package/dist/index.d.ts +18 -158
- package/dist/index.esm.css +1 -0
- package/dist/index.esm.js +21 -26
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +21 -26
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +59 -0
- package/dist/utils/api-client.d.ts +14 -0
- package/dist/utils/validation.d.ts +9 -0
- package/package.json +3 -8
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
# React Unified Auth
|
|
2
|
-
|
|
3
|
-
一个用于三方登录的统一入口React组件库,支持动态表单和可配置的认证流程。
|
|
4
|
-
|
|
5
|
-
## 功能特性
|
|
6
|
-
|
|
7
|
-
- 动态表单渲染:通过HTTP请求获取表单结构并动态渲染
|
|
8
|
-
- 可配置的认证流程:支持自定义API端点和参数
|
|
9
|
-
- 预构建的UI组件:登录表单、用户信息框、社交登录按钮等
|
|
10
|
-
- TypeScript支持:完整的类型定义
|
|
11
|
-
- React Hooks:提供便捷的认证和表单管理hooks
|
|
12
|
-
|
|
13
|
-
## 安装
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install react-unified-auth
|
|
1
|
+
# React Unified Auth
|
|
2
|
+
|
|
3
|
+
一个用于三方登录的统一入口React组件库,支持动态表单和可配置的认证流程。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- 动态表单渲染:通过HTTP请求获取表单结构并动态渲染
|
|
8
|
+
- 可配置的认证流程:支持自定义API端点和参数
|
|
9
|
+
- 预构建的UI组件:登录表单、用户信息框、社交登录按钮等
|
|
10
|
+
- TypeScript支持:完整的类型定义
|
|
11
|
+
- React Hooks:提供便捷的认证和表单管理hooks
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install react-unified-auth
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## 使用方法
|
|
@@ -23,7 +23,8 @@ npm install react-unified-auth
|
|
|
23
23
|
在你的应用根组件中包裹`AuthProvider`:
|
|
24
24
|
|
|
25
25
|
```tsx
|
|
26
|
-
import { AuthProvider } from 'react-
|
|
26
|
+
import { AuthProvider } from 'react-unified-auth';
|
|
27
|
+
import 'react-unified-auth/dist/index.css';
|
|
27
28
|
|
|
28
29
|
function App() {
|
|
29
30
|
const config = {
|
|
@@ -47,7 +48,7 @@ function App() {
|
|
|
47
48
|
### 2. 使用登录表单
|
|
48
49
|
|
|
49
50
|
```tsx
|
|
50
|
-
import { LoginForm } from 'react-
|
|
51
|
+
import { LoginForm } from 'react-unified-auth';
|
|
51
52
|
|
|
52
53
|
function LoginPage() {
|
|
53
54
|
return (
|
|
@@ -65,7 +66,7 @@ function LoginPage() {
|
|
|
65
66
|
### 3. 使用用户信息框
|
|
66
67
|
|
|
67
68
|
```tsx
|
|
68
|
-
import { UserInfoBox, useAuth } from 'react-
|
|
69
|
+
import { UserInfoBox, useAuth } from 'react-unified-auth';
|
|
69
70
|
|
|
70
71
|
function UserProfile() {
|
|
71
72
|
const { user, logout } = useAuth();
|
|
@@ -84,7 +85,7 @@ function UserProfile() {
|
|
|
84
85
|
### 4. 使用社交登录
|
|
85
86
|
|
|
86
87
|
```tsx
|
|
87
|
-
import { SocialLogin } from 'react-
|
|
88
|
+
import { SocialLogin } from 'react-unified-auth';
|
|
88
89
|
|
|
89
90
|
function SocialLoginPage() {
|
|
90
91
|
const providers = [
|
|
@@ -220,77 +221,81 @@ const { formConfig, loading, error } = useDynamicForm(formId);
|
|
|
220
221
|
- `POST /auth/logout` - 用户登出
|
|
221
222
|
- `GET /auth/me` - 获取当前用户信息
|
|
222
223
|
|
|
223
|
-
## 样式自定义
|
|
224
|
-
|
|
225
|
-
组件使用了Tailwind CSS的类名,你可以通过以下方式自定义样式:
|
|
226
|
-
|
|
227
|
-
### 1. 基本配置
|
|
228
|
-
|
|
229
|
-
确保你的项目中已正确配置Tailwind CSS:
|
|
230
|
-
|
|
231
|
-
```bash
|
|
232
|
-
# 安装依赖
|
|
233
|
-
npm install -D tailwindcss postcss autoprefixer
|
|
234
|
-
|
|
235
|
-
# 初始化配置
|
|
236
|
-
npx tailwindcss init -p
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
### 2. 更新tailwind.config.js
|
|
240
|
-
|
|
241
|
-
在配置中包含组件库的文件路径:
|
|
242
|
-
|
|
243
|
-
```js
|
|
244
|
-
// tailwind.config.js
|
|
245
|
-
module.exports = {
|
|
246
|
-
content: [
|
|
247
|
-
"./src/**/*.{js,ts,jsx,tsx}",
|
|
248
|
-
"./node_modules/react-unified-auth/dist/**/*.{js,ts,jsx,tsx}", // 必须包含此行
|
|
249
|
-
],
|
|
250
|
-
theme: {
|
|
251
|
-
extend: {},
|
|
252
|
-
},
|
|
253
|
-
plugins: [],
|
|
254
|
-
}
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
### 3. 导入Tailwind CSS
|
|
258
|
-
|
|
259
|
-
在项目入口文件中导入:
|
|
260
|
-
|
|
261
|
-
```css
|
|
262
|
-
/* src/index.css */
|
|
263
|
-
@tailwind base;
|
|
264
|
-
@tailwind components;
|
|
265
|
-
@tailwind utilities;
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
```jsx
|
|
269
|
-
// src/index.jsx
|
|
270
|
-
import './index.css';
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
### 4. 自定义样式
|
|
274
|
-
|
|
275
|
-
- **通过className覆盖**:
|
|
276
|
-
```jsx
|
|
277
|
-
<Button className="bg-red-600 hover:bg-red-700" />
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
- **在Tailwind配置中扩展**:
|
|
281
|
-
```js
|
|
282
|
-
// tailwind.config.js
|
|
283
|
-
module.exports = {
|
|
284
|
-
theme: {
|
|
285
|
-
extend: {
|
|
286
|
-
colors: {
|
|
287
|
-
primary: '#1d4ed8',
|
|
288
|
-
},
|
|
289
|
-
},
|
|
290
|
-
},
|
|
291
|
-
}
|
|
292
|
-
```
|
|
293
|
-
|
|
224
|
+
## 样式自定义
|
|
225
|
+
|
|
226
|
+
组件使用了Tailwind CSS的类名,你可以通过以下方式自定义样式:
|
|
227
|
+
|
|
228
|
+
### 1. 基本配置
|
|
229
|
+
|
|
230
|
+
确保你的项目中已正确配置Tailwind CSS:
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
# 安装依赖
|
|
234
|
+
npm install -D tailwindcss postcss autoprefixer
|
|
235
|
+
|
|
236
|
+
# 初始化配置
|
|
237
|
+
npx tailwindcss init -p
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 2. 更新tailwind.config.js
|
|
241
|
+
|
|
242
|
+
在配置中包含组件库的文件路径:
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
// tailwind.config.js
|
|
246
|
+
module.exports = {
|
|
247
|
+
content: [
|
|
248
|
+
"./src/**/*.{js,ts,jsx,tsx}",
|
|
249
|
+
"./node_modules/react-unified-auth/dist/**/*.{js,ts,jsx,tsx}", // 必须包含此行
|
|
250
|
+
],
|
|
251
|
+
theme: {
|
|
252
|
+
extend: {},
|
|
253
|
+
},
|
|
254
|
+
plugins: [],
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### 3. 导入Tailwind CSS
|
|
259
|
+
|
|
260
|
+
在项目入口文件中导入:
|
|
261
|
+
|
|
262
|
+
```css
|
|
263
|
+
/* src/index.css */
|
|
264
|
+
@tailwind base;
|
|
265
|
+
@tailwind components;
|
|
266
|
+
@tailwind utilities;
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
```jsx
|
|
270
|
+
// src/index.jsx
|
|
271
|
+
import './index.css';
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 4. 自定义样式
|
|
275
|
+
|
|
276
|
+
- **通过className覆盖**:
|
|
277
|
+
```jsx
|
|
278
|
+
<Button className="bg-red-600 hover:bg-red-700" />
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
- **在Tailwind配置中扩展**:
|
|
282
|
+
```js
|
|
283
|
+
// tailwind.config.js
|
|
284
|
+
module.exports = {
|
|
285
|
+
theme: {
|
|
286
|
+
extend: {
|
|
287
|
+
colors: {
|
|
288
|
+
primary: '#1d4ed8',
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### 5. 样式不生效?查看详细排查指南
|
|
296
|
+
|
|
297
|
+
如果样式仍然不生效,请查看我们的[Tailwind CSS样式排查指南](docs/TAILWIND_TROUBLESHOOTING.md),包含完整的问题解决步骤!
|
|
298
|
+
|
|
294
299
|
## 开发
|
|
295
300
|
|
|
296
301
|
```bash
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
3
|
+
variant?: 'primary' | 'secondary' | 'outline';
|
|
4
|
+
size?: 'sm' | 'md' | 'lg';
|
|
5
|
+
loading?: boolean;
|
|
6
|
+
fullWidth?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function Button({ variant, size, loading, fullWidth, children, disabled, className, ...props }: ButtonProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FormField } from '../types';
|
|
2
|
+
export interface DynamicFormProps {
|
|
3
|
+
fields: FormField[];
|
|
4
|
+
onSubmit: (values: Record<string, any>) => void;
|
|
5
|
+
submitText?: string;
|
|
6
|
+
submitVariant?: 'primary' | 'secondary' | 'outline';
|
|
7
|
+
loading?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function DynamicForm({ fields, onSubmit, submitText, submitVariant, loading, }: DynamicFormProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SocialProvider } from '../types';
|
|
2
|
+
export interface SocialLoginButtonProps {
|
|
3
|
+
provider: SocialProvider;
|
|
4
|
+
onClick: (provider: SocialProvider) => void;
|
|
5
|
+
loading?: boolean;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function SocialLoginButton({ provider, onClick, loading, className }: SocialLoginButtonProps): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export interface SocialLoginProps {
|
|
10
|
+
providers: SocialProvider[];
|
|
11
|
+
onLogin: (provider: SocialProvider) => void;
|
|
12
|
+
loading?: boolean;
|
|
13
|
+
className?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function SocialLogin({ providers, onLogin, loading, className }: SocialLoginProps): import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { UserInfo } from '../types';
|
|
2
|
+
export interface UserInfoBoxProps {
|
|
3
|
+
user: UserInfo;
|
|
4
|
+
onLogout?: () => void;
|
|
5
|
+
loading?: boolean;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function UserInfoBox({ user, onLogout, loading, className }: UserInfoBoxProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { AuthConfig } from '../types';
|
|
3
|
+
export interface AuthProviderProps {
|
|
4
|
+
config: AuthConfig;
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare function AuthProvider({ config, children }: AuthProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export declare function useAuthConfig(): AuthConfig;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AuthResponse, UserInfo } from '../types';
|
|
2
|
+
export declare function useAuth(): {
|
|
3
|
+
user: UserInfo | null;
|
|
4
|
+
loading: boolean;
|
|
5
|
+
error: string | null;
|
|
6
|
+
login: (credentials: Record<string, any>) => Promise<AuthResponse>;
|
|
7
|
+
logout: () => Promise<void>;
|
|
8
|
+
getUserInfo: () => Promise<UserInfo | null>;
|
|
9
|
+
isAuthenticated: boolean;
|
|
10
|
+
};
|