@oxyhq/services 5.8.1 → 5.8.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.
Files changed (137) hide show
  1. package/lib/commonjs/index.js +9 -27
  2. package/lib/commonjs/index.js.map +1 -1
  3. package/lib/commonjs/node/createAuth.js +7 -585
  4. package/lib/commonjs/node/createAuth.js.map +1 -1
  5. package/lib/commonjs/node/index.js +1 -38
  6. package/lib/commonjs/node/index.js.map +1 -1
  7. package/lib/commonjs/ui/components/FollowButton.js +100 -12
  8. package/lib/commonjs/ui/components/FollowButton.js.map +1 -1
  9. package/lib/commonjs/ui/components/Header.js +40 -6
  10. package/lib/commonjs/ui/components/Header.js.map +1 -1
  11. package/lib/commonjs/ui/components/OxyProvider.js +5 -0
  12. package/lib/commonjs/ui/components/OxyProvider.js.map +1 -1
  13. package/lib/commonjs/ui/context/OxyContext.js +63 -125
  14. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  15. package/lib/commonjs/ui/hooks/index.js +6 -0
  16. package/lib/commonjs/ui/hooks/index.js.map +1 -1
  17. package/lib/commonjs/ui/hooks/useFollow.js +59 -2
  18. package/lib/commonjs/ui/hooks/useFollow.js.map +1 -1
  19. package/lib/commonjs/ui/navigation/OxyRouter.js +10 -0
  20. package/lib/commonjs/ui/navigation/OxyRouter.js.map +1 -1
  21. package/lib/commonjs/ui/screens/AccountSettingsScreen.js +9 -0
  22. package/lib/commonjs/ui/screens/AccountSettingsScreen.js.map +1 -1
  23. package/lib/commonjs/ui/screens/ProfileScreen.js +214 -37
  24. package/lib/commonjs/ui/screens/ProfileScreen.js.map +1 -1
  25. package/lib/commonjs/ui/screens/UserLinksScreen.js +90 -0
  26. package/lib/commonjs/ui/screens/UserLinksScreen.js.map +1 -0
  27. package/lib/commonjs/ui/screens/karma/KarmaAboutScreen.js +9 -6
  28. package/lib/commonjs/ui/screens/karma/KarmaAboutScreen.js.map +1 -1
  29. package/lib/commonjs/ui/screens/karma/KarmaCenterScreen.js +3 -30
  30. package/lib/commonjs/ui/screens/karma/KarmaCenterScreen.js.map +1 -1
  31. package/lib/commonjs/ui/screens/karma/KarmaFAQScreen.js +37 -46
  32. package/lib/commonjs/ui/screens/karma/KarmaFAQScreen.js.map +1 -1
  33. package/lib/commonjs/ui/screens/karma/KarmaLeaderboardScreen.js +9 -12
  34. package/lib/commonjs/ui/screens/karma/KarmaLeaderboardScreen.js.map +1 -1
  35. package/lib/commonjs/ui/screens/karma/KarmaRewardsScreen.js +9 -12
  36. package/lib/commonjs/ui/screens/karma/KarmaRewardsScreen.js.map +1 -1
  37. package/lib/commonjs/ui/screens/karma/KarmaRulesScreen.js +9 -12
  38. package/lib/commonjs/ui/screens/karma/KarmaRulesScreen.js.map +1 -1
  39. package/lib/commonjs/ui/stores/authStore.js +24 -6
  40. package/lib/commonjs/ui/stores/authStore.js.map +1 -1
  41. package/lib/commonjs/ui/stores/followStore.js +106 -1
  42. package/lib/commonjs/ui/stores/followStore.js.map +1 -1
  43. package/lib/module/index.js +1 -3
  44. package/lib/module/index.js.map +1 -1
  45. package/lib/module/node/createAuth.js +7 -584
  46. package/lib/module/node/createAuth.js.map +1 -1
  47. package/lib/module/node/index.js +1 -7
  48. package/lib/module/node/index.js.map +1 -1
  49. package/lib/module/ui/components/FollowButton.js +101 -13
  50. package/lib/module/ui/components/FollowButton.js.map +1 -1
  51. package/lib/module/ui/components/Header.js +40 -6
  52. package/lib/module/ui/components/Header.js.map +1 -1
  53. package/lib/module/ui/components/OxyProvider.js +5 -0
  54. package/lib/module/ui/components/OxyProvider.js.map +1 -1
  55. package/lib/module/ui/context/OxyContext.js +63 -125
  56. package/lib/module/ui/context/OxyContext.js.map +1 -1
  57. package/lib/module/ui/hooks/index.js +1 -1
  58. package/lib/module/ui/hooks/index.js.map +1 -1
  59. package/lib/module/ui/hooks/useFollow.js +57 -1
  60. package/lib/module/ui/hooks/useFollow.js.map +1 -1
  61. package/lib/module/ui/navigation/OxyRouter.js +10 -0
  62. package/lib/module/ui/navigation/OxyRouter.js.map +1 -1
  63. package/lib/module/ui/screens/AccountSettingsScreen.js +9 -0
  64. package/lib/module/ui/screens/AccountSettingsScreen.js.map +1 -1
  65. package/lib/module/ui/screens/ProfileScreen.js +214 -37
  66. package/lib/module/ui/screens/ProfileScreen.js.map +1 -1
  67. package/lib/module/ui/screens/UserLinksScreen.js +85 -0
  68. package/lib/module/ui/screens/UserLinksScreen.js.map +1 -0
  69. package/lib/module/ui/screens/karma/KarmaAboutScreen.js +9 -6
  70. package/lib/module/ui/screens/karma/KarmaAboutScreen.js.map +1 -1
  71. package/lib/module/ui/screens/karma/KarmaCenterScreen.js +3 -30
  72. package/lib/module/ui/screens/karma/KarmaCenterScreen.js.map +1 -1
  73. package/lib/module/ui/screens/karma/KarmaFAQScreen.js +37 -46
  74. package/lib/module/ui/screens/karma/KarmaFAQScreen.js.map +1 -1
  75. package/lib/module/ui/screens/karma/KarmaLeaderboardScreen.js +9 -12
  76. package/lib/module/ui/screens/karma/KarmaLeaderboardScreen.js.map +1 -1
  77. package/lib/module/ui/screens/karma/KarmaRewardsScreen.js +9 -12
  78. package/lib/module/ui/screens/karma/KarmaRewardsScreen.js.map +1 -1
  79. package/lib/module/ui/screens/karma/KarmaRulesScreen.js +9 -12
  80. package/lib/module/ui/screens/karma/KarmaRulesScreen.js.map +1 -1
  81. package/lib/module/ui/stores/authStore.js +24 -6
  82. package/lib/module/ui/stores/authStore.js.map +1 -1
  83. package/lib/module/ui/stores/followStore.js +106 -1
  84. package/lib/module/ui/stores/followStore.js.map +1 -1
  85. package/lib/typescript/index.d.ts +1 -1
  86. package/lib/typescript/index.d.ts.map +1 -1
  87. package/lib/typescript/node/createAuth.d.ts +0 -112
  88. package/lib/typescript/node/createAuth.d.ts.map +1 -1
  89. package/lib/typescript/node/index.d.ts +0 -2
  90. package/lib/typescript/node/index.d.ts.map +1 -1
  91. package/lib/typescript/ui/components/FollowButton.d.ts +1 -0
  92. package/lib/typescript/ui/components/FollowButton.d.ts.map +1 -1
  93. package/lib/typescript/ui/components/Header.d.ts +2 -0
  94. package/lib/typescript/ui/components/Header.d.ts.map +1 -1
  95. package/lib/typescript/ui/components/OxyProvider.d.ts.map +1 -1
  96. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  97. package/lib/typescript/ui/hooks/index.d.ts +1 -1
  98. package/lib/typescript/ui/hooks/index.d.ts.map +1 -1
  99. package/lib/typescript/ui/hooks/useFollow.d.ts +20 -0
  100. package/lib/typescript/ui/hooks/useFollow.d.ts.map +1 -1
  101. package/lib/typescript/ui/navigation/OxyRouter.d.ts.map +1 -1
  102. package/lib/typescript/ui/screens/AccountSettingsScreen.d.ts.map +1 -1
  103. package/lib/typescript/ui/screens/ProfileScreen.d.ts.map +1 -1
  104. package/lib/typescript/ui/screens/UserLinksScreen.d.ts +15 -0
  105. package/lib/typescript/ui/screens/UserLinksScreen.d.ts.map +1 -0
  106. package/lib/typescript/ui/screens/karma/KarmaAboutScreen.d.ts.map +1 -1
  107. package/lib/typescript/ui/screens/karma/KarmaCenterScreen.d.ts.map +1 -1
  108. package/lib/typescript/ui/screens/karma/KarmaFAQScreen.d.ts.map +1 -1
  109. package/lib/typescript/ui/screens/karma/KarmaLeaderboardScreen.d.ts.map +1 -1
  110. package/lib/typescript/ui/screens/karma/KarmaRewardsScreen.d.ts.map +1 -1
  111. package/lib/typescript/ui/screens/karma/KarmaRulesScreen.d.ts.map +1 -1
  112. package/lib/typescript/ui/stores/authStore.d.ts +3 -1
  113. package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
  114. package/lib/typescript/ui/stores/followStore.d.ts +10 -0
  115. package/lib/typescript/ui/stores/followStore.d.ts.map +1 -1
  116. package/package.json +3 -2
  117. package/src/index.ts +2 -10
  118. package/src/node/createAuth.ts +7 -623
  119. package/src/node/index.ts +1 -19
  120. package/src/ui/components/FollowButton.tsx +95 -11
  121. package/src/ui/components/Header.tsx +45 -4
  122. package/src/ui/components/OxyProvider.tsx +6 -0
  123. package/src/ui/context/OxyContext.tsx +65 -136
  124. package/src/ui/hooks/index.ts +1 -1
  125. package/src/ui/hooks/useFollow.ts +63 -0
  126. package/src/ui/navigation/OxyRouter.tsx +10 -0
  127. package/src/ui/screens/AccountSettingsScreen.tsx +8 -0
  128. package/src/ui/screens/ProfileScreen.tsx +191 -28
  129. package/src/ui/screens/UserLinksScreen.tsx +96 -0
  130. package/src/ui/screens/karma/KarmaAboutScreen.tsx +9 -2
  131. package/src/ui/screens/karma/KarmaCenterScreen.tsx +1 -20
  132. package/src/ui/screens/karma/KarmaFAQScreen.tsx +40 -24
  133. package/src/ui/screens/karma/KarmaLeaderboardScreen.tsx +9 -3
  134. package/src/ui/screens/karma/KarmaRewardsScreen.tsx +9 -3
  135. package/src/ui/screens/karma/KarmaRulesScreen.tsx +9 -3
  136. package/src/ui/stores/authStore.ts +22 -7
  137. package/src/ui/stores/followStore.ts +102 -1
