@pixygon/auth 1.0.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 ADDED
@@ -0,0 +1,267 @@
1
+ # @pixygon/auth
2
+
3
+ Shared authentication package for all Pixygon applications (Pixygon.ai, BimboChat, MonsterTask, etc.)
4
+
5
+ ## Features
6
+
7
+ - Unified authentication across all Pixygon apps
8
+ - Automatic token refresh
9
+ - Secure token storage with app-specific prefixes
10
+ - TypeScript support
11
+ - Pre-built branded components
12
+ - React hooks for easy integration
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ # In monorepo, add as local dependency
18
+ npm install @pixygon/auth
19
+
20
+ # Or link locally
21
+ npm link ../pixygon-packages/@pixygon/auth
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ### 1. Wrap your app with AuthProvider
27
+
28
+ ```tsx
29
+ import { AuthProvider } from '@pixygon/auth';
30
+
31
+ function App() {
32
+ return (
33
+ <AuthProvider
34
+ config={{
35
+ baseUrl: 'https://pixygon-server.onrender.com/v1',
36
+ appId: 'pixygon-ai', // Unique ID for token storage
37
+ appName: 'Pixygon AI',
38
+ debug: process.env.NODE_ENV === 'development',
39
+ onLogin: (user) => console.log('Logged in:', user.userName),
40
+ onLogout: () => console.log('Logged out'),
41
+ }}
42
+ >
43
+ <YourApp />
44
+ </AuthProvider>
45
+ );
46
+ }
47
+ ```
48
+
49
+ ### 2. Use the auth hooks
50
+
51
+ ```tsx
52
+ import { useAuth, useUser } from '@pixygon/auth';
53
+
54
+ function MyComponent() {
55
+ const { login, logout, isAuthenticated, isLoading } = useAuth();
56
+ const { userName, email, totalTokens } = useUser();
57
+
58
+ const handleLogin = async () => {
59
+ try {
60
+ await login({ userName: 'user', password: 'pass' });
61
+ } catch (error) {
62
+ console.error('Login failed:', error.message);
63
+ }
64
+ };
65
+
66
+ if (isLoading) return <div>Loading...</div>;
67
+
68
+ return isAuthenticated ? (
69
+ <div>
70
+ <p>Welcome, {userName}!</p>
71
+ <p>Tokens: {totalTokens}</p>
72
+ <button onClick={logout}>Logout</button>
73
+ </div>
74
+ ) : (
75
+ <button onClick={handleLogin}>Login</button>
76
+ );
77
+ }
78
+ ```
79
+
80
+ ### 3. Or use the pre-built components
81
+
82
+ ```tsx
83
+ import { PixygonAuth } from '@pixygon/auth/components';
84
+
85
+ function LoginPage() {
86
+ return (
87
+ <PixygonAuth
88
+ mode="login"
89
+ onSuccess={(user) => navigate('/dashboard')}
90
+ onModeChange={(mode) => navigate(`/auth/${mode}`)}
91
+ theme="dark"
92
+ />
93
+ );
94
+ }
95
+ ```
96
+
97
+ ## Available Hooks
98
+
99
+ ### `useAuth()`
100
+ Main hook with all auth state and actions.
101
+ ```tsx
102
+ const {
103
+ user,
104
+ status, // 'idle' | 'loading' | 'authenticated' | 'unauthenticated' | 'verifying'
105
+ isAuthenticated,
106
+ isLoading,
107
+ error,
108
+ login,
109
+ register,
110
+ logout,
111
+ verify,
112
+ forgotPassword,
113
+ recoverPassword,
114
+ hasRole,
115
+ } = useAuth();
116
+ ```
117
+
118
+ ### `useUser()`
119
+ User data with convenient shortcuts.
120
+ ```tsx
121
+ const {
122
+ user,
123
+ userName,
124
+ email,
125
+ role,
126
+ totalTokens,
127
+ subscriptionTier,
128
+ hasRole,
129
+ } = useUser();
130
+ ```
131
+
132
+ ### `useToken()`
133
+ Token management.
134
+ ```tsx
135
+ const {
136
+ accessToken,
137
+ refreshToken,
138
+ getAccessToken,
139
+ refreshTokens,
140
+ } = useToken();
141
+ ```
142
+
143
+ ### `useAuthStatus()`
144
+ Status checks.
145
+ ```tsx
146
+ const {
147
+ isIdle,
148
+ isLoading,
149
+ isAuthenticated,
150
+ isUnauthenticated,
151
+ isVerifying,
152
+ } = useAuthStatus();
153
+ ```
154
+
155
+ ### `useRequireAuth()`
156
+ Protected routes.
157
+ ```tsx
158
+ const { isAuthorized, isLoading } = useRequireAuth({
159
+ roles: ['admin', 'superadmin'],
160
+ onUnauthorized: () => navigate('/login'),
161
+ });
162
+ ```
163
+
164
+ ## Integration with Redux
165
+
166
+ If your app uses Redux, you can sync the auth state:
167
+
168
+ ```tsx
169
+ import { AuthProvider, useAuth } from '@pixygon/auth';
170
+ import { useDispatch } from 'react-redux';
171
+ import { setLogin, setLogout } from './state';
172
+
173
+ // Create a sync component
174
+ function AuthReduxSync({ children }) {
175
+ const { user, accessToken, isAuthenticated } = useAuth();
176
+ const dispatch = useDispatch();
177
+
178
+ useEffect(() => {
179
+ if (isAuthenticated && user && accessToken) {
180
+ dispatch(setLogin({ user, token: accessToken }));
181
+ } else if (!isAuthenticated) {
182
+ dispatch(setLogout());
183
+ }
184
+ }, [isAuthenticated, user, accessToken, dispatch]);
185
+
186
+ return children;
187
+ }
188
+
189
+ // Use it inside AuthProvider
190
+ function App() {
191
+ return (
192
+ <AuthProvider config={authConfig}>
193
+ <ReduxProvider store={store}>
194
+ <AuthReduxSync>
195
+ <YourApp />
196
+ </AuthReduxSync>
197
+ </ReduxProvider>
198
+ </AuthProvider>
199
+ );
200
+ }
201
+ ```
202
+
203
+ ## Components
204
+
205
+ ### Individual Forms
206
+ ```tsx
207
+ import { LoginForm, RegisterForm, VerifyForm, ForgotPasswordForm } from '@pixygon/auth/components';
208
+ ```
209
+
210
+ ### All-in-one Component
211
+ ```tsx
212
+ import { PixygonAuth } from '@pixygon/auth/components';
213
+
214
+ <PixygonAuth
215
+ mode="login" // 'login' | 'register' | 'verify' | 'forgot-password'
216
+ onSuccess={(user) => {}}
217
+ onError={(error) => {}}
218
+ onModeChange={(mode) => {}}
219
+ userName="user" // For verify mode
220
+ showBranding={true}
221
+ theme="dark" // 'dark' | 'light'
222
+ />
223
+ ```
224
+
225
+ ## Configuration Options
226
+
227
+ ```tsx
228
+ interface AuthConfig {
229
+ // Required
230
+ baseUrl: string; // API base URL
231
+ appId: string; // Unique app identifier
232
+
233
+ // Optional
234
+ appName?: string; // Display name
235
+ autoRefresh?: boolean; // Auto-refresh tokens (default: true)
236
+ refreshThreshold?: number; // Seconds before expiry to refresh (default: 300)
237
+ debug?: boolean; // Enable debug logging
238
+
239
+ // Callbacks
240
+ onLogin?: (user: User) => void;
241
+ onLogout?: () => void;
242
+ onTokenRefresh?: (tokens: TokenPair) => void;
243
+ onError?: (error: AuthError) => void;
244
+
245
+ // Custom storage (default: localStorage)
246
+ storage?: AuthStorage;
247
+ }
248
+ ```
249
+
250
+ ## API Client (Advanced)
251
+
252
+ For making authenticated API calls outside of React:
253
+
254
+ ```tsx
255
+ import { createAuthApiClient, createTokenStorage } from '@pixygon/auth';
256
+
257
+ const storage = createTokenStorage('my-app');
258
+ const client = createAuthApiClient(config, storage);
259
+
260
+ // Make authenticated requests
261
+ const user = await client.getMe();
262
+ const profile = await client.updateProfile({ displayName: 'New Name' });
263
+ ```
264
+
265
+ ## License
266
+
267
+ MIT - Pixygon