@oxyhq/services 5.5.7 → 5.5.8

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 (34) hide show
  1. package/lib/commonjs/core/index.js +13 -13
  2. package/lib/commonjs/core/index.js.map +1 -1
  3. package/lib/commonjs/ui/context/OxyContext.js +467 -87
  4. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  5. package/lib/commonjs/ui/hooks/useAuthFetch.js +79 -45
  6. package/lib/commonjs/ui/hooks/useAuthFetch.js.map +1 -1
  7. package/lib/commonjs/ui/screens/SessionManagementScreen.js.map +1 -1
  8. package/lib/module/core/index.js +13 -6
  9. package/lib/module/core/index.js.map +1 -1
  10. package/lib/module/ui/context/OxyContext.js +467 -87
  11. package/lib/module/ui/context/OxyContext.js.map +1 -1
  12. package/lib/module/ui/hooks/useAuthFetch.js +79 -45
  13. package/lib/module/ui/hooks/useAuthFetch.js.map +1 -1
  14. package/lib/module/ui/screens/SessionManagementScreen.js.map +1 -1
  15. package/lib/typescript/core/index.d.ts +7 -6
  16. package/lib/typescript/core/index.d.ts.map +1 -1
  17. package/lib/typescript/ui/context/OxyContext.d.ts +11 -7
  18. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  19. package/lib/typescript/ui/hooks/useAuthFetch.d.ts +10 -4
  20. package/lib/typescript/ui/hooks/useAuthFetch.d.ts.map +1 -1
  21. package/package.json +1 -1
  22. package/src/__tests__/backend-middleware.test.ts +209 -0
  23. package/src/__tests__/ui/hooks/useAuthFetch.test.ts +70 -0
  24. package/src/core/index.ts +13 -7
  25. package/src/ui/context/OxyContext.tsx +522 -99
  26. package/src/ui/hooks/useAuthFetch.ts +81 -47
  27. package/src/ui/screens/SessionManagementScreen.tsx +9 -9
  28. package/lib/commonjs/core/AuthManager.js +0 -378
  29. package/lib/commonjs/core/AuthManager.js.map +0 -1
  30. package/lib/module/core/AuthManager.js +0 -373
  31. package/lib/module/core/AuthManager.js.map +0 -1
  32. package/lib/typescript/core/AuthManager.d.ts +0 -100
  33. package/lib/typescript/core/AuthManager.d.ts.map +0 -1
  34. package/src/core/AuthManager.ts +0 -389
