@oxyhq/services 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +144 -250
- package/dist/backend/auth.d.ts +37 -0
- package/dist/backend/index.d.ts +12 -0
- package/dist/backend/karma.d.ts +45 -0
- package/dist/backend/users.d.ts +39 -0
- package/dist/backend/wallet.d.ts +29 -0
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +9 -0
- package/dist/frontend/context/OxyContext.d.ts +20 -0
- package/dist/frontend/hooks/useAuth.d.ts +19 -0
- package/dist/frontend/hooks/useKarma.d.ts +27 -0
- package/dist/frontend/hooks/useUser.d.ts +13 -0
- package/dist/frontend/hooks/useWallet.d.ts +14 -0
- package/dist/frontend/index.d.ts +6 -0
- package/dist/index.d.ts +3 -28
- package/dist/shared/api-client.d.ts +15 -0
- package/dist/shared/types.d.ts +135 -0
- package/package.json +45 -71
- package/dist/components/AuthBottomSheet/types.d.ts +0 -24
- package/dist/components/AuthBottomSheet/types.js +0 -2
- package/dist/components/FileSelectorModal/types.d.ts +0 -35
- package/dist/components/FileSelectorModal/types.js +0 -2
- package/dist/components/SessionProvider.d.ts +0 -26
- package/dist/components/SessionProvider.js +0 -367
- package/dist/config.d.ts +0 -169
- package/dist/config.js +0 -115
- package/dist/constants.d.ts +0 -54
- package/dist/constants.js +0 -61
- package/dist/hooks/index.d.ts +0 -11
- package/dist/hooks/index.js +0 -23
- package/dist/hooks/useAuth.d.ts +0 -6
- package/dist/hooks/useAuth.js +0 -17
- package/dist/hooks/useFiles.d.ts +0 -10
- package/dist/hooks/useFiles.js +0 -329
- package/dist/hooks/useProfile.d.ts +0 -13
- package/dist/hooks/useProfile.js +0 -154
- package/dist/hooks/useSession.d.ts +0 -2
- package/dist/hooks/useSession.js +0 -12
- package/dist/hooks/useSubscription.d.ts +0 -15
- package/dist/hooks/useSubscription.js +0 -85
- package/dist/index.js +0 -82
- package/dist/package.json +0 -71
- package/dist/reducers/index.d.ts +0 -8
- package/dist/reducers/index.js +0 -29
- package/dist/reducers/profileReducer.d.ts +0 -55
- package/dist/reducers/profileReducer.js +0 -136
- package/dist/services/OxyClient.d.ts +0 -84
- package/dist/services/OxyClient.js +0 -309
- package/dist/services/api.service.d.ts +0 -61
- package/dist/services/api.service.js +0 -289
- package/dist/services/auth.service.d.ts +0 -87
- package/dist/services/auth.service.js +0 -266
- package/dist/services/index.d.ts +0 -30
- package/dist/services/index.js +0 -88
- package/dist/services/payment.service.d.ts +0 -20
- package/dist/services/payment.service.js +0 -56
- package/dist/services/privacy.service.d.ts +0 -16
- package/dist/services/privacy.service.js +0 -69
- package/dist/services/profile.service.d.ts +0 -20
- package/dist/services/profile.service.js +0 -158
- package/dist/services/subscription.service.d.ts +0 -7
- package/dist/services/subscription.service.js +0 -46
- package/dist/services/user.service.d.ts +0 -25
- package/dist/services/user.service.js +0 -109
- package/dist/styles/colors.d.ts +0 -24
- package/dist/styles/colors.js +0 -31
- package/dist/styles/shared.d.ts +0 -168
- package/dist/styles/shared.js +0 -177
- package/dist/types/index.d.ts +0 -136
- package/dist/types/index.js +0 -7
- package/dist/utils/api.d.ts +0 -20
- package/dist/utils/api.js +0 -546
- package/dist/utils/authEvents.d.ts +0 -44
- package/dist/utils/authEvents.js +0 -73
- package/dist/utils/errorHandler.d.ts +0 -68
- package/dist/utils/errorHandler.js +0 -166
- package/dist/utils/index.d.ts +0 -12
- package/dist/utils/index.js +0 -41
- package/dist/utils/logger.d.ts +0 -100
- package/dist/utils/logger.js +0 -298
- package/dist/utils/socket.d.ts +0 -6
- package/dist/utils/socket.js +0 -179
- package/dist/utils/socketConfig.d.ts +0 -32
- package/dist/utils/socketConfig.js +0 -67
- package/dist/utils/storage.d.ts +0 -54
- package/dist/utils/storage.js +0 -253
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.apiService = void 0;
|
|
16
|
-
const axios_1 = __importDefault(require("axios"));
|
|
17
|
-
const storage_1 = require("../utils/storage");
|
|
18
|
-
const config_1 = require("../config");
|
|
19
|
-
const constants_1 = require("../constants");
|
|
20
|
-
// Import authEvents instead of showAuthBottomSheet
|
|
21
|
-
const authEvents_1 = require("../utils/authEvents");
|
|
22
|
-
class ApiService {
|
|
23
|
-
constructor() {
|
|
24
|
-
this.isRefreshing = false;
|
|
25
|
-
this.failedQueue = [];
|
|
26
|
-
this.cache = new Map();
|
|
27
|
-
this.abortControllers = new Map();
|
|
28
|
-
this.api = axios_1.default.create({
|
|
29
|
-
baseURL: config_1.OXY_API_CONFIG.BASE_URL,
|
|
30
|
-
timeout: config_1.OXY_API_CONFIG.TIMEOUT,
|
|
31
|
-
headers: {
|
|
32
|
-
'Content-Type': 'application/json'
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
this.setupInterceptors();
|
|
36
|
-
}
|
|
37
|
-
setupInterceptors() {
|
|
38
|
-
this.api.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
try {
|
|
40
|
-
const accessToken = yield (0, storage_1.getSecureData)('accessToken');
|
|
41
|
-
if (accessToken && config.headers) {
|
|
42
|
-
config.headers.Authorization = `Bearer ${accessToken}`;
|
|
43
|
-
}
|
|
44
|
-
// Add abort controller to each request
|
|
45
|
-
if (config.method && ['get', 'post', 'put', 'delete', 'patch'].includes(config.method)) {
|
|
46
|
-
const controller = new AbortController();
|
|
47
|
-
const requestId = this.getRequestId(config);
|
|
48
|
-
this.abortControllers.set(requestId, controller);
|
|
49
|
-
config.signal = controller.signal;
|
|
50
|
-
}
|
|
51
|
-
return config;
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
console.error('Error in request interceptor:', error);
|
|
55
|
-
return config;
|
|
56
|
-
}
|
|
57
|
-
}));
|
|
58
|
-
this.api.interceptors.response.use((response) => response, (error) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
var _a;
|
|
60
|
-
const originalRequest = error.config;
|
|
61
|
-
// Clean up abort controller after request completes
|
|
62
|
-
if (originalRequest) {
|
|
63
|
-
const requestId = this.getRequestId(originalRequest);
|
|
64
|
-
this.abortControllers.delete(requestId);
|
|
65
|
-
}
|
|
66
|
-
if (!originalRequest || ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== 401 || originalRequest._retry) {
|
|
67
|
-
return Promise.reject(error);
|
|
68
|
-
}
|
|
69
|
-
if (this.isRefreshing) {
|
|
70
|
-
return new Promise((resolve, reject) => {
|
|
71
|
-
this.failedQueue.push({ resolve, reject, config: originalRequest });
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
originalRequest._retry = true;
|
|
75
|
-
this.isRefreshing = true;
|
|
76
|
-
try {
|
|
77
|
-
const refreshToken = yield (0, storage_1.getSecureData)('refreshToken');
|
|
78
|
-
if (!refreshToken) {
|
|
79
|
-
throw new Error('No refresh token available');
|
|
80
|
-
}
|
|
81
|
-
const response = yield axios_1.default.post(`${config_1.OXY_API_CONFIG.BASE_URL}/auth/refresh`, { refreshToken });
|
|
82
|
-
const { accessToken, refreshToken: newRefreshToken } = response.data;
|
|
83
|
-
if (!accessToken) {
|
|
84
|
-
throw new Error('Invalid refresh response');
|
|
85
|
-
}
|
|
86
|
-
// Store new tokens
|
|
87
|
-
yield Promise.all([
|
|
88
|
-
(0, storage_1.storeSecureData)('accessToken', accessToken),
|
|
89
|
-
newRefreshToken ? (0, storage_1.storeSecureData)('refreshToken', newRefreshToken) : Promise.resolve()
|
|
90
|
-
]);
|
|
91
|
-
// Update authorization header
|
|
92
|
-
this.api.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
|
93
|
-
if (originalRequest.headers) {
|
|
94
|
-
originalRequest.headers['Authorization'] = `Bearer ${accessToken}`;
|
|
95
|
-
}
|
|
96
|
-
// Process queued requests
|
|
97
|
-
this.processQueue(null, accessToken);
|
|
98
|
-
// Retry original request
|
|
99
|
-
return this.api(originalRequest);
|
|
100
|
-
}
|
|
101
|
-
catch (refreshError) {
|
|
102
|
-
// Process queued requests with error
|
|
103
|
-
this.processQueue(refreshError);
|
|
104
|
-
// Clear tokens and show auth screen
|
|
105
|
-
yield Promise.all([
|
|
106
|
-
(0, storage_1.storeSecureData)('accessToken', null),
|
|
107
|
-
(0, storage_1.storeSecureData)('refreshToken', null)
|
|
108
|
-
]);
|
|
109
|
-
// Use authEvents instead of direct function call
|
|
110
|
-
authEvents_1.authEvents.requireAuth();
|
|
111
|
-
return Promise.reject(refreshError);
|
|
112
|
-
}
|
|
113
|
-
finally {
|
|
114
|
-
this.isRefreshing = false;
|
|
115
|
-
}
|
|
116
|
-
}));
|
|
117
|
-
}
|
|
118
|
-
processQueue(error, accessToken) {
|
|
119
|
-
this.failedQueue.forEach(({ resolve, reject, config }) => {
|
|
120
|
-
if (error) {
|
|
121
|
-
reject(error);
|
|
122
|
-
}
|
|
123
|
-
else if (accessToken && config.headers) {
|
|
124
|
-
config.headers['Authorization'] = `Bearer ${accessToken}`;
|
|
125
|
-
resolve(this.api(config));
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
this.failedQueue = [];
|
|
129
|
-
}
|
|
130
|
-
getRequestId(config) {
|
|
131
|
-
const { method, url, params, data } = config;
|
|
132
|
-
return `${method}:${url}:${JSON.stringify(params || {})}:${JSON.stringify(data || {})}`;
|
|
133
|
-
}
|
|
134
|
-
getCacheKey(url, params) {
|
|
135
|
-
return `${url}:${JSON.stringify(params || {})}`;
|
|
136
|
-
}
|
|
137
|
-
isCacheValid(cacheItem) {
|
|
138
|
-
return Date.now() < cacheItem.expiresAt;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Cancel all pending requests
|
|
142
|
-
*/
|
|
143
|
-
cancelAllRequests() {
|
|
144
|
-
this.abortControllers.forEach(controller => {
|
|
145
|
-
controller.abort();
|
|
146
|
-
});
|
|
147
|
-
this.abortControllers.clear();
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Cancel a specific request
|
|
151
|
-
*/
|
|
152
|
-
cancelRequest(requestId) {
|
|
153
|
-
const controller = this.abortControllers.get(requestId);
|
|
154
|
-
if (controller) {
|
|
155
|
-
controller.abort();
|
|
156
|
-
this.abortControllers.delete(requestId);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Clear the entire cache
|
|
161
|
-
*/
|
|
162
|
-
clearCache() {
|
|
163
|
-
this.cache.clear();
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Clear a specific cache entry
|
|
167
|
-
*/
|
|
168
|
-
clearCacheEntry(url, params) {
|
|
169
|
-
const cacheKey = this.getCacheKey(url, params);
|
|
170
|
-
this.cache.delete(cacheKey);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Make a GET request with optional caching
|
|
174
|
-
*/
|
|
175
|
-
get(url, config) {
|
|
176
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
const { params, useCache = false, cacheTTL = config_1.OXY_CACHE_CONFIG.DEFAULT_TTL } = config || {};
|
|
178
|
-
if (useCache) {
|
|
179
|
-
const cacheKey = this.getCacheKey(url, params);
|
|
180
|
-
const cachedItem = this.cache.get(cacheKey);
|
|
181
|
-
if (cachedItem && this.isCacheValid(cachedItem)) {
|
|
182
|
-
return {
|
|
183
|
-
data: cachedItem.data,
|
|
184
|
-
status: 200,
|
|
185
|
-
statusText: 'OK (cached)',
|
|
186
|
-
headers: {},
|
|
187
|
-
config: {}
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
try {
|
|
192
|
-
const response = yield this.api.get(url, Object.assign({}, config));
|
|
193
|
-
if (useCache) {
|
|
194
|
-
const cacheKey = this.getCacheKey(url, params);
|
|
195
|
-
this.cache.set(cacheKey, {
|
|
196
|
-
data: response.data,
|
|
197
|
-
timestamp: Date.now(),
|
|
198
|
-
expiresAt: Date.now() + cacheTTL
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
return response;
|
|
202
|
-
}
|
|
203
|
-
catch (error) {
|
|
204
|
-
this.handleRequestError(error);
|
|
205
|
-
throw error;
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Make a POST request
|
|
211
|
-
*/
|
|
212
|
-
post(url, data, config) {
|
|
213
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
214
|
-
try {
|
|
215
|
-
return yield this.api.post(url, data, config);
|
|
216
|
-
}
|
|
217
|
-
catch (error) {
|
|
218
|
-
this.handleRequestError(error);
|
|
219
|
-
throw error;
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Make a PUT request
|
|
225
|
-
*/
|
|
226
|
-
put(url, data, config) {
|
|
227
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
228
|
-
try {
|
|
229
|
-
return yield this.api.put(url, data, config);
|
|
230
|
-
}
|
|
231
|
-
catch (error) {
|
|
232
|
-
this.handleRequestError(error);
|
|
233
|
-
throw error;
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Make a DELETE request
|
|
239
|
-
*/
|
|
240
|
-
delete(url, config) {
|
|
241
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
242
|
-
try {
|
|
243
|
-
return yield this.api.delete(url, config);
|
|
244
|
-
}
|
|
245
|
-
catch (error) {
|
|
246
|
-
this.handleRequestError(error);
|
|
247
|
-
throw error;
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Make a PATCH request
|
|
253
|
-
*/
|
|
254
|
-
patch(url, data, config) {
|
|
255
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
256
|
-
try {
|
|
257
|
-
return yield this.api.patch(url, data, config);
|
|
258
|
-
}
|
|
259
|
-
catch (error) {
|
|
260
|
-
this.handleRequestError(error);
|
|
261
|
-
throw error;
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Handle common request errors
|
|
267
|
-
*/
|
|
268
|
-
handleRequestError(error) {
|
|
269
|
-
if (axios_1.default.isAxiosError(error)) {
|
|
270
|
-
const axiosError = error;
|
|
271
|
-
if (axiosError.code === 'ECONNABORTED') {
|
|
272
|
-
console.error('Request timeout:', axiosError.message);
|
|
273
|
-
}
|
|
274
|
-
else if (axiosError.response) {
|
|
275
|
-
console.error(`API Error ${axiosError.response.status}:`, axiosError.response.data || constants_1.ERROR_MESSAGES.DEFAULT);
|
|
276
|
-
}
|
|
277
|
-
else if (axiosError.request) {
|
|
278
|
-
console.error('No response received:', axiosError.message);
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
console.error('Request error:', axiosError.message);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
console.error('Unexpected error:', error);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
exports.apiService = new ApiService();
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Authentication Service
|
|
3
|
-
*
|
|
4
|
-
* Manages user authentication, session validation, and token management.
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* User profile information
|
|
8
|
-
*/
|
|
9
|
-
export interface User {
|
|
10
|
-
id: string;
|
|
11
|
-
username: string;
|
|
12
|
-
email: string;
|
|
13
|
-
name?: {
|
|
14
|
-
first?: string;
|
|
15
|
-
last?: string;
|
|
16
|
-
};
|
|
17
|
-
avatar?: string;
|
|
18
|
-
avatarSource?: {
|
|
19
|
-
uri: string;
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Response from registration endpoint
|
|
24
|
-
*/
|
|
25
|
-
interface RegisterResponse {
|
|
26
|
-
success: boolean;
|
|
27
|
-
message: string;
|
|
28
|
-
accessToken: string;
|
|
29
|
-
refreshToken: string;
|
|
30
|
-
user: User;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Authentication service class
|
|
34
|
-
*/
|
|
35
|
-
declare class AuthService {
|
|
36
|
-
/**
|
|
37
|
-
* Register a new user
|
|
38
|
-
*/
|
|
39
|
-
register(user: {
|
|
40
|
-
username: string;
|
|
41
|
-
email: string;
|
|
42
|
-
password: string;
|
|
43
|
-
}): Promise<RegisterResponse>;
|
|
44
|
-
/**
|
|
45
|
-
* Login a user with credentials
|
|
46
|
-
*/
|
|
47
|
-
login(credentials: {
|
|
48
|
-
username: string;
|
|
49
|
-
password: string;
|
|
50
|
-
}): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Validate the current user session
|
|
53
|
-
*/
|
|
54
|
-
validateCurrentSession(): Promise<boolean>;
|
|
55
|
-
/**
|
|
56
|
-
* Refresh the access token using the refresh token
|
|
57
|
-
*/
|
|
58
|
-
refreshToken(): Promise<{
|
|
59
|
-
accessToken: string;
|
|
60
|
-
refreshToken: string;
|
|
61
|
-
} | false>;
|
|
62
|
-
/**
|
|
63
|
-
* Check if the token needs to be refreshed
|
|
64
|
-
*/
|
|
65
|
-
shouldRefreshToken(token: string): boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Logout the current user
|
|
68
|
-
*/
|
|
69
|
-
logout(): Promise<void>;
|
|
70
|
-
/**
|
|
71
|
-
* Check if the user is authenticated
|
|
72
|
-
*/
|
|
73
|
-
isAuthenticated(): Promise<boolean>;
|
|
74
|
-
/**
|
|
75
|
-
* Get the current user ID from stored data or JWT token
|
|
76
|
-
*/
|
|
77
|
-
getCurrentSessionUserId(): Promise<string | null>;
|
|
78
|
-
/**
|
|
79
|
-
* Check if a username is available
|
|
80
|
-
*/
|
|
81
|
-
checkUsernameAvailability(username: string): Promise<{
|
|
82
|
-
available: boolean;
|
|
83
|
-
message?: string;
|
|
84
|
-
}>;
|
|
85
|
-
}
|
|
86
|
-
export declare const authService: AuthService;
|
|
87
|
-
export {};
|
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Authentication Service
|
|
4
|
-
*
|
|
5
|
-
* Manages user authentication, session validation, and token management.
|
|
6
|
-
*/
|
|
7
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.authService = void 0;
|
|
21
|
-
const axios_1 = __importDefault(require("axios"));
|
|
22
|
-
const api_service_1 = require("./api.service");
|
|
23
|
-
const storage_1 = require("../utils/storage");
|
|
24
|
-
const user_service_1 = require("./user.service");
|
|
25
|
-
const constants_1 = require("../constants");
|
|
26
|
-
/**
|
|
27
|
-
* Authentication service class
|
|
28
|
-
*/
|
|
29
|
-
class AuthService {
|
|
30
|
-
/**
|
|
31
|
-
* Register a new user
|
|
32
|
-
*/
|
|
33
|
-
register(user) {
|
|
34
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
var _a;
|
|
36
|
-
try {
|
|
37
|
-
const response = yield api_service_1.apiService.post(constants_1.ENDPOINTS.AUTH.REGISTER, user);
|
|
38
|
-
if (response.data.success && response.data.accessToken && response.data.refreshToken) {
|
|
39
|
-
// Store tokens and user data securely
|
|
40
|
-
yield Promise.all([
|
|
41
|
-
(0, storage_1.storeSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN, response.data.accessToken),
|
|
42
|
-
(0, storage_1.storeSecureData)(constants_1.STORAGE_KEYS.REFRESH_TOKEN, response.data.refreshToken),
|
|
43
|
-
(0, storage_1.storeData)(constants_1.STORAGE_KEYS.USER, response.data.user),
|
|
44
|
-
(0, storage_1.storeData)(constants_1.STORAGE_KEYS.USER_ID, response.data.user.id)
|
|
45
|
-
]);
|
|
46
|
-
}
|
|
47
|
-
return response.data;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
if (axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data)) {
|
|
51
|
-
throw error.response.data;
|
|
52
|
-
}
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Login a user with credentials
|
|
59
|
-
*/
|
|
60
|
-
login(credentials) {
|
|
61
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
var _a;
|
|
63
|
-
// Validate credentials before making the request
|
|
64
|
-
const validationErrors = {
|
|
65
|
-
username: !credentials.username ? "Username is required" : null,
|
|
66
|
-
password: !credentials.password ? "Password is required" : null
|
|
67
|
-
};
|
|
68
|
-
const hasErrors = Object.values(validationErrors).some(error => error !== null);
|
|
69
|
-
if (hasErrors) {
|
|
70
|
-
throw {
|
|
71
|
-
message: constants_1.ERROR_MESSAGES.VALIDATION,
|
|
72
|
-
details: validationErrors
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
const response = yield api_service_1.apiService.post(constants_1.ENDPOINTS.AUTH.LOGIN, credentials);
|
|
77
|
-
const { user, accessToken, refreshToken } = response.data;
|
|
78
|
-
// Store tokens and user data securely
|
|
79
|
-
yield Promise.all([
|
|
80
|
-
(0, storage_1.storeSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN, accessToken),
|
|
81
|
-
(0, storage_1.storeSecureData)(constants_1.STORAGE_KEYS.REFRESH_TOKEN, refreshToken),
|
|
82
|
-
(0, storage_1.storeData)(constants_1.STORAGE_KEYS.USER, user),
|
|
83
|
-
(0, storage_1.storeData)(constants_1.STORAGE_KEYS.USER_ID, user.id),
|
|
84
|
-
user_service_1.userService.addUserSession(user, accessToken, refreshToken)
|
|
85
|
-
]);
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
if (axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data)) {
|
|
89
|
-
if (error.response.data.details) {
|
|
90
|
-
throw {
|
|
91
|
-
message: error.response.data.message || constants_1.ERROR_MESSAGES.VALIDATION,
|
|
92
|
-
details: error.response.data.details
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
throw error.response.data;
|
|
96
|
-
}
|
|
97
|
-
console.error('Login error:', error);
|
|
98
|
-
throw error;
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Validate the current user session
|
|
104
|
-
*/
|
|
105
|
-
validateCurrentSession() {
|
|
106
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
-
try {
|
|
108
|
-
const accessToken = yield (0, storage_1.getSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN);
|
|
109
|
-
if (!accessToken)
|
|
110
|
-
return false;
|
|
111
|
-
// Check if token needs refresh
|
|
112
|
-
if (this.shouldRefreshToken(accessToken)) {
|
|
113
|
-
const refreshResult = yield this.refreshToken();
|
|
114
|
-
if (!refreshResult)
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
const response = yield api_service_1.apiService.get(constants_1.ENDPOINTS.AUTH.VALIDATE);
|
|
118
|
-
return response.data.valid;
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Refresh the access token using the refresh token
|
|
127
|
-
*/
|
|
128
|
-
refreshToken() {
|
|
129
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
try {
|
|
131
|
-
const currentRefreshToken = yield (0, storage_1.getSecureData)(constants_1.STORAGE_KEYS.REFRESH_TOKEN);
|
|
132
|
-
if (!currentRefreshToken) {
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
const response = yield api_service_1.apiService.post(constants_1.ENDPOINTS.AUTH.REFRESH, { refreshToken: currentRefreshToken });
|
|
136
|
-
const { accessToken, refreshToken = currentRefreshToken } = response.data;
|
|
137
|
-
if (!accessToken) {
|
|
138
|
-
throw new Error('Invalid refresh response');
|
|
139
|
-
}
|
|
140
|
-
// Store tokens securely
|
|
141
|
-
yield Promise.all([
|
|
142
|
-
(0, storage_1.storeSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN, accessToken),
|
|
143
|
-
(0, storage_1.storeSecureData)(constants_1.STORAGE_KEYS.REFRESH_TOKEN, refreshToken)
|
|
144
|
-
]);
|
|
145
|
-
return { accessToken, refreshToken };
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
console.error('Token refresh failed:', error);
|
|
149
|
-
yield this.logout();
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Check if the token needs to be refreshed
|
|
156
|
-
*/
|
|
157
|
-
shouldRefreshToken(token) {
|
|
158
|
-
try {
|
|
159
|
-
const parts = token.split('.');
|
|
160
|
-
if (parts.length !== 3)
|
|
161
|
-
return true;
|
|
162
|
-
const payload = JSON.parse(atob(parts[1]));
|
|
163
|
-
const exp = payload.exp * 1000; // Convert to milliseconds
|
|
164
|
-
const now = Date.now();
|
|
165
|
-
// Refresh if token expires in less than 5 minutes
|
|
166
|
-
return exp - now < 5 * 60 * 1000;
|
|
167
|
-
}
|
|
168
|
-
catch (error) {
|
|
169
|
-
console.error('Error checking token expiration:', error);
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Logout the current user
|
|
175
|
-
*/
|
|
176
|
-
logout() {
|
|
177
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
try {
|
|
179
|
-
const session = yield user_service_1.userService.getActiveSession();
|
|
180
|
-
if (session === null || session === void 0 ? void 0 : session.id) {
|
|
181
|
-
yield user_service_1.userService.removeUserSession(session.id);
|
|
182
|
-
}
|
|
183
|
-
// Clear all secure data
|
|
184
|
-
yield Promise.all([
|
|
185
|
-
(0, storage_1.clearSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN),
|
|
186
|
-
(0, storage_1.clearSecureData)(constants_1.STORAGE_KEYS.REFRESH_TOKEN)
|
|
187
|
-
]);
|
|
188
|
-
// Optional: Call logout endpoint to invalidate on server
|
|
189
|
-
try {
|
|
190
|
-
yield api_service_1.apiService.post(constants_1.ENDPOINTS.AUTH.LOGOUT);
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
// Continue with logout even if server call fails
|
|
194
|
-
console.warn('Server logout failed:', error);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
catch (error) {
|
|
198
|
-
console.error('Logout error:', error);
|
|
199
|
-
throw error;
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Check if the user is authenticated
|
|
205
|
-
*/
|
|
206
|
-
isAuthenticated() {
|
|
207
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
-
try {
|
|
209
|
-
const session = yield user_service_1.userService.getActiveSession();
|
|
210
|
-
const accessToken = yield (0, storage_1.getSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN);
|
|
211
|
-
return !!(session && accessToken);
|
|
212
|
-
}
|
|
213
|
-
catch (_a) {
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Get the current user ID from stored data or JWT token
|
|
220
|
-
*/
|
|
221
|
-
getCurrentSessionUserId() {
|
|
222
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
223
|
-
try {
|
|
224
|
-
// First try to get the stored userId
|
|
225
|
-
const userId = yield (0, storage_1.getSecureData)(constants_1.STORAGE_KEYS.USER_ID);
|
|
226
|
-
if (userId)
|
|
227
|
-
return userId;
|
|
228
|
-
// If not available, try to decode from token
|
|
229
|
-
const token = yield (0, storage_1.getSecureData)(constants_1.STORAGE_KEYS.ACCESS_TOKEN);
|
|
230
|
-
if (!token)
|
|
231
|
-
return null;
|
|
232
|
-
const parts = token.split('.');
|
|
233
|
-
if (parts.length !== 3)
|
|
234
|
-
return null;
|
|
235
|
-
const payload = JSON.parse(atob(parts[1]));
|
|
236
|
-
return payload.id || null;
|
|
237
|
-
}
|
|
238
|
-
catch (error) {
|
|
239
|
-
console.error('Error decoding token:', error);
|
|
240
|
-
return null;
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Check if a username is available
|
|
246
|
-
*/
|
|
247
|
-
checkUsernameAvailability(username) {
|
|
248
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
-
var _a;
|
|
250
|
-
try {
|
|
251
|
-
const response = yield api_service_1.apiService.get(`/auth/check-username/${username}`);
|
|
252
|
-
return response.data;
|
|
253
|
-
}
|
|
254
|
-
catch (error) {
|
|
255
|
-
if (axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data)) {
|
|
256
|
-
return {
|
|
257
|
-
available: false,
|
|
258
|
-
message: error.response.data.message || 'Username is not available'
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
throw error;
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
exports.authService = new AuthService();
|
package/dist/services/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OxyHQ Services
|
|
3
|
-
*
|
|
4
|
-
* This file exports all services for interacting with the OxyHQ API.
|
|
5
|
-
* These services provide core functionality for authentication, data fetching,
|
|
6
|
-
* and other backend operations.
|
|
7
|
-
*/
|
|
8
|
-
export { apiService } from './api.service';
|
|
9
|
-
export { OxyClient } from './OxyClient';
|
|
10
|
-
import { OxyClient as OxyClientType } from './OxyClient';
|
|
11
|
-
import { authService as AuthServiceType } from './auth.service';
|
|
12
|
-
import { userService as UserServiceType } from './user.service';
|
|
13
|
-
import { profileService as ProfileServiceType } from './profile.service';
|
|
14
|
-
import { paymentService as PaymentServiceType } from './payment.service';
|
|
15
|
-
import { subscriptionService as SubscriptionServiceType } from './subscription.service';
|
|
16
|
-
import { privacyService as PrivacyServiceType } from './privacy.service';
|
|
17
|
-
export declare const getOxyClient: () => OxyClientType;
|
|
18
|
-
export declare const getAuthService: () => typeof AuthServiceType;
|
|
19
|
-
export declare const getUserService: () => typeof UserServiceType;
|
|
20
|
-
export declare const getProfileService: () => typeof ProfileServiceType;
|
|
21
|
-
export declare const getPaymentService: () => typeof PaymentServiceType;
|
|
22
|
-
export declare const getSubscriptionService: () => typeof SubscriptionServiceType;
|
|
23
|
-
export declare const getPrivacyService: () => typeof PrivacyServiceType;
|
|
24
|
-
export { getOxyClient as oxyClient };
|
|
25
|
-
export { getAuthService as authService };
|
|
26
|
-
export { getUserService as userService };
|
|
27
|
-
export { getProfileService as profileService };
|
|
28
|
-
export { getPaymentService as paymentService };
|
|
29
|
-
export { getSubscriptionService as subscriptionService };
|
|
30
|
-
export { getPrivacyService as privacyService };
|