@@ -3,428 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.OxyAuth = void 0;
7
6
  exports.createAuth = createAuth;
8
7
  var _express = _interopRequireDefault(require("express"));
9
8
  var _core = require("../core");
10
- var _jwtDecode = require("jwt-decode");
11
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
- // Types for enhanced authentication
13
-
14
- // User cache for performance
15
- class UserCache {
16
- cache = new Map();
17
- constructor(ttl = 300) {
18
- // 5 minutes default
19
- this.ttl = ttl * 1000;
20
- }
21
- set(userId, user) {
22
- this.cache.set(userId, {
23
- user,
24
- expiresAt: Date.now() + this.ttl
25
- });
26
- }
27
- get(userId) {
28
- const item = this.cache.get(userId);
29
- if (!item || Date.now() > item.expiresAt) {
30
- this.cache.delete(userId);
31
- return null;
32
- }
33
- return item.user;
34
- }
35
- clear() {
36
- this.cache.clear();
37
- }
38
- }
39
-
40
- /**
41
- * Enhanced OxyAuth class for backend authentication
42
- */
43
- class OxyAuth {
44
- userCache = null;
45
- constructor(options) {
46
- this.options = {
47
- loadFullUser: true,
48
- enableSessionAuth: true,
49
- enableDeviceAuth: true,
50
- cacheUserData: true,
51
- userCacheTTL: 300,
52
- ...options
53
- };
54
- this.oxy = new _core.OxyServices({
55
- baseURL: options.baseURL
56
- });
57
- if (this.options.cacheUserData) {
58
- this.userCache = new UserCache(this.options.userCacheTTL);
59
- }
60
- }
61
-
62
- /**
63
- * Create authentication middleware
64
- */
65
- createAuthMiddleware(options = {}) {
66
- return async (req, res, next) => {
67
- try {
68
- const result = await this.authenticateRequest(req);
69
- if (!result.valid && options.required !== false) {
70
- const error = {
71
- message: 'Authentication required',
72
- code: 'AUTH_REQUIRED'
73
- };
74
- if (options.onError) {
75
- options.onError(error, req, res);
76
- } else {
77
- res.status(401).json(error);
78
- }
79
- return;
80
- }
81
-
82
- // Check roles if specified
83
- if (result.valid && options.roles && result.user) {
84
- const hasRole = options.roles.some(role => result.user.roles?.includes(role) || result.user.role === role);
85
- if (!hasRole) {
86
- const error = {
87
- message: 'Insufficient permissions',
88
- code: 'INSUFFICIENT_ROLES'
89
- };
90
- if (options.onError) {
91
- options.onError(error, req, res);
92
- } else {
93
- res.status(403).json(error);
94
- }
95
- return;
96
- }
97
- }
98
-
99
- // Check permissions if specified
100
- if (result.valid && options.permissions && result.userId) {
101
- for (const permission of options.permissions) {
102
- const hasPermission = await this.hasPermission(result.userId, permission);
103
- if (!hasPermission) {
104
- const error = {
105
- message: 'Insufficient permissions',
106
- code: 'INSUFFICIENT_PERMISSIONS'
107
- };
108
- if (options.onError) {
109
- options.onError(error, req, res);
110
- } else {
111
- res.status(403).json(error);
112
- }
113
- return;
114
- }
115
- }
116
- }
117
- next();
118
- } catch (error) {
119
- if (options.onError) {
120
- options.onError(error, req, res);
121
- } else {
122
- res.status(500).json({
123
- message: 'Authentication error'
124
- });
125
- }
126
- }
127
- };
128
- }
129
-
130
- /**
131
- * Authenticate request and populate user data
132
- */
133
- async authenticateRequest(req) {
134
- // Try JWT token first
135
- const authHeader = req.headers.authorization;
136
- if (authHeader && authHeader.startsWith('Bearer ')) {
137
- const token = authHeader.substring(7);
138
- const result = await this.validateToken(token);
139
- if (result.valid) {
140
- req.user = result.user;
141
- req.userId = result.userId;
142
- req.accessToken = token;
143
- return {
144
- ...result,
145
- accessToken: token
146
- };
147
- }
148
- }
149
-
150
- // Try session-based auth
151
- if (this.options.enableSessionAuth) {
152
- const sessionId = req.headers['x-session-id'];
153
- if (sessionId) {
154
- const result = await this.validateSession(sessionId);
155
- if (result.valid) {
156
- req.user = result.user;
157
- req.userId = result.userId;
158
- req.sessionId = sessionId;
159
- return result;
160
- }
161
- }
162
- }
163
-
164
- // Try device-based auth
165
- if (this.options.enableDeviceAuth) {
166
- const deviceFingerprint = req.headers['x-device-fingerprint'];
167
- const userId = req.headers['x-user-id'];
168
- if (deviceFingerprint && userId) {
169
- const result = await this.validateDevice(userId, deviceFingerprint);
170
- if (result.valid) {
171
- req.user = result.user;
172
- req.userId = result.userId;
173
- req.deviceFingerprint = deviceFingerprint;
174
- return result;
175
- }
176
- }
177
- }
178
- return {
179
- valid: false,
180
- error: 'No valid authentication found'
181
- };
182
- }
183
-
184
- /**
185
- * Validate JWT token
186
- */
187
- async validateToken(token) {
188
- try {
189
- // Local JWT validation if secret is provided
190
- if (this.options.jwtSecret) {
191
- const decoded = (0, _jwtDecode.jwtDecode)(token);
192
- const currentTime = Math.floor(Date.now() / 1000);
193
- if (decoded.exp && decoded.exp < currentTime) {
194
- return {
195
- valid: false,
196
- error: 'Token expired',
197
- code: 'TOKEN_EXPIRED',
198
- expiresAt: decoded.exp
199
- };
200
- }
201
- const userId = decoded.userId || decoded.id;
202
- if (!userId) {
203
- return {
204
- valid: false,
205
- error: 'Invalid token payload',
206
- code: 'INVALID_PAYLOAD'
207
- };
208
- }
209
-
210
- // Get user data from cache or API
211
- let user = this.userCache?.get(userId);
212
- const cached = !!user;
213
- if (!user && this.options.loadFullUser) {
214
- try {
215
- user = await this.oxy.getUserById(userId);
216
- this.userCache?.set(userId, user);
217
- } catch (error) {
218
- user = {
219
- id: userId
220
- };
221
- }
222
- } else if (!user) {
223
- user = {
224
- id: userId
225
- };
226
- }
227
- return {
228
- valid: true,
229
- userId,
230
- user,
231
- expiresAt: decoded.exp,
232
- cached
233
- };
234
- }
235
-
236
- // Remote validation using OxyServices
237
- const tempOxy = new _core.OxyServices({
238
- baseURL: this.oxy.getBaseURL()
239
- });
240
- tempOxy.setTokens(token, '');
241
- const isValid = await tempOxy.validate();
242
- if (!isValid) {
243
- return {
244
- valid: false,
245
- error: 'Invalid token',
246
- code: 'INVALID_TOKEN'
247
- };
248
- }
249
- const userId = tempOxy.getCurrentUserId();
250
- if (!userId) {
251
- return {
252
- valid: false,
253
- error: 'Invalid token payload',
254
- code: 'INVALID_PAYLOAD'
255
- };
256
- }
257
-
258
- // Get user data
259
- let user = this.userCache?.get(userId);
260
- const cached = !!user;
261
- if (!user && this.options.loadFullUser) {
262
- try {
263
- user = await tempOxy.getUserById(userId);
264
- this.userCache?.set(userId, user);
265
- } catch (error) {
266
- user = {
267
- id: userId
268
- };
269
- }
270
- } else if (!user) {
271
- user = {
272
- id: userId
273
- };
274
- }
275
- return {
276
- valid: true,
277
- userId,
278
- user,
279
- cached
280
- };
281
- } catch (error) {
282
- return {
283
- valid: false,
284
- error: 'Token validation failed',
285
- code: 'VALIDATION_ERROR'
286
- };
287
- }
288
- }
289
-
290
- /**
291
- * Validate session-based authentication
292
- */
293
- async validateSession(sessionId, deviceFingerprint) {
294
- try {
295
- // This would integrate with your session management system
296
- // For now, it's a placeholder implementation
297
- return {
298
- valid: false,
299
- error: 'Session validation not implemented',
300
- code: 'NOT_IMPLEMENTED'
301
- };
302
- } catch (error) {
303
- return {
304
- valid: false,
305
- error: 'Session validation failed',
306
- code: 'VALIDATION_ERROR'
307
- };
308
- }
309
- }
310
-
311
- /**
312
- * Validate device-based authentication
313
- */
314
- async validateDevice(userId, deviceFingerprint) {
315
- try {
316
- // This would validate device fingerprint against stored data
317
- // For now, it's a placeholder implementation
318
- return {
319
- valid: false,
320
- error: 'Device validation not implemented',
321
- code: 'NOT_IMPLEMENTED'
322
- };
323
- } catch (error) {
324
- return {
325
- valid: false,
326
- error: 'Device validation failed',
327
- code: 'VALIDATION_ERROR'
328
- };
329
- }
330
- }
331
-
332
- /**
333
- * Create role-based middleware
334
- */
335
- requireRole(roles) {
336
- const roleArray = Array.isArray(roles) ? roles : [roles];
337
- return this.createAuthMiddleware({
338
- required: true,
339
- roles: roleArray
340
- });
341
- }
342
-
343
- /**
344
- * Create permission-based middleware
345
- */
346
- requirePermission(permissions) {
347
- const permissionArray = Array.isArray(permissions) ? permissions : [permissions];
348
- return this.createAuthMiddleware({
349
- required: true,
350
- permissions: permissionArray
351
- });
352
- }
353
-
354
- /**
355
- * Create optional authentication middleware
356
- */
357
- optionalAuth() {
358
- return this.createAuthMiddleware({
359
- required: false,
360
- onError: () => {} // No error thrown for optional auth
361
- });
362
- }
363
-
364
- /**
365
- * Clear user cache
366
- */
367
- clearCache() {
368
- this.userCache?.clear();
369
- }
370
-
371
- /**
372
- * Check if user data is cached for a given token
373
- */
374
- isUserCached(token) {
375
- try {
376
- const decoded = (0, _jwtDecode.jwtDecode)(token);
377
- const userId = decoded.userId || decoded.id;
378
- return userId ? this.userCache?.get(userId) !== null : false;
379
- } catch {
380
- return false;
381
- }
382
- }
383
-
384
- /**
385
- * Check if user has a specific permission
386
- */
387
- async hasPermission(userId, permission) {
388
- try {
389
- // This is a placeholder implementation
390
- // In a real implementation, you would check against user roles/permissions
391
- const user = this.userCache?.get(userId) || (await this.oxy.getUserById(userId));
392
- return user?.permissions?.includes(permission) || user?.role === 'admin' || false;
393
- } catch {
394
- return false;
395
- }
396
- }
397
-
398
- /**
399
- * Get OxyServices instance
400
- */
401
- getOxyServices() {
402
- return this.oxy;
403
- }
404
- }
405
- exports.OxyAuth = OxyAuth;
406
- /**
407
- * Enhanced createAuth function that provides both router and middleware capabilities
408
- *
409
- * This is a unified authentication system that:
410
- * 1. Maintains backward compatibility with the old router-based approach
411
- * 2. Adds powerful new middleware capabilities
412
- * 3. Includes caching, role-based access, and performance optimizations
413
- * 4. Supports multiple authentication strategies
414
- */
415
10
  function createAuth(options) {
416
- // Create the enhanced OxyAuth instance
417
- const authOptions = {
418
- baseURL: options.baseURL,
419
- jwtSecret: options.jwtSecret,
420
- loadFullUser: options.loadFullUser ?? true,
421
- enableSessionAuth: options.enableSessionAuth ?? true,
422
- enableDeviceAuth: options.enableDeviceAuth ?? true,
423
- cacheUserData: options.cacheUserData ?? true,
424
- userCacheTTL: options.userCacheTTL ?? 300
425
- };
426
- const oxyAuth = new OxyAuth(authOptions);
427
- const oxy = oxyAuth.getOxyServices();
11
+ const oxy = new _core.OxyServices({
12
+ baseURL: options.baseURL
13
+ });
428
14
  const router = _express.default.Router();
429
15
 
430
16
  // Helper to handle async route functions
@@ -437,237 +23,73 @@ function createAuth(options) {
437
23
  });
