@oxyhq/services 5.16.22 → 5.16.24

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 (73) hide show
  1. package/lib/commonjs/core/HttpService.js +15 -79
  2. package/lib/commonjs/core/HttpService.js.map +1 -1
  3. package/lib/commonjs/core/OxyServices.base.js +3 -11
  4. package/lib/commonjs/core/OxyServices.base.js.map +1 -1
  5. package/lib/commonjs/core/services/SessionService.js +163 -0
  6. package/lib/commonjs/core/services/SessionService.js.map +1 -0
  7. package/lib/commonjs/core/services/TokenService.js +206 -0
  8. package/lib/commonjs/core/services/TokenService.js.map +1 -0
  9. package/lib/commonjs/models/interfaces.js +15 -0
  10. package/lib/commonjs/models/interfaces.js.map +1 -1
  11. package/lib/commonjs/ui/context/OxyContext.js +100 -22
  12. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  13. package/lib/commonjs/ui/context/hooks/useAuthOperations.js +11 -3
  14. package/lib/commonjs/ui/context/hooks/useAuthOperations.js.map +1 -1
  15. package/lib/commonjs/ui/hooks/useSessionSocket.js +228 -57
  16. package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
  17. package/lib/module/core/HttpService.js +15 -79
  18. package/lib/module/core/HttpService.js.map +1 -1
  19. package/lib/module/core/OxyServices.base.js +4 -11
  20. package/lib/module/core/OxyServices.base.js.map +1 -1
  21. package/lib/module/core/services/SessionService.js +159 -0
  22. package/lib/module/core/services/SessionService.js.map +1 -0
  23. package/lib/module/core/services/TokenService.js +203 -0
  24. package/lib/module/core/services/TokenService.js.map +1 -0
  25. package/lib/module/models/interfaces.js +15 -0
  26. package/lib/module/models/interfaces.js.map +1 -1
  27. package/lib/module/ui/context/OxyContext.js +100 -22
  28. package/lib/module/ui/context/OxyContext.js.map +1 -1
  29. package/lib/module/ui/context/hooks/useAuthOperations.js +11 -3
  30. package/lib/module/ui/context/hooks/useAuthOperations.js.map +1 -1
  31. package/lib/module/ui/hooks/useSessionSocket.js +228 -57
  32. package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
  33. package/lib/typescript/core/HttpService.d.ts +1 -1
  34. package/lib/typescript/core/HttpService.d.ts.map +1 -1
  35. package/lib/typescript/core/OxyServices.base.d.ts +6 -0
  36. package/lib/typescript/core/OxyServices.base.d.ts.map +1 -1
  37. package/lib/typescript/core/mixins/OxyServices.analytics.d.ts.map +1 -1
  38. package/lib/typescript/core/mixins/OxyServices.assets.d.ts.map +1 -1
  39. package/lib/typescript/core/mixins/OxyServices.auth.d.ts.map +1 -1
  40. package/lib/typescript/core/mixins/OxyServices.developer.d.ts.map +1 -1
  41. package/lib/typescript/core/mixins/OxyServices.devices.d.ts.map +1 -1
  42. package/lib/typescript/core/mixins/OxyServices.karma.d.ts.map +1 -1
  43. package/lib/typescript/core/mixins/OxyServices.language.d.ts.map +1 -1
  44. package/lib/typescript/core/mixins/OxyServices.location.d.ts.map +1 -1
  45. package/lib/typescript/core/mixins/OxyServices.payment.d.ts.map +1 -1
  46. package/lib/typescript/core/mixins/OxyServices.privacy.d.ts.map +1 -1
  47. package/lib/typescript/core/mixins/OxyServices.security.d.ts.map +1 -1
  48. package/lib/typescript/core/mixins/OxyServices.user.d.ts +1 -3
  49. package/lib/typescript/core/mixins/OxyServices.user.d.ts.map +1 -1
  50. package/lib/typescript/core/mixins/OxyServices.utility.d.ts.map +1 -1
  51. package/lib/typescript/core/mixins/index.d.ts.map +1 -1
  52. package/lib/typescript/core/services/SessionService.d.ts +78 -0
  53. package/lib/typescript/core/services/SessionService.d.ts.map +1 -0
  54. package/lib/typescript/core/services/TokenService.d.ts +72 -0
  55. package/lib/typescript/core/services/TokenService.d.ts.map +1 -0
  56. package/lib/typescript/models/interfaces.d.ts +14 -0
  57. package/lib/typescript/models/interfaces.d.ts.map +1 -1
  58. package/lib/typescript/models/session.d.ts +7 -0
  59. package/lib/typescript/models/session.d.ts.map +1 -1
  60. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  61. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts.map +1 -1
  62. package/lib/typescript/ui/hooks/useSessionSocket.d.ts +8 -1
  63. package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
  64. package/package.json +1 -1
  65. package/src/core/HttpService.ts +15 -95
  66. package/src/core/OxyServices.base.ts +3 -20
  67. package/src/core/services/SessionService.ts +173 -0
  68. package/src/core/services/TokenService.ts +226 -0
  69. package/src/models/interfaces.ts +16 -2
  70. package/src/models/session.ts +8 -1
  71. package/src/ui/context/OxyContext.tsx +105 -23
  72. package/src/ui/context/hooks/useAuthOperations.ts +11 -3
  73. package/src/ui/hooks/useSessionSocket.ts +229 -55
