@oxyhq/services 5.7.4 → 5.7.5

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.
Files changed (59) hide show
  1. package/README.md +121 -264
  2. package/lib/commonjs/core/auth-manager.js +440 -0
  3. package/lib/commonjs/core/auth-manager.js.map +1 -0
  4. package/lib/commonjs/core/index.js +102 -5
  5. package/lib/commonjs/core/index.js.map +1 -1
  6. package/lib/commonjs/core/use-auth.js +244 -0
  7. package/lib/commonjs/core/use-auth.js.map +1 -0
  8. package/lib/commonjs/node/index.js +2 -49
  9. package/lib/commonjs/node/index.js.map +1 -1
  10. package/lib/commonjs/ui/index.js +1 -16
  11. package/lib/commonjs/ui/index.js.map +1 -1
  12. package/lib/module/core/auth-manager.js +432 -0
  13. package/lib/module/core/auth-manager.js.map +1 -0
  14. package/lib/module/core/index.js +41 -5
  15. package/lib/module/core/index.js.map +1 -1
  16. package/lib/module/core/use-auth.js +235 -0
  17. package/lib/module/core/use-auth.js.map +1 -0
  18. package/lib/module/node/index.js +1 -11
  19. package/lib/module/node/index.js.map +1 -1
  20. package/lib/module/ui/index.js +1 -16
  21. package/lib/module/ui/index.js.map +1 -1
  22. package/lib/typescript/core/auth-manager.d.ts +136 -0
  23. package/lib/typescript/core/auth-manager.d.ts.map +1 -0
  24. package/lib/typescript/core/index.d.ts +24 -1
  25. package/lib/typescript/core/index.d.ts.map +1 -1
  26. package/lib/typescript/core/use-auth.d.ts +79 -0
  27. package/lib/typescript/core/use-auth.d.ts.map +1 -0
  28. package/lib/typescript/node/index.d.ts +1 -7
  29. package/lib/typescript/node/index.d.ts.map +1 -1
  30. package/lib/typescript/ui/index.d.ts +1 -2
  31. package/lib/typescript/ui/index.d.ts.map +1 -1
  32. package/package.json +4 -6
  33. package/src/__tests__/zero-config-auth.test.ts +607 -0
  34. package/src/core/auth-manager.ts +500 -0
  35. package/src/core/index.ts +41 -6
  36. package/src/core/use-auth.tsx +245 -0
  37. package/src/node/index.ts +1 -17
  38. package/src/ui/index.ts +1 -19
  39. package/lib/commonjs/node/middleware.js +0 -227
  40. package/lib/commonjs/node/middleware.js.map +0 -1
  41. package/lib/commonjs/ui/zero-config/index.js +0 -25
  42. package/lib/commonjs/ui/zero-config/index.js.map +0 -1
  43. package/lib/commonjs/ui/zero-config/provider.js +0 -278
  44. package/lib/commonjs/ui/zero-config/provider.js.map +0 -1
  45. package/lib/module/node/middleware.js +0 -199
  46. package/lib/module/node/middleware.js.map +0 -1
  47. package/lib/module/ui/zero-config/index.js +0 -8
  48. package/lib/module/ui/zero-config/index.js.map +0 -1
  49. package/lib/module/ui/zero-config/provider.js +0 -270
  50. package/lib/module/ui/zero-config/provider.js.map +0 -1
  51. package/lib/typescript/node/middleware.d.ts +0 -92
  52. package/lib/typescript/node/middleware.d.ts.map +0 -1
  53. package/lib/typescript/ui/zero-config/index.d.ts +0 -5
  54. package/lib/typescript/ui/zero-config/index.d.ts.map +0 -1
  55. package/lib/typescript/ui/zero-config/provider.d.ts +0 -84
  56. package/lib/typescript/ui/zero-config/provider.d.ts.map +0 -1
  57. package/src/node/middleware.ts +0 -234
  58. package/src/ui/zero-config/index.ts +0 -11
  59. package/src/ui/zero-config/provider.tsx +0 -310
package/README.md CHANGED
@@ -1,347 +1,201 @@
1
1
  # OxyHQServices
2
2
 
3
- A TypeScript client library for the Oxy API providing **zero-config** authentication, user management, and UI components for React and React Native applications.
3
+ 🚀 **Zero-config authentication and user management** for React, React Native, and Node.js applications. No manual token handling, no interceptor setup, no middleware configuration required.
4
4
 
5
- ## ✨ Zero-Config Quick Start
5
+ ## ✨ Quick Start (Zero Config)
6
6
 