438
24
  }
439
25
  };
440
-
441
- // Enhanced signup with validation
442
26
  router.post('/signup', wrap(async (req, res) => {
443
27
  const {
444
28
  username,
445
29
  email,
446
30
  password
447
31
  } = req.body;
448
-
449
- // Enhanced validation
450
- if (!username || !email || !password) {
451
- return res.status(400).json({
452
- message: 'Username, email, and password are required'
453
- });
454
- }
455
32
  const result = await oxy.signUp(username, email, password);
456
33
  res.json(result);
457
34
  }));
458
-
459
- // Enhanced login with device fingerprinting
460
35
  router.post('/login', wrap(async (req, res) => {
461
36
  const {
462
37
  username,
463
- password,
464
- deviceFingerprint
38
+ password
465
39
  } = req.body;
466
- if (!username || !password) {
467
- return res.status(400).json({
468
- message: 'Username and password are required'
469
- });
470
- }
471
40
  const result = await oxy.login(username, password);
472
-
473
- // Store device fingerprint if provided
474
- if (deviceFingerprint && result.user?.id) {
475
- // This could be stored in a database for device tracking
476
- console.log(`Device login: ${deviceFingerprint} for user ${result.user.id}`);
477
- }
478
41
  res.json(result);
479
42
  }));
