@erikey/react 0.1.0
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 +188 -0
- package/dist/index.d.mts +2215 -0
- package/dist/index.d.ts +2215 -0
- package/dist/index.js +545 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +517 -0
- package/dist/index.mjs.map +1 -0
- package/dist/styles.css +1473 -0
- package/dist/styles.css.map +1 -0
- package/dist/styles.d.mts +2 -0
- package/dist/styles.d.ts +2 -0
- package/dist/ui/index.css +1473 -0
- package/dist/ui/index.css.map +1 -0
- package/dist/ui/index.d.mts +22 -0
- package/dist/ui/index.d.ts +22 -0
- package/dist/ui/index.js +63 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/index.mjs +41 -0
- package/dist/ui/index.mjs.map +1 -0
- package/package.json +74 -0
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# @auth-ai/sdk
|
|
2
|
+
|
|
3
|
+
React SDK for Auth.ai - Easy authentication integration for your React applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @auth-ai/sdk
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @auth-ai/sdk
|
|
11
|
+
# or
|
|
12
|
+
yarn add @auth-ai/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
### 1. Wrap your app with AuthProvider
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { AuthProvider } from '@auth-ai/sdk';
|
|
21
|
+
|
|
22
|
+
function App() {
|
|
23
|
+
return (
|
|
24
|
+
<AuthProvider config={{ baseURL: 'https://api.yourapp.com' }}>
|
|
25
|
+
<YourApp />
|
|
26
|
+
</AuthProvider>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 2. Use authentication hooks
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useAuth, ProtectedRoute } from '@auth-ai/sdk';
|
|
35
|
+
|
|
36
|
+
function Dashboard() {
|
|
37
|
+
const { user, signOut } = useAuth();
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<ProtectedRoute>
|
|
41
|
+
<div>
|
|
42
|
+
<h1>Welcome, {user?.email}!</h1>
|
|
43
|
+
<button onClick={signOut}>Sign Out</button>
|
|
44
|
+
</div>
|
|
45
|
+
</ProtectedRoute>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API Reference
|
|
51
|
+
|
|
52
|
+
### AuthProvider
|
|
53
|
+
|
|
54
|
+
Wrap your application with this provider to enable authentication.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
<AuthProvider
|
|
58
|
+
config={{
|
|
59
|
+
baseURL: 'https://api.yourapp.com',
|
|
60
|
+
credentials: 'include', // optional
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
{children}
|
|
64
|
+
</AuthProvider>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### useAuth()
|
|
68
|
+
|
|
69
|
+
Access authentication state and methods.
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
const {
|
|
73
|
+
user, // Current user object
|
|
74
|
+
session, // Current session
|
|
75
|
+
isLoading, // Loading state
|
|
76
|
+
isAuthenticated, // Boolean auth status
|
|
77
|
+
signIn, // Sign in function
|
|
78
|
+
signUp, // Sign up function
|
|
79
|
+
signOut, // Sign out function
|
|
80
|
+
refreshSession, // Manually refresh session
|
|
81
|
+
} = useAuth();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### useUser()
|
|
85
|
+
|
|
86
|
+
Get current user data.
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
const user = useUser();
|
|
90
|
+
|
|
91
|
+
if (user) {
|
|
92
|
+
console.log(user.email, user.role);
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### useOrganization()
|
|
97
|
+
|
|
98
|
+
Access organization data and methods.
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
const {
|
|
102
|
+
organization, // Current active organization
|
|
103
|
+
organizations, // List of user's organizations
|
|
104
|
+
switchOrganization, // Switch to different org
|
|
105
|
+
isLoading,
|
|
106
|
+
} = useOrganization();
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### ProtectedRoute
|
|
110
|
+
|
|
111
|
+
Protect routes that require authentication.
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
<ProtectedRoute
|
|
115
|
+
fallback={<LoginPage />} // Optional: custom fallback
|
|
116
|
+
redirectTo="/login" // Optional: redirect URL
|
|
117
|
+
>
|
|
118
|
+
<PrivatePage />
|
|
119
|
+
</ProtectedRoute>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Examples
|
|
123
|
+
|
|
124
|
+
### Login Form
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { useAuth } from '@auth-ai/sdk';
|
|
128
|
+
import { useState } from 'react';
|
|
129
|
+
|
|
130
|
+
function LoginForm() {
|
|
131
|
+
const { signIn } = useAuth();
|
|
132
|
+
const [email, setEmail] = useState('');
|
|
133
|
+
const [password, setPassword] = useState('');
|
|
134
|
+
|
|
135
|
+
const handleSubmit = async (e) => {
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
try {
|
|
138
|
+
await signIn(email, password);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error('Login failed:', error);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<form onSubmit={handleSubmit}>
|
|
146
|
+
<input
|
|
147
|
+
type="email"
|
|
148
|
+
value={email}
|
|
149
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
150
|
+
placeholder="Email"
|
|
151
|
+
/>
|
|
152
|
+
<input
|
|
153
|
+
type="password"
|
|
154
|
+
value={password}
|
|
155
|
+
onChange={(e) => setPassword(e.target.value)}
|
|
156
|
+
placeholder="Password"
|
|
157
|
+
/>
|
|
158
|
+
<button type="submit">Sign In</button>
|
|
159
|
+
</form>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### User Profile
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
import { useUser } from '@auth-ai/sdk';
|
|
168
|
+
|
|
169
|
+
function UserProfile() {
|
|
170
|
+
const user = useUser();
|
|
171
|
+
|
|
172
|
+
if (!user) {
|
|
173
|
+
return <div>Not logged in</div>;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<div>
|
|
178
|
+
<h1>{user.name || user.email}</h1>
|
|
179
|
+
<p>Role: {user.role}</p>
|
|
180
|
+
<p>Joined: {new Date(user.createdAt).toLocaleDateString()}</p>
|
|
181
|
+
</div>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|