@@ -5,10 +5,11 @@
5
5
  *
6
6
  * Contains core infrastructure, HTTP client, request management, and error handling
7
7
  */
8
- import { jwtDecode } from 'jwt-decode';
8
+
9
9
  import { handleHttpError } from '../utils/errorUtils';
10
10
  import { HttpService } from './HttpService';
11
11
  import { OxyAuthenticationError, OxyAuthenticationTimeoutError } from './OxyServices.errors';
12
+ import { tokenService } from './services/TokenService';
12
13
  /**
13
14
  * Base class for OxyServices with core infrastructure
14
15
  */
@@ -114,18 +115,10 @@ export class OxyServicesBase {
114
115
 
115
116
  /**
116
117
  * Get the current user ID from the access token
118
+ * Returns MongoDB ObjectId (never publicKey)
117
119
  */
118
120
  getCurrentUserId() {
119
- const accessToken = this.httpService.getAccessToken();
120
- if (!accessToken) {
121
- return null;
122
- }
123
- try {
124
- const decoded = jwtDecode(accessToken);
125
- return decoded.userId || decoded.id || null;
126
- } catch (error) {
127
- return null;
128
- }
121
+ return tokenService.getUserIdFromToken();
129
122
  }
130
123
 
131
124
  /**
@@ -1 +1 @@
1
- {"version":3,"names":["jwtDecode","handleHttpError","HttpService","OxyAuthenticationError","OxyAuthenticationTimeoutError","OxyServicesBase","constructor","args","config","Error","cloudURL","httpService","__resetTokensForTests","makeRequest","method","url","data","options","request","undefined","params","getBaseURL","getClient","getMetrics","clearCache","clearCacheEntry","key","getCacheStats","getCloudURL","setTokens","accessToken","refreshToken","clearTokens","getCurrentUserId","getAccessToken","decoded","userId","id","error","hasValidToken","hasAccessToken","waitForAuth","timeoutMs","startTime","performance","now","maxTime","pollInterval","Promise","resolve","setTimeout","Math","min","withAuthRetry","operation","operationName","maxRetries","retryDelay","authTimeoutMs","attempt","authReady","isLastAttempt","errorObj","isAuthError","response","status","code","message","includes","handleError","validate","res","cache","retry","valid","api","trim","err","details","healthCheck","timeout"],"sourceRoot":"../../../src","sources":["core/OxyServices.base.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,SAAS,QAAQ,YAAY;AAEtC,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,WAAW,QAA6B,eAAe;AAChE,SAASC,sBAAsB,EAAEC,6BAA6B,QAAQ,sBAAsB;AAc5F;AACA;AACA;AACA,OAAO,MAAMC,eAAe,CAAC;EAK3BC,WAAWA,CAAC,GAAGC,IAAW,EAAE;IAC1B,MAAMC,MAAM,GAAGD,IAAI,CAAC,CAAC,CAAc;IACnC,IAAI,CAACC,MAAM,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;MACzC,MAAM,IAAIC,KAAK,CAAC,uBAAuB,CAAC;IAC1C;IACA,IAAI,CAACD,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACE,QAAQ,GAAGF,MAAM,CAACE,QAAQ,IAAI,sBAAsB;;IAEzD;IACA,IAAI,CAACC,WAAW,GAAG,IAAIT,WAAW,CAACM,MAAM,CAAC;EAC5C;;EAEA;EACA,OAAOI,qBAAqBA,CAAA,EAAS;IACnCV,WAAW,CAACU,qBAAqB,CAAC,CAAC;EACrC;;EAEA;AACF;AACA;AACA;EACE,MAAaC,WAAWA,CACtBC,MAAmD,EACnDC,GAAW,EACXC,IAAU,EACVC,OAAuB,GAAG,CAAC,CAAC,EAChB;IACZ,OAAO,IAAI,CAACN,WAAW,CAACO,OAAO,CAAI;MACjCJ,MAAM;MACNC,GAAG;MACHC,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,MAAME,WAAWA,CAAA,EAKd;IACD,IAAI;MACF,OAAO,MAAM,IAAI,CAAClE,WAAW,CAAC,KAAK,EAAE,SAAS,EAAEM,SAAS,EAAE;QACzDqD,KAAK,EAAE,KAAK;QACZC,KAAK,EAAE,KAAK;QACZO,OAAO,EAAE;MACX,CAAC,CAAC;IACJ,CAAC,CAAC,OAAO1C,KAAK,EAAE;MACd,MAAM,IAAI,CAAC+B,WAAW,CAAC/B,KAAK,CAAC;IAC/B;EACF;AACF","ignoreList":[]}
1
+ {"version":3,"names":["handleHttpError","HttpService","OxyAuthenticationError","OxyAuthenticationTimeoutError","tokenService","OxyServicesBase","constructor","args","config","Error","cloudURL","httpService","__resetTokensForTests","makeRequest","method","url","data","options","request","undefined","params","getBaseURL","getClient","getMetrics","clearCache","clearCacheEntry","key","getCacheStats","getCloudURL","setTokens","accessToken","refreshToken","clearTokens","getCurrentUserId","getUserIdFromToken","hasValidToken","hasAccessToken","getAccessToken","waitForAuth","timeoutMs","startTime","performance","now","maxTime","pollInterval","Promise","resolve","setTimeout","Math","min","withAuthRetry","operation","operationName","maxRetries","retryDelay","authTimeoutMs","attempt","authReady","error","isLastAttempt","errorObj","isAuthError","response","status","code","message","includes","handleError","validate","res","cache","retry","valid","api","trim","err","details","healthCheck","timeout"],"sourceRoot":"../../../src","sources":["core/OxyServices.base.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;;AAEA,SAASA,eAAe,QAAQ,qBAAqB;AACrD,SAASC,WAAW,QAA6B,eAAe;AAChE,SAASC,sBAAsB,EAAEC,6BAA6B,QAAQ,sBAAsB;AAC5F,SAASC,YAAY,QAAQ,yBAAyB;AAMtD;AACA;AACA;AACA,OAAO,MAAMC,eAAe,CAAC;EAK3BC,WAAWA,CAAC,GAAGC,IAAW,EAAE;IAC1B,MAAMC,MAAM,GAAGD,IAAI,CAAC,CAAC,CAAc;IACnC,IAAI,CAACC,MAAM,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;MACzC,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KAAK,CAACwD,OAAO,CAAkF;IAC/GU,GAAG,CAACX,IAAI,GAAGS,GAAG,CAACT,IAAI;IACnBW,GAAG,CAACZ,MAAM,GAAGU,GAAG,CAACV,MAAM;IACvBY,GAAG,CAACC,OAAO,GAAGH,GAAG,CAACG,OAAO;IACzB,OAAOD,GAAG;EACZ;;EAEA;AACF;AACA;EACE,MAAME,WAAWA,CAAA,EAKd;IACD,IAAI;MACF,OAAO,MAAM,IAAI,CAAChE,WAAW,CAAC,KAAK,EAAE,SAAS,EAAEM,SAAS,EAAE;QACzDmD,KAAK,EAAE,KAAK;QACZC,KAAK,EAAE,KAAK;QACZO,OAAO,EAAE;MACX,CAAC,CAAC;IACJ,CAAC,CAAC,OAAOpB,KAAK,EAAE;MACd,MAAM,IAAI,CAACS,WAAW,CAACT,KAAK,CAAC;IAC/B;EACF;AACF","ignoreList":[]}
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * SessionService - Single Source of Truth for Session Management
5
+ *
6
+ * Handles all session operations: creation, validation, refresh, invalidation.
7
+ * Manages active session state and provides session data to other services.
8
+ *
9
+ * Architecture:
10
+ * - Single source of truth for session operations
11
+ * - Handles both online and offline sessions
12
+ * - Integrates with TokenService for token management
13
+ * - userId is always MongoDB ObjectId, never publicKey
14
+ */
15
+
16
+ import { tokenService } from './TokenService';
17
+ /**
18
+ * SessionService - Singleton pattern for global session management
19
+ */
20
+ class SessionService {
21
+ oxyServices = null;
22
+ activeSession = null;
23
+ sessions = [];
24
+ constructor() {}
25
+ static getInstance() {
26
+ if (!SessionService.instance) {
27
+ SessionService.instance = new SessionService();
28
+ }
29
+ return SessionService.instance;
30
+ }
31
+
32
+ /**
33
+ * Initialize SessionService with OxyServices instance
34
+ */
35
+ initialize(oxyServices) {
36
+ this.oxyServices = oxyServices;
37
+ }
38
+
39
+ /**
40
+ * Get active session
41
+ */
42
+ getActiveSession() {
43
+ return this.activeSession;
44
+ }
45
+
46
+ /**
47
+ * Get all sessions
48
+ */
49
+ getAllSessions() {
50
+ return [...this.sessions];
51
+ }
52
+
53
+ /**
54
+ * Create a new session (sign in)
55
+ * @param publicKey - User's public key for authentication
56
+ * @returns User object and session data
57
+ */
58
+ async createSession(publicKey) {
59
+ if (!this.oxyServices) {
60
+ throw new Error('SessionService not initialized with OxyServices');
61
+ }
62
+
63
+ // This will be implemented by delegating to existing sign-in logic
64
+ // For now, this is a placeholder that shows the interface
65
+ throw new Error('SessionService.createSession not yet implemented - use existing signIn flow');
66
+ }
67
+
68
+ /**
69
+ * Refresh current session
70
+ */
71
+ async refreshSession() {
72
+ if (!this.activeSession) {
73
+ throw new Error('No active session to refresh');
74
+ }
75
+
76
+ // Refresh token first
77
+ await tokenService.refreshTokenIfNeeded();
78
+
79
+ // Then refresh session data from server
80
+ // Implementation will be added
81
+ return this.activeSession;
82
+ }
83
+
84
+ /**
85
+ * Validate current session
86
+ */
87
+ async validateSession() {
88
+ if (!this.activeSession) {
89
+ return false;
90
+ }
91
+
92
+ // Check if session expired
93
+ if (new Date(this.activeSession.expiresAt) < new Date()) {
94
+ return false;
95
+ }
96
+
97
+ // Check if token is valid
98
+ const token = tokenService.getAccessToken();
99
+ if (!token) {
100
+ return false;
101
+ }
102
+
103
+ // Additional validation can be added here
104
+ return true;
105
+ }
106
+
107
+ /**
108
+ * Invalidate current session (sign out)
109
+ */
110
+ async invalidateSession() {
111
+ if (!this.activeSession || !this.oxyServices) {
112
+ return;
113
+ }
114
+ try {
115
+ // Call API to invalidate session on server
116
+ await this.oxyServices.makeRequest('POST', `/api/session/${this.activeSession.sessionId}/logout`, undefined, {
117
+ cache: false
118
+ });
119
+ } catch (error) {
120
+ // Continue with local cleanup even if API call fails
121
+ console.warn('Failed to invalidate session on server:', error);
122
+ }
123
+
124
+ // Clear tokens
125
+ tokenService.clearTokens();
126
+
127
+ // Clear active session
128
+ this.activeSession = null;
129
+ this.sessions = this.sessions.filter(s => s.sessionId !== this.activeSession?.sessionId);
130
+ }
131
+
132
+ /**
133
+ * Set active session (internal use)
134
+ */
135
+ setActiveSession(session) {
136
+ this.activeSession = session;
137
+
138
+ // Update sessions list
139
+ const existingIndex = this.sessions.findIndex(s => s.sessionId === session.sessionId);
140
+ if (existingIndex >= 0) {
141
+ this.sessions[existingIndex] = session;
142
+ } else {
143
+ this.sessions.push(session);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Clear all sessions (logout all)
149
+ */
150
+ clearAllSessions() {
151
+ this.activeSession = null;
152
+ this.sessions = [];
153
+ tokenService.clearTokens();
154
+ }
155
+ }
156
+
157
+ // Export singleton instance
158
+ export const sessionService = SessionService.getInstance();
159
+ //# sourceMappingURL=SessionService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["tokenService","SessionService","oxyServices","activeSession","sessions","constructor","getInstance","instance","initialize","getActiveSession","getAllSessions","createSession","publicKey","Error","refreshSession","refreshTokenIfNeeded","validateSession","Date","expiresAt","token","getAccessToken","invalidateSession","makeRequest","sessionId","undefined","cache","error","console","warn","clearTokens","filter","s","setActiveSession","session","existingIndex","findIndex","push","clearAllSessions","sessionService"],"sourceRoot":"../../../../src","sources":["core/services/SessionService.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA,SAASA,YAAY,QAAQ,gBAAgB;AAc7C;AACA;AACA;AACA,MAAMC,cAAc,CAAC;EAEXC,WAAW,GAAuB,IAAI;EACtCC,aAAa,GAAmB,IAAI;EACpCC,QAAQ,GAAc,EAAE;EAExBC,WAAWA,CAAA,EAAG,CAAC;EAEvB,OAAOC,WAAWA,CAAA,EAAmB;IACnC,IAAI,CAACL,cAAc,CAACM,QAAQ,EAAE;MAC5BN,cAAc,CAACM,QAAQ,GAAG,IAAIN,cAAc,CAAC,CAAC;IAChD;IACA,OAAOA,cAAc,CAACM,QAAQ;EAChC;;EAEA;AACF;AACA;EACEC,UAAUA,CAACN,WAAwB,EAAQ;IACzC,IAAI,CAACA,WAAW,GAAG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th for Token Management
5
+ *
6
+ * Handles all token storage, retrieval, refresh, and validation.
7
+ * Used by HttpService, SocketService, and other services that need tokens.
8
+ *
9
+ * Architecture:
10
+ * - Single storage location (no duplication)
11
+ * - Automatic token refresh when expiring soon
12
+ * - Type-safe token payload handling
13
+ * - userId is always MongoDB ObjectId, never publicKey
14
+ */
15
+
16
+ import { jwtDecode } from 'jwt-decode';
17
+
18
+ /**
19
+ * AccessTokenPayload - Matches the token payload structure from API
20
+ * userId is always MongoDB ObjectId (24 hex characters), never publicKey
21
+ */
22
+
23
+ /**
24
+ * TokenService - Singleton pattern for global token management
25
+ */
26
+ class TokenService {
27
+ tokenStore = {
28
+ accessToken: null,
29
+ refreshToken: null
30
+ };
31
+ refreshPromise = null;
32
+ baseURL = null;
33
+ constructor() {}
34
+ static getInstance() {
35
+ if (!TokenService.instance) {
36
+ TokenService.instance = new TokenService();
37
+ }
38
+ return TokenService.instance;
39
+ }
40
+
41
+ /**
42
+ * Initialize TokenService with base URL for refresh requests
43
+ */
44
+ initialize(baseURL) {
45
+ this.baseURL = baseURL;
46
+ }
47
+
48
+ /**
49
+ * Get current access token
50
+ */
51
+ getAccessToken() {
52
+ return this.tokenStore.accessToken;
53
+ }
54
+
55
+ /**
56
+ * Get current refresh token
57
+ */
58
+ getRefreshToken() {
59
+ return this.tokenStore.refreshToken;
60
+ }
61
+
62
+ /**
63
+ * Set tokens (called after login or token refresh)
64
+ */
65
+ setTokens(accessToken, refreshToken = '') {
66
+ this.tokenStore.accessToken = accessToken;
67
+ this.tokenStore.refreshToken = refreshToken || this.tokenStore.refreshToken;
68
+ }
69
+
70
+ /**
71
+ * Clear all tokens (called on logout)
72
+ */
73
+ clearTokens() {
74
+ this.tokenStore.accessToken = null;
75
+ this.tokenStore.refreshToken = null;
76
+ this.refreshPromise = null;
77
+ }
78
+
79
+ /**
80
+ * Check if access token exists
81
+ */
82
+ hasAccessToken() {
83
+ return !!this.tokenStore.accessToken;
84
+ }
85
+
86
+ /**
87
+ * Check if token is expiring soon (within 60 seconds)
88
+ */
89
+ isTokenExpiringSoon() {
90
+ const token = this.tokenStore.accessToken;
91
+ if (!token) return false;
92
+ try {
93
+ const decoded = jwtDecode(token);
94
+ if (!decoded.exp) return false;
95
+ const currentTime = Math.floor(Date.now() / 1000);
96
+ return decoded.exp - currentTime < 60; // Expiring within 60 seconds
97
+ } catch {
98
+ return false;
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Get userId from current access token
104
+ * Returns MongoDB ObjectId (never publicKey)
105
+ */
106
+ getUserIdFromToken() {
107
+ const token = this.tokenStore.accessToken;
108
+ if (!token) return null;
109
+ try {
110
+ const decoded = jwtDecode(token);
111
+ return decoded.userId || null;
112
+ } catch {
113
+ return null;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Refresh access token if expiring soon
119
+ * Returns promise that resolves when token is refreshed (or already valid)
120
+ */
121
+ async refreshTokenIfNeeded() {
122
+ // If already refreshing, wait for that promise
123
+ if (this.refreshPromise) {
124
+ return this.refreshPromise;
125
+ }
126
+
127
+ // If token not expiring soon, no refresh needed
128
+ if (!this.isTokenExpiringSoon()) {
129
+ return;
130
+ }
131
+
132
+ // Start refresh
133
+ this.refreshPromise = this._performRefresh();
134
+ try {
135
+ await this.refreshPromise;
136
+ } finally {
137
+ this.refreshPromise = null;
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Perform token refresh
143
+ */
144
+ async _performRefresh() {
145
+ const token = this.tokenStore.accessToken;
146
+ if (!token) {
147
+ throw new Error('No access token to refresh');
148
+ }
149
+ try {
150
+ const decoded = jwtDecode(token);
151
+ if (!decoded.sessionId) {
152
+ throw new Error('Token missing sessionId');
153
+ }
154
+ if (!this.baseURL) {
155
+ throw new Error('TokenService not initialized with baseURL');
156
+ }
157
+ const refreshUrl = `${this.baseURL}/api/session/token/${decoded.sessionId}`;
158
+ const response = await fetch(refreshUrl, {
159
+ method: 'GET',
160
+ headers: {
161
+ 'Accept': 'application/json'
162
+ },
163
+ signal: AbortSignal.timeout(5000)
164
+ });
165
+ if (!response.ok) {
166
+ throw new Error(`Token refresh failed: ${response.status}`);
167
+ }
168
+ const {
169
+ accessToken: newToken
170
+ } = await response.json();
171
+ if (!newToken) {
172
+ throw new Error('No access token in refresh response');
173
+ }
174
+
175
+ // Validate new token has correct userId format (ObjectId)
176
+ const newDecoded = jwtDecode(newToken);
177
+ if (newDecoded.userId && !/^[0-9a-fA-F]{24}$/.test(newDecoded.userId)) {
178
+ throw new Error(`Invalid userId format in refreshed token: ${newDecoded.userId.substring(0, 20)}...`);
179
+ }
180
+ this.setTokens(newToken);
181
+ } catch (error) {
182
+ // Clear tokens on refresh failure (likely expired or invalid)
183
+ this.clearTokens();
184
+ throw error;
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Get authorization header with automatic refresh
190
+ */
191
+ async getAuthHeader() {
192
+ // Refresh if needed
193
+ await this.refreshTokenIfNeeded().catch(() => {
194
+ // Ignore refresh errors, will use current token or return null
195
+ });
196
+ const token = this.tokenStore.accessToken;
197
+ return token ? `Bearer ${token}` : null;
198
+ }
199
+ }
200
+
201
+ // Export singleton instance
202
+ export const tokenService = TokenService.getInstance();
203
+ //# sourceMappingURL=TokenService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["jwtDecode","TokenService","tokenStore","accessToken","refreshToken","refreshPromise","baseURL","constructor","getInstance","instance","initialize","getAccessToken","getRefreshToken","setTokens","clearTokens","hasAccessToken","isTokenExpiringSoon","token","decoded","exp","currentTime","Math","floor","Date","now","getUserIdFromToken","userId","refreshTokenIfNeeded","_performRefresh","Error","sessionId","refreshUrl","response","fetch","method","headers","signal","AbortSignal","timeout","ok","status","newToken","json","newDecoded","test","substring","error","getAuthHeader","catch","tokenService"],"sourceRoot":"../../../../src","sources":["core/services/TokenService.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,SAAS,QAAQ,YAAY;;AAEtC;AACA;AACA;AACA;;AA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C4B,QAAQ,CAAC;IAC1B,CAAC,CAAC,OAAOK,KAAK,EAAE;MACd;MACA,IAAI,CAAChC,WAAW,CAAC,CAAC;MAClB,MAAMgC,KAAK;IACb;EACF;;EAEA;AACF;AACA;EACE,MAAMC,aAAaA,CAAA,EAA2B;IAC5C;IACA,MAAM,IAAI,CAACpB,oBAAoB,CAAC,CAAC,CAACqB,KAAK,CAAC,MAAM;MAC5C;IAAA,CACD,CAAC;IAEF,MAAM/B,KAAK,GAAG,IAAI,CAACf,UAAU,CAACC,WAAW;IACzC,OAAOc,KAAK,GAAG,UAAUA,KAAK,EAAE,GAAG,IAAI;EACzC;AACF;;AAEA;AACA,OAAO,MAAMgC,YAAY,GAAGhD,YAAY,CAACO,WAAW,CAAC,CAAC","ignoreList":[]}
@@ -1,5 +1,20 @@
1
1
  "use strict";
2
2
 
3
+ /**
4
+ * User Model
5
+ *
6
+ * IMPORTANT:
7
+ * - id: MongoDB ObjectId (24 hex characters) - PRIMARY IDENTIFIER for all internal operations
8
+ * - publicKey: Cryptographic public key (130 hex characters) - LOOKUP KEY for authentication and identity operations
9
+ *
10
+ * Never use publicKey as an ID. Always use id (ObjectId) for:
11
+ * - Database queries
12
+ * - Session userId
13
+ * - Token userId
14
+ * - Socket room names
15
+ * - API route parameters (unless explicitly doing publicKey lookup)
16
+ */
17
+
3
18
  /**
4
19
  * File management interfaces
5
20
  */
@@ -1 +1 @@
1
- {"version":3,"names":["SECURITY_EVENT_SEVERITY_MAP"],"sourceRoot":"../../../src","sources":["models/interfaces.ts"],"mappings":";;AAyNA;AACA;AACA;;AAmDA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAuGA;AACA;AACA;;AAqBA;AACA;AACA;;AAcA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,OAAO,MAAMA,2BAA6E,GAAG;EAC3F,SAAS,EAAE,KAAK;EAChB,UAAU,EAAE,KAAK;EACjB,iBAAiB,EAAE,KAAK;EACxB,eAAe,EAAE,QAAQ;EACzB,cAAc,EAAE,QAAQ;EACxB,gBAAgB,EAAE,QAAQ;EAC1B,2BAA2B,EAAE,QAAQ;EACrC,kBAAkB,EAAE,MAAM;EAC1B,sBAAsB,EAAE,MAAM;EAC9B,gBAAgB,EAAE,MAAM;EACxB,qBAAqB,EAAE;AACzB,CAAC;;AAED;AACA;AACA;;AAeA;AACA;AACA;;AAkBA","ignoreList":[]}
1
+ {"version":3,"names":["SECURITY_EVENT_SEVERITY_MAP"],"sourceRoot":"../../../src","sources":["models/interfaces.ts"],"mappings":";;AAuBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmMA;AACA;AACA;;AAmDA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAuGA;AACA;AACA;;AAqBA;AACA;AACA;;AAcA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,OAAO,MAAMA,2BAA6E,GAAG;EAC3F,SAAS,EAAE,KAAK;EAChB,UAAU,EAAE,KAAK;EACjB,iBAAiB,EAAE,KAAK;EACxB,eAAe,EAAE,QAAQ;EACzB,cAAc,EAAE,QAAQ;EACxB,gBAAgB,EAAE,QAAQ;EAC1B,2BAA2B,EAAE,QAAQ;EACrC,kBAAkB,EAAE,MAAM;EAC1B,sBAAsB,EAAE,MAAM;EAC9B,gBAAgB,EAAE,MAAM;EACxB,qBAAqB,EAAE;AACzB,CAAC;;AAED;AACA;AACA;;AAeA;AACA;AACA;;AAkBA","ignoreList":[]}
@@ -507,7 +507,11 @@ export const OxyProvider = ({
507
507
  }, [restoreSessionsFromStorage, storage]);
508
508
  const activeSession = activeSessionId ? sessions.find(session => session.sessionId === activeSessionId) : undefined;
509
509
  const currentDeviceId = activeSession?.deviceId ?? null;
510
- const userId = user?.id;
510
+
511
+ // Get userId from JWT token (MongoDB ObjectId) for socket room matching
512
+ // user.id is set to publicKey for compatibility, but socket rooms use MongoDB ObjectId
513
+ // The JWT token's userId field contains the MongoDB ObjectId
514
+ const userId = oxyServices.getCurrentUserId() || user?.id;
511
515
 
512
516
  // Store transfer codes in memory for verification
513
517
  // Map: transferId -> { code, sourceDeviceId, publicKey, timestamp }
@@ -569,30 +573,47 @@ export const OxyProvider = ({
569
573
  logout().catch(remoteError => logger('Failed to process remote sign out', remoteError));
570
574
  }, [logger, logout]);
571
575
  const handleIdentityTransferComplete = useCallback(async data => {
576
+ if (__DEV__) {
577
+ console.log('[OxyContext] handleIdentityTransferComplete called', {
578
+ transferId: data.transferId,
579
+ sourceDeviceId: data.sourceDeviceId,
580
+ currentDeviceId,
581
+ hasActiveSession: activeSessionId !== null
582
+ });
583
+ }
572
584
  try {
585
+ logger('Received identity transfer complete notification', {
586
+ transferId: data.transferId,
587
+ sourceDeviceId: data.sourceDeviceId,
588
+ currentDeviceId,
589
+ hasActiveSession: activeSessionId !== null,
590
+ publicKey: data.publicKey.substring(0, 16) + '...'
591
+ });
592
+
573
593
  // Get stored transfer code for verification
574
594
  const storedTransfer = getTransferCode(data.transferId);
575
595
  if (!storedTransfer) {
596
+ if (__DEV__) {
597
+ console.warn('[OxyContext] Transfer code not found for transferId', {
598
+ transferId: data.transferId
599
+ });
600
+ }
576
601
  logger('Transfer code not found for transferId', {
577
602
  transferId: data.transferId
578
603
  });
579
604
  toast.error('Transfer verification failed: Code not found. Identity will not be deleted.');
580
605
  return;
581
606
  }
582
-
583
- // Verify deviceId matches (or session exists as fallback)
584
- const deviceIdMatches = data.sourceDeviceId && (data.sourceDeviceId === currentDeviceId || currentDeviceId === null && activeSessionId !== null);
585
- if (!deviceIdMatches) {
586
- logger('Device ID mismatch for transfer', {
607
+ if (__DEV__) {
608
+ console.log('[OxyContext] Found stored transfer code', {
587
609
  transferId: data.transferId,
588
- receivedDeviceId: data.sourceDeviceId,
610
+ storedSourceDeviceId: storedTransfer.sourceDeviceId,
611
+ receivedSourceDeviceId: data.sourceDeviceId,
589
612
  currentDeviceId
590
613
  });
591
- toast.error('Transfer verification failed: Device mismatch. Identity will not be deleted.');
592
- return;
593
614
  }
594
615
 
595
- // Verify publicKey matches
616
+ // Verify publicKey matches first (most important check)
596
617
  const publicKeyMatches = data.publicKey === storedTransfer.publicKey;
597
618
  if (!publicKeyMatches) {
598
619
  logger('Public key mismatch for transfer', {
@@ -604,21 +625,53 @@ export const OxyProvider = ({
604
625
  return;
605
626
  }
606
627
 
628
+ // Verify deviceId matches - very lenient since publicKey is the critical check
629
+ // If publicKey matches, we allow deletion even if deviceId doesn't match exactly
630
+ // This handles cases where deviceId might not be available or slightly different
631
+ const deviceIdMatches =
632
+ // Exact match
633
+ data.sourceDeviceId && data.sourceDeviceId === currentDeviceId ||
634
+ // Stored sourceDeviceId matches current deviceId
635
+ storedTransfer.sourceDeviceId && storedTransfer.sourceDeviceId === currentDeviceId;
636
+
637
+ // If publicKey matches, we're very lenient with deviceId - only warn but don't block
638
+ if (!deviceIdMatches && publicKeyMatches) {
639
+ logger('Device ID mismatch for transfer, but publicKey matches - proceeding with deletion', {
640
+ transferId: data.transferId,
641
+ receivedDeviceId: data.sourceDeviceId,
642
+ storedDeviceId: storedTransfer.sourceDeviceId,
643
+ currentDeviceId,
644
+ hasActiveSession: activeSessionId !== null
645
+ });
646
+ // Proceed with deletion - publicKey match is the critical verification
647
+ } else if (!deviceIdMatches && !publicKeyMatches) {
648
+ // Both don't match - this is suspicious, block deletion
649
+ logger('Device ID and publicKey mismatch for transfer', {
650
+ transferId: data.transferId,
651
+ receivedDeviceId: data.sourceDeviceId,
652
+ currentDeviceId
653
+ });
654
+ toast.error('Transfer verification failed: Device and key mismatch. Identity will not be deleted.');
655
+ return;
656
+ }
657
+
607
658
  // Verify transfer code matches (if provided)
659
+ // Transfer code is optional - if not provided, we still proceed if publicKey matches
608
660
  if (data.transferCode) {
609
661
  const codeMatches = data.transferCode.toUpperCase() === storedTransfer.code.toUpperCase();
610
662
  if (!codeMatches) {
611
- logger('Transfer code mismatch', {
663
+ logger('Transfer code mismatch, but publicKey matches - proceeding with deletion', {
612
664
  transferId: data.transferId,
613
- receivedCode: data.transferCode
665
+ receivedCode: data.transferCode,
666
+ storedCode: storedTransfer.code.substring(0, 2) + '****'
614
667
  });
615
- toast.error('Transfer verification failed: Code mismatch. Identity will not be deleted.');
616
- return;
668
+ // Don't block - publicKey match is sufficient, code mismatch might be due to user error
669
+ // Log warning but proceed
617
670
  }
618
671
  } else {
619
- // If transfer code is not provided, log warning but still proceed (for backward compatibility)
672
+ // If transfer code is not provided, log info but proceed
620
673
  if (__DEV__) {
621
- logger('Transfer code not provided in completion notification', {
674
+ logger('Transfer code not provided in completion notification, but publicKey matches - proceeding', {
622
675
  transferId: data.transferId
623
676
  });
624
677
  }
@@ -630,7 +683,8 @@ export const OxyProvider = ({
630
683
  if (transferAge > tenMinutes) {
631
684
  logger('Transfer confirmation received too late', {
632
685
  transferId: data.transferId,
633
- age: transferAge
686
+ age: transferAge,
687
+ ageMinutes: Math.round(transferAge / 60000)
634
688
  });
635
689
  toast.error('Transfer verification failed: Confirmation received too late. Identity will not be deleted.');
636
690
  clearTransferCode(data.transferId);
@@ -638,15 +692,37 @@ export const OxyProvider = ({
638
692
  }
639
693
 
640
694
  // All verifications passed - automatically delete identity
695
+ if (__DEV__) {
696
+ console.log('[OxyContext] All verifications passed, deleting identity', {
697
+ transferId: data.transferId,
698
+ sourceDeviceId: data.sourceDeviceId,
699
+ currentDeviceId
700
+ });
701
+ }
641
702
  logger('All transfer verifications passed, deleting identity from source device', {
642
703
  transferId: data.transferId,
643
- sourceDeviceId: data.sourceDeviceId
704
+ sourceDeviceId: data.sourceDeviceId,
705
+ publicKey: data.publicKey.substring(0, 16) + '...'
644
706
  });
645
- await deleteIdentityAndClearAccount(false, false, true);
707
+ try {
708
+ await deleteIdentityAndClearAccount(false, false, true);
709
+ if (__DEV__) {
710
+ console.log('[OxyContext] Identity deleted successfully');
711
+ }
646
712
 
647
- // Clear the transfer code after successful deletion
648
- clearTransferCode(data.transferId);
649
- toast.success('Identity successfully transferred and removed from this device');
713
+ // Clear the transfer code after successful deletion
714
+ clearTransferCode(data.transferId);
715
+ logger('Identity successfully deleted and transfer code cleared', {
716
+ transferId: data.transferId
717
+ });
718
+ toast.success('Identity successfully transferred and removed from this device');
719
+ } catch (deleteError) {
720
+ if (__DEV__) {
721
+ console.error('[OxyContext] Error deleting identity', deleteError);
722
+ }
723
+ logger('Error during identity deletion', deleteError);
724
+ throw deleteError; // Re-throw to be caught by outer catch
725
+ }
650
726
  } catch (error) {
651
727
  logger('Failed to delete identity after transfer', error);
652
728
  toast.error(error?.message || 'Failed to remove identity from this device. Please try again manually from Security Settings.');
@@ -660,6 +736,8 @@ export const OxyProvider = ({
660
736
  logout,
661
737
  clearSessionState,
662
738
  baseURL: oxyServices.getBaseURL(),
739
+ getAccessToken: () => oxyServices.getAccessToken(),
740
+ getTransferCode: getTransferCode,
663
741
  onRemoteSignOut: handleRemoteSignOut,
664
742
  onSessionRemoved: handleSessionRemoved,
665
743
  onIdentityTransferComplete: handleIdentityTransferComplete