480
-
481
- // Enhanced logout with session management
482
43
  router.post('/logout', wrap(async (req, res) => {
483
44
  const token = req.headers.authorization?.split(' ')[1];
484
45
  const refreshToken = req.body.refreshToken;
485
- const sessionId = req.body.sessionId;
486
46
  if (token) oxy.setTokens(token, refreshToken);
487
-
488
- // Enhanced logout with session tracking
489
- if (sessionId) {
490
- await oxy.logoutSession(sessionId);
491
- } else {
492
- await oxy.logout();
493
- }
47
+ await oxy.logout();
494
48
  res.json({
495
49
  success: true
496
50
  });
497
51
  }));
498
-
499
- // Enhanced token refresh
500
52
  router.post('/refresh', wrap(async (req, res) => {
501
53
  const refreshToken = req.body.refreshToken;
502
54
  const accessToken = req.headers.authorization?.split(' ')[1] || '';
503
- if (!refreshToken) {
504
- return res.status(400).json({
505
- message: 'Refresh token is required'
506
- });
507
- }
508
55
  oxy.setTokens(accessToken, refreshToken);
509
56
  const tokens = await oxy.refreshTokens();
510
57
  res.json(tokens);
511
58
  }));
512
-
513
- // Enhanced token validation with caching
514
59
  router.get('/validate', wrap(async (req, res) => {
515
60
  const token = req.headers.authorization?.split(' ')[1] || '';
516
- if (!token) {
517
- return res.status(401).json({
518
- valid: false,
519
- message: 'No token provided'
520
- });
521
- }
522
61
  oxy.setTokens(token, '');
523
62
  const valid = await oxy.validate();
524
-
525
- // Enhanced response with more details
526
63
  res.json({
527
- valid,
528
- timestamp: new Date().toISOString(),
529
- cached: oxyAuth.isUserCached(token) // Check if user data is cached
64
+ valid
530
65
  });
531
66
  }));