@@ -1,389 +0,0 @@
1
- /**
2
- * Centralized Authentication Manager
3
- *
4
- * Single source of truth for all authentication operations
5
- * Handles both JWT and session-based authentication seamlessly
6
- */
7
-
8
- import { OxyServices } from './index';
9
-
10
- export interface AuthState {
11
- isAuthenticated: boolean;
12
- accessToken: string | null;
13
- user: any | null;
14
- activeSessionId: string | null;
15
- sessions: any[]; // For multi-session support
16
- isLoading: boolean;
17
- error: string | null;
18
- }
19
-
20
- export interface AuthConfig {
21
- oxyServices: OxyServices;
22
- onStateChange?: (state: AuthState) => void;
23
- }
24
-
25
- export class AuthManager {
26
- private oxyServices: OxyServices;
27
- private onStateChange?: (state: AuthState) => void;
28
- private currentState: AuthState = {
29
- isAuthenticated: false,
30
- accessToken: null,
31
- user: null,
32
- activeSessionId: null,
33
- sessions: [],
34
- isLoading: false,
35
- error: null,
36
- };
37
-
38
- constructor(config: AuthConfig) {
39
- this.oxyServices = config.oxyServices;
40
- this.onStateChange = config.onStateChange;
41
- }
42
-
43
- /**
44
- * Get current authentication state
45
- */
46
- getState(): AuthState {
47
- return { ...this.currentState };
48
- }
49
-
50
- /**
51
- * Get access token for API calls
52
- * This is the ONLY method that should be used to get tokens
53
- */
54
- async getAccessToken(): Promise<string | null> {
55
- console.log('[AuthManager] getAccessToken called');
56
- console.log('[AuthManager] Current state:', {
57
- isAuthenticated: this.currentState.isAuthenticated,
58
- activeSessionId: this.currentState.activeSessionId,
59
- hasUser: !!this.currentState.user,
60
- cachedToken: !!this.currentState.accessToken
61
- });
62
-
63
- try {
64
- // If we have a cached token in state, return it
65
- if (this.currentState.accessToken) {
66
- console.log('[AuthManager] Returning cached token from state');
67
- return this.currentState.accessToken;
68
- }
69
-
70
- // If we already have a valid JWT token in service, return it
71
- let token = this.oxyServices.getAccessToken();
72
- if (token) {
73
- console.log('[AuthManager] Found token in OxyServices');
74
- // Cache it in our state for consistency
75
- this.updateState({ accessToken: token });
76
- return token;
77
- }
78
-
79
- // If we have an active session, get token from session
80
- if (this.currentState.activeSessionId) {
81
- console.log('[AuthManager] Getting token from session:', this.currentState.activeSessionId);
82
- const tokenData = await this.oxyServices.getTokenBySession(this.currentState.activeSessionId);
83
- token = tokenData.accessToken;
84
-
85
- // Cache it both in service and state
86
- this.oxyServices.setTokens(token, '');
87
- this.updateState({ accessToken: token });
88
- console.log('[AuthManager] Successfully retrieved and cached token from session');
89
- return token;
90
- }
91
-
92
- console.warn('[AuthManager] No way to get access token - not authenticated or no session');
93
- return null;
94
- } catch (error) {
95
- console.error('[AuthManager] Failed to get access token:', error);
96
- return null;
97
- }
98
- }
99
-
100
- /**
101
- * Login with username/password
102
- */
103
- async login(username: string, password: string, deviceName?: string): Promise<void> {
104
- this.updateState({ isLoading: true, error: null });
105
-
106
- try {
107
- // Use secure session login
108
- const response = await this.oxyServices.secureLogin(username, password, deviceName);
109
-
110
- // Get and cache the access token
111
- const tokenData = await this.oxyServices.getTokenBySession(response.sessionId);
112
- this.oxyServices.setTokens(tokenData.accessToken, '');
113
-
114
- // Load full user data
115
- const user = await this.oxyServices.getUserBySession(response.sessionId);
116
-
117
- // Load sessions list
118
- const sessions = await this.oxyServices.getSessionsBySessionId(response.sessionId);
119
-
120
- this.updateState({
121
- isAuthenticated: true,
122
- accessToken: tokenData.accessToken,
123
- user,
124
- activeSessionId: response.sessionId,
125
- sessions,
126
- isLoading: false,
127
- error: null,
128
- });
129
- } catch (error: any) {
130
- this.updateState({
131
- isLoading: false,
132
- error: error.message || 'Login failed',
133
- });
134
- throw error;
135
- }
136
- }
137
-
138
- /**
139
- * Sign up new user
140
- */
141
- async signUp(username: string, email: string, password: string): Promise<void> {
142
- this.updateState({ isLoading: true, error: null });
143
-
144
- try {
145
- // Create account
146
- await this.oxyServices.signUp(username, email, password);
147
-
148
- // Auto-login after signup
149
- await this.login(username, password);
150
- } catch (error: any) {
151
- this.updateState({
152
- isLoading: false,
153
- error: error.message || 'Sign up failed',
154
- });
155
- throw error;
156
- }
157
- }
158
-
159
- /**
160
- * Logout current session or specific session
161
- */
162
- async logout(targetSessionId?: string): Promise<void> {
163
- if (!this.currentState.activeSessionId) return;
164
-
165
- const sessionToLogout = targetSessionId || this.currentState.activeSessionId;
166
-
167
- try {
168
- await this.oxyServices.logoutSecureSession(
169
- this.currentState.activeSessionId,
170
- sessionToLogout
171
- );
172
- } catch (error) {
173
- console.warn('[AuthManager] Logout API call failed:', error);
174
- }
175
-
176
- // If logging out current session, clear all state
177
- if (sessionToLogout === this.currentState.activeSessionId) {
178
- this.clearAuthState();
179
- } else {
180
- // Just remove the specific session from the list
181
- const updatedSessions = this.currentState.sessions.filter(
182
- (s: any) => s.sessionId !== sessionToLogout
183
- );
184
- this.updateState({ sessions: updatedSessions });
185
- }
186
- }
187
-
188
- /**
189
- * Initialize authentication from stored data
190
- */
191
- async initialize(activeSessionId: string | null): Promise<void> {
192
- if (!activeSessionId) {
193
- this.clearAuthState();
194
- return;
195
- }
196
-
197
- this.updateState({ isLoading: true, error: null });
198
-
199
- try {
200
- // Validate the session
201
- const validation = await this.oxyServices.validateSession(activeSessionId);
202
-
203
- if (!validation.valid) {
204
- this.clearAuthState();
205
- return;
206
- }
207
-
208
- // Get access token
209
- const tokenData = await this.oxyServices.getTokenBySession(activeSessionId);
210
- this.oxyServices.setTokens(tokenData.accessToken, '');
211
-
212
- // Load user data
213
- const user = await this.oxyServices.getUserBySession(activeSessionId);
214
-
215
- // Load sessions list
216
- const sessions = await this.oxyServices.getSessionsBySessionId(activeSessionId);
217
-
218
- this.updateState({
219
- isAuthenticated: true,
220
- accessToken: tokenData.accessToken,
221
- user,
222
- activeSessionId,
223
- sessions,
224
- isLoading: false,
225
- error: null,
226
- });
227
- } catch (error) {
228
- console.error('[AuthManager] Initialization failed:', error);
229
- this.clearAuthState();
230
- }
231
- }
232
-
233
- /**
234
- * Refresh current token
235
- */
236
- async refreshToken(): Promise<void> {
237
- if (!this.currentState.activeSessionId) {
238
- throw new Error('No active session to refresh');
239
- }
240
-
241
- try {
242
- const tokenData = await this.oxyServices.getTokenBySession(this.currentState.activeSessionId);
243
- this.oxyServices.setTokens(tokenData.accessToken, '');
244
-
245
- this.updateState({
246
- accessToken: tokenData.accessToken,
247
- });
248
- } catch (error) {
249
- console.error('[AuthManager] Token refresh failed:', error);
250
- this.clearAuthState();
251
- throw error;
252
- }
253
- }
254
-
255
- /**
256
- * Check if user is authenticated
257
- */
258
- isAuthenticated(): boolean {
259
- const result = this.currentState.isAuthenticated;
260
- console.log('[AuthManager] isAuthenticated check:', result);
261
- return result;
262
- }
263
-
264
- /**
265
- * Get current user
266
- */
267
- getCurrentUser(): any | null {
268
- return this.currentState.user;
269
- }
270
-
271
- /**
272
- * Get current session ID
273
- */
274
- getActiveSessionId(): string | null {
275
- return this.currentState.activeSessionId;
276
- }
277
-
278
- /**
279
- * Get base URL for API calls
280
- */
281
- getBaseURL(): string {
282
- return this.oxyServices.getBaseURL();
283
- }
284
-
285
- /**
286
- * Refresh sessions list
287
- */
288
- async refreshSessions(): Promise<void> {
289
- if (!this.currentState.activeSessionId) return;
290
-
291
- try {
292
- const sessions = await this.oxyServices.getSessionsBySessionId(this.currentState.activeSessionId);
293
- this.updateState({ sessions });
294
- } catch (error) {
295
- console.error('[AuthManager] Failed to refresh sessions:', error);
296
- }
297
- }
298
-
299
- /**
300
- * Get current sessions
301
- */
302
- getSessions(): any[] {
303
- return this.currentState.sessions;
304
- }
305
-
306
- /**
307
- * Switch to a different session
308
- */
309
- async switchSession(sessionId: string): Promise<void> {
310
- if (sessionId === this.currentState.activeSessionId) return;
311
-
312
- this.updateState({ isLoading: true, error: null });
313
-
314
- try {
315
- // Get access token for this session
316
- const tokenData = await this.oxyServices.getTokenBySession(sessionId);
317
- this.oxyServices.setTokens(tokenData.accessToken, '');
318
-
319
- // Load user data
320
- const user = await this.oxyServices.getUserBySession(sessionId);
321
-
322
- this.updateState({
323
- activeSessionId: sessionId,
324
- accessToken: tokenData.accessToken,
325
- user,
326
- isLoading: false,
327
- });
328
- } catch (error) {
329
- console.error('[AuthManager] Switch session failed:', error);
330
- this.updateState({
331
- isLoading: false,
332
- error: 'Failed to switch session',
333
- });
334
- throw error;
335
- }
336
- }
337
-
338
- /**
339
- * Remove a session (alias for logout with sessionId)
340
- */
341
- async removeSession(sessionId: string): Promise<void> {
342
- await this.logout(sessionId);
343
- }
344
-
345
- /**
346
- * Logout all sessions
347
- */
348
- async logoutAll(): Promise<void> {
349
- if (!this.currentState.activeSessionId) {
350
- throw new Error('No active session found');
351
- }
352
-
353
- try {
354
- await this.oxyServices.logoutAllSecureSessions(this.currentState.activeSessionId);
355
- this.clearAuthState();
356
- } catch (error) {
357
- console.error('[AuthManager] Logout all failed:', error);
358
- throw error;
359
- }
360
- }
361
-
362
- /**
363
- * Clear authentication state
364
- */
365
- private clearAuthState(): void {
366
- this.oxyServices.setTokens('', ''); // Clear tokens from service
367
-
368
- this.updateState({
369
- isAuthenticated: false,
370
- accessToken: null,
371
- user: null,
372
- activeSessionId: null,
373
- sessions: [],
374
- isLoading: false,
375
- error: null,
376
- });
377
- }
378
-
379
- /**
380
- * Update state and notify listeners
381
- */
382
- private updateState(updates: Partial<AuthState>): void {
383
- this.currentState = { ...this.currentState, ...updates };
384
-
385
- if (this.onStateChange) {
386
- this.onStateChange(this.currentState);
387
- }
388
- }
389
- }