7
- ### Frontend (React/Next.js) - 2 lines of code:
7
+ ### Frontend (React/React Native)
8
8
 
9
- ```jsx
10
- import { OxyZeroConfigProvider, useOxyZeroConfig } from '@oxyhq/services/ui';
9
+ ```tsx
10
+ import React from 'react';
11
+ import { AuthProvider, useAuth } from '@oxyhq/services';
11
12
 
12
- // 1. Wrap your app
13
+ // 1. Wrap your app (that's it for setup!)
13
14
  function App() {
14
15
  return (
15
- <OxyZeroConfigProvider>
16
- <YourApp />
17
- </OxyZeroConfigProvider>
16
+ <AuthProvider baseURL="https://api.oxy.so">
17
+ <MainApp />
18
+ </AuthProvider>
18
19
  );
19
20
  }
20
21
 
21
22
  // 2. Use authentication anywhere
22
- function Dashboard() {
23
- const { user, login, logout, isAuthenticated } = useOxyZeroConfig();
24
-
25
- if (isAuthenticated) {
26
- return <div>Welcome {user.username}! <button onClick={logout}>Logout</button></div>;
27
- }
23
+ function MainApp() {
24
+ const { isAuthenticated, user, login, logout } = useAuth();
28
25
 
29
- return <button onClick={() => login('user', 'password')}>Login</button>;
30
- }
31
- ```
32
-
33
- ### Backend (Express.js) - 1 line of code:
34
-
35
- ```js
36
- import express from 'express';
37
- import { createOxyAuth } from '@oxyhq/services/node';
38
-
39
- const app = express();
40
-
41
- // Zero-config auth - req.user automatically available
42
- app.use('/api', createOxyAuth());
43
-
44
- app.get('/api/profile', (req, res) => {
45
- res.json({ user: req.user }); // req.user populated automatically!
46
- });
47
- ```
48
-
49
- **That's it!** No complex configuration, no manual token management, no middleware setup.
50
-
51
- ## Table of Contents
52
-
53
- - [Zero-Config Guide](#-zero-config-quick-start)
54
- - [Features](#features)
55
- - [Installation](#installation)
56
- - [Complete Examples](#complete-examples)
57
- - [Advanced Usage](#advanced-usage)
58
- - [Documentation](#documentation)
59
- - [Migration Guide](#migration-guide)
60
-
61
- ## Features
62
-
63
- - 🚀 **Zero-Config**: Get authentication working in 2-3 lines of code
64
- - 🔐 **Automatic Token Management**: Tokens saved, restored, and refreshed automatically
65
- - 👥 **User Management**: Profile operations and social features
66
- - 🎨 **UI Components**: Pre-built React components for common functionality
67
- - 📱 **Cross-Platform**: Works in React Native and web applications
68
- - 🔧 **TypeScript**: Full type safety and IntelliSense support
69
- - 🔄 **Auto Backend Integration**: Frontend automatically sends tokens to backend
70
- - 📦 **Express Middleware**: One-line backend authentication with `req.user` support
71
-
72
- ## Installation
73
-
74
- ```bash
75
- npm install @oxyhq/services
76
- ```
77
-
78
- ## Complete Examples
79
-
80
- ### React Application
81
-
82
- **App.js**
83
- ```jsx
84
- import React from 'react';
85
- import { OxyZeroConfigProvider } from '@oxyhq/services/ui';
86
- import Dashboard from './Dashboard';
87
-
88
- export default function App() {
89
- return (
90
- <OxyZeroConfigProvider>
91
- <Dashboard />
92
- </OxyZeroConfigProvider>
93
- );
94
- }
95
- ```
96
-
97
- **Dashboard.js**
98
- ```jsx
99
- import React from 'react';
100
- import { useOxyZeroConfig } from '@oxyhq/services/ui';
101
-
102
- export default function Dashboard() {
103
- const { user, login, logout, isAuthenticated, isLoading } = useOxyZeroConfig();
104
-
105
- if (isLoading) return <div>Loading...</div>;
106
-
107
26
  if (!isAuthenticated) {
108
- return (
109
- <div>
110
- <h1>Please log in</h1>
111
- <button onClick={() => login('demo', 'password')}>Login</button>
112
- </div>
113
- );
27
+ return <button onClick={() => login('user', 'pass')}>Login</button>;
114
28
  }
115
-
29
+
116
30
  return (
117
31
  <div>
118
- <h1>Welcome, {user.username}!</h1>
32
+ <h1>Welcome {user?.username}!</h1>
119
33
  <button onClick={logout}>Logout</button>
120
34
  </div>
121
35
  );
122
36
  }
123
37
  ```
124
38
 
125
- ### Express.js Backend
39
+ ### Backend (Express.js)
126
40
 
127
- **server.js**
128
- ```js
41
+ ```typescript
129
42
  import express from 'express';
130
- import { createOxyAuth } from '@oxyhq/services/node';
43
+ import { authenticateRequest, OxyRequest } from '@oxyhq/services/api';
131
44
 
132
45
  const app = express();
133
- app.use(express.json());
134
-
135
- // Public routes
136
- app.get('/', (req, res) => {
137
- res.json({ message: 'Public endpoint' });
138
- });
139
-
140
- // Protected routes - zero config!
141
- app.use('/api', createOxyAuth());
142
46
 
143
- // req.user is automatically available
144
- app.get('/api/profile', (req, res) => {
47
+ // Zero-config authentication - just add the middleware
48
+ app.get('/profile', authenticateRequest(), (req: OxyRequest, res) => {
49
+ // req.user is automatically populated!
145
50
  res.json({
146
- message: `Hello ${req.user.username}!`,
51
+ message: `Hello ${req.user!.username}!`,
147
52
  user: req.user
148
53
  });
149
54
  });
150
-
151
- app.listen(3000, () => console.log('Server running on port 3000'));
152
55
  ```
153
56
 
154
- ### Next.js Full-Stack
57
+ That's it! 🎉 Authentication is now fully automated with:
58
+ - ✅ Automatic token management and refresh
59
+ - ✅ Secure storage across app restarts
60
+ - ✅ Built-in error handling and retry logic
61
+ - ✅ Cross-platform support (React Native + Web)
62
+ - ✅ TypeScript support throughout
155
63
 
156
- **pages/_app.js**
157
- ```jsx
158
- import { OxyZeroConfigProvider } from '@oxyhq/services/ui';
64
+ ## 📖 Documentation
159
65
 
160
- export default function App({ Component, pageProps }) {
161
- return (
162
- <OxyZeroConfigProvider>
163
- <Component {...pageProps} />
164
- </OxyZeroConfigProvider>
165
- );
166
- }
167
- ```
66
+ - **[🚀 Zero-Config Authentication Guide](./packages/services/ZERO_CONFIG_AUTH.md)** - Complete setup guide
67
+ - **[🔧 Migration Guide](./packages/services/ZERO_CONFIG_AUTH.md#migration-from-legacy-authentication)** - Upgrade from legacy auth
68
+ - **[📚 Examples](./packages/services/examples/)** - Complete integration examples
69
+ - **[🔐 API Reference](./packages/services/ZERO_CONFIG_AUTH.md#complete-api-reference)** - All components and hooks
168
70
 
169
- **pages/api/user.js**
170
- ```js
171
- import { createOxyAuth } from '@oxyhq/services/node';
71
+ ## Table of Contents
172
72
 
173
- const auth = createOxyAuth();
73
+ - [Legacy Features](#features)
74
+ - [Legacy Quick Start](#quick-start)
75
+ - [Package Exports](#package-exports)
76
+ - [Requirements](#requirements)
77
+ - [Development](#development)
78
+ - [Integration](#integration)
79
+ - [License](#license)
174
80
 
175
- export default async function handler(req, res) {
176
- // Apply authentication
177
- await new Promise((resolve, reject) => {
178
- auth(req, res, (err) => {
179
- if (err) reject(err);
180
- else resolve();
181
- });
182
- });
81
+ ---
183
82
 
184
- // req.user is now available
185
- res.json({ user: req.user });
186
- }
187
- ```
83
+ ## Legacy Features (Still Supported)
188
84
 
189
- ## Advanced Usage
85
+ A TypeScript client library for the Oxy API providing authentication, user management, and UI components for React and React Native applications.
190
86
 
191
- ### Configuration Options
87
+ - 🔐 **Authentication**: JWT-based auth with automatic token refresh
88
+ - 👥 **User Management**: Profile operations and social features
89
+ - 🎨 **UI Components**: Pre-built React components for common functionality
90
+ - 📱 **Cross-Platform**: Works in React Native and web applications
91
+ - 🔧 **TypeScript**: Full type safety and IntelliSense support
192
92
 
193
- **Frontend**
194
- ```jsx
195
- <OxyZeroConfigProvider
196
- apiUrl="https://your-api.com"
197
- onAuthChange={(user) => console.log('Auth changed:', user)}
198
- >
199
- <App />
200
- </OxyZeroConfigProvider>
201
- ```
93
+ ## Legacy Quick Start
202
94
 
203
- **Backend**
204
- ```js
205
- app.use('/api', createOxyAuth({
206
- baseURL: 'https://your-oxy-api.com',
207
- loadUser: true,
208
- publicPaths: ['/api/health'],
209
- onError: (error, req, res) => {
210
- res.status(error.status).json({ error: error.message });
211
- }
212
- }));
95
+ ```bash
96
+ npm install @oxyhq/services
213
97
  ```
214
98
 
215
- ### Direct API Access
216
-
217
- ```jsx
218
- import { useOxyApi } from '@oxyhq/services/ui';
99
+ ```typescript
100
+ import { OxyServices } from '@oxyhq/services';
219
101
 
220
- function ProfileForm() {
221
- const api = useOxyApi();
102
+ const oxy = new OxyServices({
103
+ baseURL: 'http://localhost:3000'
104
+ });
222
105
 
223
- const updateProfile = async (data) => {
224
- const user = await api.updateProfile(data);
225
- // Token automatically included in request
226
- };
106
+ // Authenticate
107
+ const response = await oxy.auth.login({
108
+ email: 'user@example.com',
109
+ password: 'password'
110
+ });
227
111
 
228
- return <button onClick={() => updateProfile({ name: 'New Name' })}>Update</button>;
229
- }
112
+ // Get current user
113
+ const user = await oxy.users.getCurrentUser();
230
114
  ```
231
115
 
232
- ### Optional Authentication
116
+ - [📚 Full Documentation](./docs/README.md)
117
+ - [🚀 Quick Start Guide](./docs/quick-start.md)
118
+ - [🔐 Core API Reference](./docs/core-api.md)
119
+ - [💼 Integration Examples](./docs/examples/)
233
120
 
234
- ```js
235
- import { createOptionalOxyAuth } from '@oxyhq/services/node';
121
+ ## UI Components
236
122
 
237
- // Works with or without authentication
238
- app.use('/api/content', createOptionalOxyAuth());
123
+ Import and use pre-built React components:
239
124
 
240
- app.get('/api/content', (req, res) => {
241
- if (req.user) {
242
- res.json({ content: 'personalized', user: req.user });
243
- } else {
244
- res.json({ content: 'public' });
245
- }
246
- });
125
+ ```typescript
126
+ import { OxyProvider, Avatar, FollowButton } from '@oxyhq/services/ui';
247
127
  ```
248
128
 
249
- ## Documentation
129
+ ## Package Exports
250
130
 
251
- ### Zero-Config Guide
252
- - **[🚀 Zero-Config Setup Guide](./docs/zero-config.md)** - Complete guide with examples
131
+ The library provides multiple entry points:
253
132
 
254
- ### Advanced Documentation
255
- - **[📚 Full Documentation](./docs/README.md)** - Complete system documentation
256
- - **[🚀 Quick Start Guide](./docs/quick-start.md)** - Traditional setup guide
257
- - **[🔐 Core API Reference](./docs/core-api.md)** - Detailed API documentation
258
- - **[💼 Integration Examples](./docs/examples/)** - More integration examples
259
-
260
- ### UI Components
261
- - **[🎨 UI Components Guide](./docs/ui-components.md)** - React/RN component usage
262
-
263
- ## Migration Guide
264
-
265
- ### From Complex Setup
266
-
267
- **Before (complex setup):**
268
- ```jsx
269
- import { OxyProvider, useOxy } from '@oxyhq/services/ui';
133
+ ```typescript
134
+ // Core services only (Node.js/Express)
270
135
  import { OxyServices } from '@oxyhq/services';
271
136
 
272
- const oxy = new OxyServices({ baseURL: 'http://localhost:3001' });
137
+ // UI components only (React/React Native)
138
+ import { OxyProvider, Avatar } from '@oxyhq/services/ui';
273
139
 
274
- function App() {
275
- return (
276
- <OxyProvider oxyServices={oxy}>
277
- <Dashboard />
278
- </OxyProvider>
279
- );
280
- }
281
- ```
282
-
283
- **After (zero-config):**
284
- ```jsx
285
- import { OxyZeroConfigProvider, useOxyZeroConfig } from '@oxyhq/services/ui';
286
-
287
- function App() {
288
- return (
289
- <OxyZeroConfigProvider>
290
- <Dashboard />
291
- </OxyZeroConfigProvider>
292
- );
293
- }
140
+ // Full package (Core + UI)
141
+ import { OxyServices, OxyProvider } from '@oxyhq/services/full';
294
142
  ```
295
143
 
296
- Just change the provider and hook names - the API is nearly identical!
144
+ ## Zero-Config Express Router
297
145
 
298
- ### Package Exports
299
-
300
- The library provides multiple entry points for different use cases:
146
+ Quickly mount authentication routes in any Express app:
301
147
 
302
148
  ```typescript
303
- // Zero-config frontend
304
- import { OxyZeroConfigProvider, useOxyZeroConfig } from '@oxyhq/services/ui';
305
-
306
- // Zero-config backend
307
- import { createOxyAuth } from '@oxyhq/services/node';
308
-
309
- // Core services only (traditional)
310
- import { OxyServices } from '@oxyhq/services';
149
+ import express from 'express';
150
+ import { createAuth } from '@oxyhq/services/core';
311
151
 
312
- // Full package (Core + UI + Node)
313
- import { OxyServices, OxyProvider, createOxyAuth } from '@oxyhq/services/full';
152
+ const app = express();
153
+ app.use('/auth', createAuth({ baseURL: 'http://localhost:3000' }).middleware());
314
154
  ```
315
155
 
156
+ This automatically provides sign-up, login, logout, refresh, and session management endpoints.
157
+
316
158
  ## Requirements
317
159
 
318
160
  - **Node.js** 16+ (for backend usage)
319
- - **React** 16.8+ (for React components)
161
+ - **React** 16.8+ (for React components)
320
162
  - **React Native** 0.60+ (for mobile components)
321
163
  - **TypeScript** 4.0+ (optional but recommended)
322
164
 
323
165
  ## Troubleshooting
324
166
 
325
- ### Common Issues
167
+ ### FormData Issues in React Native/Expo
168
+
169
+ If you encounter `ReferenceError: Property 'FormData' doesn't exist` when using Expo with Hermes engine:
170
+
171
+ **For file uploads in React Native/Expo:**
172
+ - The library handles this automatically - no additional setup required
173
+ - File uploads will work with both native FormData (when available) and the polyfilled version
174
+ - Ensure you're using the latest version of the package
326
175
 
327
- **"useOxyZeroConfig must be used within an OxyZeroConfigProvider"**
328
- - Make sure your component is wrapped with `<OxyZeroConfigProvider>`
176
+ ### Additional Dependencies
329
177
 
330
- **"req.user is undefined"**
331
- - Ensure the middleware is applied: `app.use('/api', createOxyAuth())`
332
- - Check that the client sends `Authorization: Bearer <token>` header
178
+ For React Native projects, you may need to install peer dependencies:
179
+
180
+ ```bash
181
+ npm install axios jwt-decode invariant
182
+ ```
183
+
184
+ ## Important for React Native Hermes Users
185
+
186
+ If you use this package in a React Native app with the Hermes engine, you must add the following import as the very first line of your app's entry file (e.g., App.js or index.js):
333
187
 
334
- **FormData Issues in React Native/Expo**
335
- Add this import as the first line of your app entry file:
336
188
  ```js
337
189
  import 'react-native-url-polyfill/auto';
338
190
  ```
339
191
 
340
- ### Getting Help
192
+ This ensures that FormData and other web APIs are polyfilled before any dependencies are loaded. If you do not do this, you may see errors like:
193
+
194
+ ```
195
+ ReferenceError: Property 'FormData' doesn't exist, js engine: hermes
196
+ ```
341
197
 
342
- 1. Check the [Zero-Config Guide](./docs/zero-config.md) for complete examples
343
- 2. Review [Troubleshooting Guide](./docs/troubleshooting.md) for common issues
344
- 3. Open an issue on [GitHub](https://github.com/oxyhq/services/issues)
198
+ ---
345
199
 
346
200
  ## Development
347
201
 
@@ -352,17 +206,20 @@ npm install
352
206
  # Build the library
353
207
  npm run build
354
208
 
355
- # Run tests
209
+ # Run tests
356
210
  npm test
211
+
212
+ # Development mode
213
+ npm run dev
357
214
  ```
358
215
 
359
216
  ## Integration
360
217
 
361
218
  This library works with:
362
- - **[Oxy API](../api/)** - The companion authentication server
363
- - **Express.js** - Built-in zero-config middleware support
364
- - **React/React Native** - Zero-config UI components and hooks
365
- - **Next.js** - SSR/SSG authentication support
219
+ - **[Oxy API](../oxy-api/)** - The companion authentication server
220
+ - **Express.js** - Built-in middleware support
221
+ - **React/React Native** - UI components and hooks
222
+ - **Next.js** - SSR/SSG authentication
366
223
 
367
224
  ## License
368
225