532
-
533
- // Enhanced sessions management
534
67
  router.get('/sessions', wrap(async (req, res) => {
535
68
  const token = req.headers.authorization?.split(' ')[1] || '';
536
- if (!token) {
537
- return res.status(401).json({
538
- message: 'Authentication required'
539
- });
540
- }
541
69
  oxy.setTokens(token, '');
542
70
  const sessions = await oxy.getUserSessions();
543
71
  res.json(sessions);
544
72
  }));
545
-
546
- // Enhanced session deletion
547
73
  router.delete('/sessions/:id', wrap(async (req, res) => {
548
74
  const token = req.headers.authorization?.split(' ')[1] || '';
549
- if (!token) {
550
- return res.status(401).json({
551
- message: 'Authentication required'
552
- });
553
- }
554
75
  oxy.setTokens(token, '');
555
76
  const result = await oxy.logoutSession(req.params.id);
556
-
557
- // Clear cache for this user if logout was successful
558
- if (result.success) {
559
- oxyAuth.clearCache();
560
- }
561
77
  res.json(result);
562
78
  }));
563
-
564
- // Enhanced logout other sessions
565
79
  router.post('/sessions/logout-others', wrap(async (req, res) => {
566
80
  const token = req.headers.authorization?.split(' ')[1] || '';
567
- if (!token) {
568
- return res.status(401).json({
569
- message: 'Authentication required'
570
- });
571
- }
572
81
  oxy.setTokens(token, '');
573
82
  const result = await oxy.logoutOtherSessions();
574
-
575
- // Clear cache for this user
576
- if (result.success) {
577
- oxyAuth.clearCache();
578
- }
579
83
  res.json(result);
580
84
  }));
581
-
582
- // Enhanced logout all sessions
583
85
  router.post('/sessions/logout-all', wrap(async (req, res) => {
584
86
  const token = req.headers.authorization?.split(' ')[1] || '';
585
- if (!token) {
586
- return res.status(401).json({
587
- message: 'Authentication required'
588
- });
589
- }
590
87
  oxy.setTokens(token, '');
591
88
  const result = await oxy.logoutAllSessions();
592
-
593
- // Clear all cache
594
- if (result.success) {
595
- oxyAuth.clearCache();
596
- }
597
89
  res.json(result);
598
90
  }));
599
-
600
- // NEW: Get current user profile with caching
601
- router.get('/profile', wrap(async (req, res) => {
602
- const token = req.headers.authorization?.split(' ')[1] || '';
603
- if (!token) {
604
- return res.status(401).json({
605
- message: 'Authentication required'
606
- });
607
- }
608
-
609
- // Use the enhanced auth system for better performance
610
- const validation = await oxyAuth.validateToken(token);
611
- if (!validation.valid) {
612
- return res.status(401).json({
613
- message: 'Invalid token'
614
- });
615
- }
616
- res.json({
617
- user: validation.user,
618
- cached: validation.cached,
619
- expiresAt: validation.expiresAt
620
- });
621
- }));
622
-
623
- // NEW: Check user permissions
624
- router.post('/check-permissions', wrap(async (req, res) => {
625
- const token = req.headers.authorization?.split(' ')[1] || '';
626
- const {
627
- permissions
628
- } = req.body;
629
- if (!token) {
630
- return res.status(401).json({
631
- message: 'Authentication required'
632
- });
633
- }
634
- if (!permissions || !Array.isArray(permissions)) {
635
- return res.status(400).json({
636
- message: 'Permissions array is required'
637
- });
638
- }
639
- const validation = await oxyAuth.validateToken(token);
640
- if (!validation.valid) {
641
- return res.status(401).json({
642
- message: 'Invalid token'
643
- });
644
- }
645
-
646
- // Check each permission
647
- const results = await Promise.all(permissions.map(async permission => {
648
- const hasPermission = await oxyAuth.hasPermission(validation.userId, permission);
649
- return {
650
- permission,
651
- granted: hasPermission
652
- };
653
- }));
654
- res.json({
655
- permissions: results
656
- });
657
- }));
658
91
  return {
659
- middleware: router,
660
- // NEW: Expose the enhanced auth system
661
- auth: oxyAuth,
662
- // NEW: Convenience methods for middleware
663
- requireAuth: (roles, permissions) => oxyAuth.createAuthMiddleware({
664
- required: true,
665
- roles: Array.isArray(roles) ? roles : roles ? [roles] : undefined,
666
- permissions: Array.isArray(permissions) ? permissions : permissions ? [permissions] : undefined
667
- }),
668
- optionalAuth: () => oxyAuth.optionalAuth(),
669
- requireRole: roles => oxyAuth.requireRole(roles),
670
- requirePermission: permissions => oxyAuth.requirePermission(permissions)
92
+ middleware: router
671
93
  };
672
94
  }
673
95
  //# sourceMappingURL=createAuth.js.map