@qyh213/easyauth-client 1.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +425 -0
- package/dist/chunk-H65ZLXQJ.mjs +495 -0
- package/dist/client-RPDHpVsj.d.mts +300 -0
- package/dist/client-RPDHpVsj.d.ts +300 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +530 -0
- package/dist/index.mjs +18 -0
- package/dist/next.d.mts +106 -0
- package/dist/next.d.ts +106 -0
- package/dist/next.js +638 -0
- package/dist/next.mjs +124 -0
- package/dist/react.d.mts +68 -0
- package/dist/react.d.ts +68 -0
- package/dist/react.js +733 -0
- package/dist/react.mjs +220 -0
- package/package.json +99 -0
package/dist/react.js
ADDED
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react.tsx
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
EasyAuthClient: () => EasyAuthClient,
|
|
24
|
+
EasyAuthError: () => EasyAuthError,
|
|
25
|
+
EasyAuthProvider: () => EasyAuthProvider,
|
|
26
|
+
LocalStorageTokenStorage: () => LocalStorageTokenStorage,
|
|
27
|
+
LoginForm: () => LoginForm,
|
|
28
|
+
MemoryTokenStorage: () => MemoryTokenStorage,
|
|
29
|
+
Protected: () => Protected,
|
|
30
|
+
ScopeError: () => ScopeError,
|
|
31
|
+
useAuthActions: () => useAuthActions,
|
|
32
|
+
useEasyAuth: () => useEasyAuth,
|
|
33
|
+
useEasyAuthClient: () => useEasyAuthClient,
|
|
34
|
+
useIsAuthenticated: () => useIsAuthenticated,
|
|
35
|
+
useUser: () => useUser
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(react_exports);
|
|
38
|
+
var import_react = require("react");
|
|
39
|
+
|
|
40
|
+
// src/types.ts
|
|
41
|
+
var EasyAuthError = class extends Error {
|
|
42
|
+
constructor(message, code, statusCode) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.code = code;
|
|
45
|
+
this.statusCode = statusCode;
|
|
46
|
+
this.name = "EasyAuthError";
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var ScopeError = class extends EasyAuthError {
|
|
50
|
+
constructor(message, requiredScopes, providedScopes) {
|
|
51
|
+
super(message, "INSUFFICIENT_SCOPES", 403);
|
|
52
|
+
this.requiredScopes = requiredScopes;
|
|
53
|
+
this.providedScopes = providedScopes;
|
|
54
|
+
this.name = "ScopeError";
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var _LocalStorageTokenStorage = class _LocalStorageTokenStorage {
|
|
58
|
+
getToken() {
|
|
59
|
+
if (typeof window === "undefined") return null;
|
|
60
|
+
return localStorage.getItem(_LocalStorageTokenStorage.KEY);
|
|
61
|
+
}
|
|
62
|
+
setToken(token) {
|
|
63
|
+
if (typeof window === "undefined") return;
|
|
64
|
+
localStorage.setItem(_LocalStorageTokenStorage.KEY, token);
|
|
65
|
+
}
|
|
66
|
+
removeToken() {
|
|
67
|
+
if (typeof window === "undefined") return;
|
|
68
|
+
localStorage.removeItem(_LocalStorageTokenStorage.KEY);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
_LocalStorageTokenStorage.KEY = "easy_auth_token";
|
|
72
|
+
var LocalStorageTokenStorage = _LocalStorageTokenStorage;
|
|
73
|
+
var MemoryTokenStorage = class {
|
|
74
|
+
constructor() {
|
|
75
|
+
this.token = null;
|
|
76
|
+
}
|
|
77
|
+
getToken() {
|
|
78
|
+
return this.token;
|
|
79
|
+
}
|
|
80
|
+
setToken(token) {
|
|
81
|
+
this.token = token;
|
|
82
|
+
}
|
|
83
|
+
removeToken() {
|
|
84
|
+
this.token = null;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/client.ts
|
|
89
|
+
var EasyAuthClient = class {
|
|
90
|
+
constructor(config, tokenStorage) {
|
|
91
|
+
this.config = {
|
|
92
|
+
apiKey: "",
|
|
93
|
+
defaultTokenExpiry: 24,
|
|
94
|
+
...config
|
|
95
|
+
};
|
|
96
|
+
this.tokenStorage = tokenStorage || new LocalStorageTokenStorage();
|
|
97
|
+
}
|
|
98
|
+
// ============ HTTP Utilities ============
|
|
99
|
+
async request(endpoint, options = {}, requireAuth = false) {
|
|
100
|
+
const url = `${this.config.baseUrl}${endpoint}`;
|
|
101
|
+
const headers = {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
...options.headers || {}
|
|
104
|
+
};
|
|
105
|
+
if (this.config.apiKey && !requireAuth) {
|
|
106
|
+
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
107
|
+
}
|
|
108
|
+
if (requireAuth) {
|
|
109
|
+
const token = this.getAccessToken();
|
|
110
|
+
if (token) {
|
|
111
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const response = await fetch(url, {
|
|
116
|
+
...options,
|
|
117
|
+
headers
|
|
118
|
+
});
|
|
119
|
+
const data = await response.json();
|
|
120
|
+
if (!response.ok) {
|
|
121
|
+
if (response.status === 403 && data.required && data.provided) {
|
|
122
|
+
throw new ScopeError(
|
|
123
|
+
data.error || "Insufficient scopes",
|
|
124
|
+
data.required,
|
|
125
|
+
data.provided
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
throw new EasyAuthError(
|
|
129
|
+
data.error || `HTTP ${response.status}`,
|
|
130
|
+
data.code || "UNKNOWN_ERROR",
|
|
131
|
+
response.status
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return data;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (error instanceof EasyAuthError) {
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
throw new EasyAuthError(
|
|
140
|
+
error instanceof Error ? error.message : "Network error",
|
|
141
|
+
"NETWORK_ERROR"
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// ============ Configuration ============
|
|
146
|
+
/**
|
|
147
|
+
* Update the client's service configuration
|
|
148
|
+
*/
|
|
149
|
+
configure(config) {
|
|
150
|
+
this.config = { ...this.config, ...config };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get current configuration (without sensitive data)
|
|
154
|
+
*/
|
|
155
|
+
getConfig() {
|
|
156
|
+
const { apiKey: _, ...rest } = this.config;
|
|
157
|
+
return rest;
|
|
158
|
+
}
|
|
159
|
+
// ============ Token Management ============
|
|
160
|
+
/**
|
|
161
|
+
* Get the stored access token
|
|
162
|
+
*/
|
|
163
|
+
getAccessToken() {
|
|
164
|
+
return this.tokenStorage.getToken();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Store an access token
|
|
168
|
+
*/
|
|
169
|
+
setAccessToken(token) {
|
|
170
|
+
this.tokenStorage.setToken(token);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Remove the stored access token (logout)
|
|
174
|
+
*/
|
|
175
|
+
clearAccessToken() {
|
|
176
|
+
this.tokenStorage.removeToken();
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Check if user is logged in
|
|
180
|
+
*/
|
|
181
|
+
isLoggedIn() {
|
|
182
|
+
return !!this.getAccessToken();
|
|
183
|
+
}
|
|
184
|
+
// ============ Scope Validation ============
|
|
185
|
+
/**
|
|
186
|
+
* Introspect a token or API key and validate required scopes
|
|
187
|
+
*/
|
|
188
|
+
async introspect(request) {
|
|
189
|
+
return this.request("/api/v1/introspect", {
|
|
190
|
+
method: "POST",
|
|
191
|
+
body: JSON.stringify(request)
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Validate the current token with optional scope checking
|
|
196
|
+
*/
|
|
197
|
+
async validateWithScopes(requiredScopes) {
|
|
198
|
+
const token = this.getAccessToken();
|
|
199
|
+
if (!token) {
|
|
200
|
+
return { valid: false, error: "No token provided" };
|
|
201
|
+
}
|
|
202
|
+
if (!this.config.apiKey) {
|
|
203
|
+
return { valid: false, error: "Service API key not configured" };
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
const serviceId = this.config.apiKey.split(".")[0];
|
|
207
|
+
const result = await this.introspect({
|
|
208
|
+
serviceId,
|
|
209
|
+
token,
|
|
210
|
+
requiredScopes
|
|
211
|
+
});
|
|
212
|
+
return {
|
|
213
|
+
valid: result.valid,
|
|
214
|
+
type: result.type === "bearer" ? "token" : "api_key",
|
|
215
|
+
scopes: result.scopes,
|
|
216
|
+
userId: result.userId,
|
|
217
|
+
email: result.email,
|
|
218
|
+
error: result.error
|
|
219
|
+
};
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (error instanceof ScopeError) {
|
|
222
|
+
return {
|
|
223
|
+
valid: false,
|
|
224
|
+
error: error.message,
|
|
225
|
+
scopes: error.providedScopes
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (error instanceof EasyAuthError && error.statusCode === 401) {
|
|
229
|
+
return { valid: false, error: "Invalid or expired token" };
|
|
230
|
+
}
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Check if the current user has all the required scopes
|
|
236
|
+
* Throws ScopeError if scopes are insufficient
|
|
237
|
+
*/
|
|
238
|
+
async requireScopes(...requiredScopes) {
|
|
239
|
+
const result = await this.validateWithScopes(requiredScopes);
|
|
240
|
+
if (!result.valid) {
|
|
241
|
+
throw new EasyAuthError(
|
|
242
|
+
result.error || "Authentication required",
|
|
243
|
+
"UNAUTHORIZED",
|
|
244
|
+
401
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
const hasScopes = requiredScopes.every(
|
|
248
|
+
(scope) => result.scopes?.includes(scope)
|
|
249
|
+
);
|
|
250
|
+
if (!hasScopes) {
|
|
251
|
+
throw new ScopeError(
|
|
252
|
+
`Required scopes: ${requiredScopes.join(", ")}`,
|
|
253
|
+
requiredScopes,
|
|
254
|
+
result.scopes || []
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Execute a function only if the user has the required scopes
|
|
261
|
+
*/
|
|
262
|
+
async withScope(scopes, fn) {
|
|
263
|
+
await this.requireScopes(...scopes);
|
|
264
|
+
return fn();
|
|
265
|
+
}
|
|
266
|
+
// ============ Authentication ============
|
|
267
|
+
/**
|
|
268
|
+
* Login a user and store the access token
|
|
269
|
+
*/
|
|
270
|
+
async login(credentials) {
|
|
271
|
+
if (!this.config.apiKey) {
|
|
272
|
+
throw new EasyAuthError(
|
|
273
|
+
"API key required for login. Configure the client first.",
|
|
274
|
+
"MISSING_API_KEY"
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
const response = await this.request("/api/v1/auth/login", {
|
|
278
|
+
method: "POST",
|
|
279
|
+
body: JSON.stringify({
|
|
280
|
+
email: credentials.email,
|
|
281
|
+
password: credentials.password,
|
|
282
|
+
expires_in_hours: credentials.expiresInHours || this.config.defaultTokenExpiry,
|
|
283
|
+
scopes: credentials.scopes
|
|
284
|
+
})
|
|
285
|
+
});
|
|
286
|
+
const token = {
|
|
287
|
+
accessToken: response.access_token,
|
|
288
|
+
tokenType: response.token_type,
|
|
289
|
+
expiresIn: response.expires_in,
|
|
290
|
+
expiresAt: response.expires_at,
|
|
291
|
+
userId: response.user_id,
|
|
292
|
+
email: response.email,
|
|
293
|
+
scopes: response.scopes
|
|
294
|
+
};
|
|
295
|
+
this.setAccessToken(token.accessToken);
|
|
296
|
+
return token;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Logout the current user
|
|
300
|
+
*/
|
|
301
|
+
async logout() {
|
|
302
|
+
const token = this.getAccessToken();
|
|
303
|
+
if (token) {
|
|
304
|
+
try {
|
|
305
|
+
await this.request("/api/v1/auth/logout", {
|
|
306
|
+
method: "POST"
|
|
307
|
+
});
|
|
308
|
+
} catch (error) {
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
this.clearAccessToken();
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Validate the current token or an API key
|
|
315
|
+
* @deprecated Use validateWithScopes instead
|
|
316
|
+
*/
|
|
317
|
+
async validate(_credential) {
|
|
318
|
+
return this.validateWithScopes();
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Refresh the current token
|
|
322
|
+
*/
|
|
323
|
+
async refreshToken() {
|
|
324
|
+
const response = await this.request("/api/v1/auth/refresh", {
|
|
325
|
+
method: "POST"
|
|
326
|
+
}, true);
|
|
327
|
+
const token = {
|
|
328
|
+
accessToken: response.access_token,
|
|
329
|
+
tokenType: response.token_type,
|
|
330
|
+
expiresIn: response.expires_in,
|
|
331
|
+
expiresAt: response.expires_at,
|
|
332
|
+
userId: "",
|
|
333
|
+
// Refresh doesn't return user info
|
|
334
|
+
email: ""
|
|
335
|
+
};
|
|
336
|
+
this.setAccessToken(token.accessToken);
|
|
337
|
+
return token;
|
|
338
|
+
}
|
|
339
|
+
// ============ User Management ============
|
|
340
|
+
/**
|
|
341
|
+
* Create a new user (requires admin API key)
|
|
342
|
+
*/
|
|
343
|
+
async createUser(request) {
|
|
344
|
+
const response = await this.request("/api/v1/users/create", {
|
|
345
|
+
method: "POST",
|
|
346
|
+
body: JSON.stringify(request)
|
|
347
|
+
});
|
|
348
|
+
return {
|
|
349
|
+
userId: response.user_id,
|
|
350
|
+
email: response.email,
|
|
351
|
+
createdAt: response.created_at
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* List all users for the service
|
|
356
|
+
*/
|
|
357
|
+
async listUsers() {
|
|
358
|
+
const response = await this.request("/api/v1/users/list");
|
|
359
|
+
return response.users;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Delete a user
|
|
363
|
+
*/
|
|
364
|
+
async deleteUser(userId) {
|
|
365
|
+
await this.request("/api/v1/users/delete", {
|
|
366
|
+
method: "POST",
|
|
367
|
+
body: JSON.stringify({ user_id: userId })
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Update user password
|
|
372
|
+
*/
|
|
373
|
+
async updatePassword(userId, oldPassword, newPassword) {
|
|
374
|
+
await this.request("/api/v1/users/password", {
|
|
375
|
+
method: "POST",
|
|
376
|
+
body: JSON.stringify({
|
|
377
|
+
user_id: userId,
|
|
378
|
+
old_password: oldPassword,
|
|
379
|
+
new_password: newPassword
|
|
380
|
+
})
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
// ============ Scope Management ============
|
|
384
|
+
/**
|
|
385
|
+
* List custom scopes for the service
|
|
386
|
+
*/
|
|
387
|
+
async listScopes() {
|
|
388
|
+
const response = await this.request("/api/v1/scopes/list");
|
|
389
|
+
return response.scopes;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Create a custom scope
|
|
393
|
+
*/
|
|
394
|
+
async createScope(request) {
|
|
395
|
+
const response = await this.request("/api/v1/scopes/create", {
|
|
396
|
+
method: "POST",
|
|
397
|
+
body: JSON.stringify(request)
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
scopeId: response.scope_id,
|
|
401
|
+
name: response.name,
|
|
402
|
+
key: response.key,
|
|
403
|
+
description: response.description,
|
|
404
|
+
createdAt: response.created_at
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Delete a custom scope
|
|
409
|
+
*/
|
|
410
|
+
async deleteScope(scopeId) {
|
|
411
|
+
await this.request("/api/v1/scopes/delete", {
|
|
412
|
+
method: "POST",
|
|
413
|
+
body: JSON.stringify({ scope_id: scopeId })
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
// ============ API Key Management ============
|
|
417
|
+
/**
|
|
418
|
+
* List all API keys for the service
|
|
419
|
+
*/
|
|
420
|
+
async listApiKeys() {
|
|
421
|
+
const response = await this.request("/api/v1/keys/list");
|
|
422
|
+
return response.apiKeys;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Create a new API key with optional custom scopes
|
|
426
|
+
*/
|
|
427
|
+
async createApiKey(request) {
|
|
428
|
+
const response = await this.request("/api/v1/keys/create", {
|
|
429
|
+
method: "POST",
|
|
430
|
+
body: JSON.stringify({
|
|
431
|
+
name: request.name,
|
|
432
|
+
scope: request.scope || "service",
|
|
433
|
+
scopes: request.scopes,
|
|
434
|
+
key_type: request.keyType || "service",
|
|
435
|
+
tps_limit: request.tpsLimit || 100
|
|
436
|
+
})
|
|
437
|
+
});
|
|
438
|
+
return {
|
|
439
|
+
keyId: response.key_id,
|
|
440
|
+
apiKey: response.api_key,
|
|
441
|
+
name: response.name,
|
|
442
|
+
scope: response.scope,
|
|
443
|
+
scopes: response.scopes
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Revoke an API key
|
|
448
|
+
*/
|
|
449
|
+
async revokeApiKey(keyId) {
|
|
450
|
+
await this.request("/api/v1/keys/revoke", {
|
|
451
|
+
method: "POST",
|
|
452
|
+
body: JSON.stringify({ key_id: keyId })
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
// ============ Service Management ============
|
|
456
|
+
/**
|
|
457
|
+
* Onboard a new service (requires master admin key)
|
|
458
|
+
*/
|
|
459
|
+
async onboardService(request, masterAdminKey) {
|
|
460
|
+
const response = await this.request("/api/v1/services/onboard", {
|
|
461
|
+
method: "POST",
|
|
462
|
+
headers: {
|
|
463
|
+
"x-onboarding-admin-key": masterAdminKey
|
|
464
|
+
},
|
|
465
|
+
body: JSON.stringify({
|
|
466
|
+
service_name: request.serviceName,
|
|
467
|
+
owner_email: request.ownerEmail
|
|
468
|
+
})
|
|
469
|
+
});
|
|
470
|
+
return {
|
|
471
|
+
serviceId: response.service_id,
|
|
472
|
+
serviceName: response.service_name,
|
|
473
|
+
ownerEmail: response.owner_email,
|
|
474
|
+
adminKeyId: response.admin_key_id,
|
|
475
|
+
serviceAdminApiKey: response.service_admin_api_key,
|
|
476
|
+
createdAt: response.created_at
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Delete a service (requires master admin key)
|
|
481
|
+
*/
|
|
482
|
+
async deleteService(serviceId, masterAdminKey) {
|
|
483
|
+
await this.request("/api/v1/services/deboard", {
|
|
484
|
+
method: "POST",
|
|
485
|
+
headers: {
|
|
486
|
+
"x-onboarding-admin-key": masterAdminKey
|
|
487
|
+
},
|
|
488
|
+
body: JSON.stringify({ service_id: serviceId })
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
// ============ Webhook Management ============
|
|
492
|
+
/**
|
|
493
|
+
* List webhooks
|
|
494
|
+
*/
|
|
495
|
+
async listWebhooks() {
|
|
496
|
+
const response = await this.request(
|
|
497
|
+
"/api/v1/webhooks/list"
|
|
498
|
+
);
|
|
499
|
+
return response.webhooks;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Register a webhook
|
|
503
|
+
*/
|
|
504
|
+
async registerWebhook(request) {
|
|
505
|
+
const response = await this.request("/api/v1/webhooks/register", {
|
|
506
|
+
method: "POST",
|
|
507
|
+
body: JSON.stringify(request)
|
|
508
|
+
});
|
|
509
|
+
return {
|
|
510
|
+
webhookId: response.webhook_id,
|
|
511
|
+
url: response.url,
|
|
512
|
+
events: response.events,
|
|
513
|
+
active: response.active,
|
|
514
|
+
createdAt: response.created_at
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Delete a webhook
|
|
519
|
+
*/
|
|
520
|
+
async deleteWebhook(webhookId) {
|
|
521
|
+
await this.request("/api/v1/webhooks/delete", {
|
|
522
|
+
method: "POST",
|
|
523
|
+
body: JSON.stringify({ webhook_id: webhookId })
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
// src/react.tsx
|
|
529
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
530
|
+
var EasyAuthContext = (0, import_react.createContext)(null);
|
|
531
|
+
function EasyAuthProvider({
|
|
532
|
+
config,
|
|
533
|
+
tokenStorage,
|
|
534
|
+
children,
|
|
535
|
+
validateOnMount = true,
|
|
536
|
+
validationInterval = 5 * 60 * 1e3
|
|
537
|
+
}) {
|
|
538
|
+
const [client] = (0, import_react.useState)(() => new EasyAuthClient(config, tokenStorage));
|
|
539
|
+
const [isAuthenticated, setIsAuthenticated] = (0, import_react.useState)(false);
|
|
540
|
+
const [user, setUser] = (0, import_react.useState)(null);
|
|
541
|
+
const [isLoading, setIsLoading] = (0, import_react.useState)(true);
|
|
542
|
+
(0, import_react.useEffect)(() => {
|
|
543
|
+
const checkAuth = async () => {
|
|
544
|
+
const token = client.getAccessToken();
|
|
545
|
+
if (token && validateOnMount) {
|
|
546
|
+
try {
|
|
547
|
+
const result = await client.validate();
|
|
548
|
+
if (result.valid) {
|
|
549
|
+
setIsAuthenticated(true);
|
|
550
|
+
} else {
|
|
551
|
+
client.clearAccessToken();
|
|
552
|
+
}
|
|
553
|
+
} catch (error) {
|
|
554
|
+
client.clearAccessToken();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
setIsLoading(false);
|
|
558
|
+
};
|
|
559
|
+
checkAuth();
|
|
560
|
+
}, [client, validateOnMount]);
|
|
561
|
+
(0, import_react.useEffect)(() => {
|
|
562
|
+
if (!isAuthenticated || !validationInterval) return;
|
|
563
|
+
const interval = setInterval(async () => {
|
|
564
|
+
try {
|
|
565
|
+
const result = await client.validate();
|
|
566
|
+
if (!result.valid) {
|
|
567
|
+
setIsAuthenticated(false);
|
|
568
|
+
setUser(null);
|
|
569
|
+
}
|
|
570
|
+
} catch (error) {
|
|
571
|
+
setIsAuthenticated(false);
|
|
572
|
+
setUser(null);
|
|
573
|
+
}
|
|
574
|
+
}, validationInterval);
|
|
575
|
+
return () => clearInterval(interval);
|
|
576
|
+
}, [client, isAuthenticated, validationInterval]);
|
|
577
|
+
const login = (0, import_react.useCallback)(
|
|
578
|
+
async (email, password) => {
|
|
579
|
+
setIsLoading(true);
|
|
580
|
+
try {
|
|
581
|
+
const token = await client.login({ email, password });
|
|
582
|
+
setIsAuthenticated(true);
|
|
583
|
+
setUser({
|
|
584
|
+
userId: token.userId,
|
|
585
|
+
email: token.email,
|
|
586
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
587
|
+
});
|
|
588
|
+
} finally {
|
|
589
|
+
setIsLoading(false);
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
[client]
|
|
593
|
+
);
|
|
594
|
+
const logout = (0, import_react.useCallback)(async () => {
|
|
595
|
+
setIsLoading(true);
|
|
596
|
+
try {
|
|
597
|
+
await client.logout();
|
|
598
|
+
} finally {
|
|
599
|
+
setIsAuthenticated(false);
|
|
600
|
+
setUser(null);
|
|
601
|
+
setIsLoading(false);
|
|
602
|
+
}
|
|
603
|
+
}, [client]);
|
|
604
|
+
const validate = (0, import_react.useCallback)(async () => {
|
|
605
|
+
const result = await client.validate();
|
|
606
|
+
if (!result.valid) {
|
|
607
|
+
setIsAuthenticated(false);
|
|
608
|
+
setUser(null);
|
|
609
|
+
}
|
|
610
|
+
return result;
|
|
611
|
+
}, [client]);
|
|
612
|
+
const value = {
|
|
613
|
+
client,
|
|
614
|
+
isAuthenticated,
|
|
615
|
+
user,
|
|
616
|
+
isLoading,
|
|
617
|
+
login,
|
|
618
|
+
logout,
|
|
619
|
+
validate
|
|
620
|
+
};
|
|
621
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EasyAuthContext.Provider, { value, children });
|
|
622
|
+
}
|
|
623
|
+
function useEasyAuth() {
|
|
624
|
+
const context = (0, import_react.useContext)(EasyAuthContext);
|
|
625
|
+
if (!context) {
|
|
626
|
+
throw new Error("useEasyAuth must be used within EasyAuthProvider");
|
|
627
|
+
}
|
|
628
|
+
return context;
|
|
629
|
+
}
|
|
630
|
+
function useEasyAuthClient() {
|
|
631
|
+
return useEasyAuth().client;
|
|
632
|
+
}
|
|
633
|
+
function useIsAuthenticated() {
|
|
634
|
+
return useEasyAuth().isAuthenticated;
|
|
635
|
+
}
|
|
636
|
+
function useUser() {
|
|
637
|
+
return useEasyAuth().user;
|
|
638
|
+
}
|
|
639
|
+
function useAuthActions() {
|
|
640
|
+
const { login, logout, isLoading } = useEasyAuth();
|
|
641
|
+
return { login, logout, isLoading };
|
|
642
|
+
}
|
|
643
|
+
function Protected({ children, fallback = null }) {
|
|
644
|
+
const { isAuthenticated, isLoading } = useEasyAuth();
|
|
645
|
+
if (isLoading) {
|
|
646
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback });
|
|
647
|
+
}
|
|
648
|
+
if (!isAuthenticated) {
|
|
649
|
+
return null;
|
|
650
|
+
}
|
|
651
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
652
|
+
}
|
|
653
|
+
function LoginForm({ onSuccess, onError, className }) {
|
|
654
|
+
const { login, isLoading } = useEasyAuth();
|
|
655
|
+
const [email, setEmail] = (0, import_react.useState)("");
|
|
656
|
+
const [password, setPassword] = (0, import_react.useState)("");
|
|
657
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
658
|
+
const handleSubmit = async (e) => {
|
|
659
|
+
e.preventDefault();
|
|
660
|
+
setError(null);
|
|
661
|
+
try {
|
|
662
|
+
await login(email, password);
|
|
663
|
+
onSuccess?.();
|
|
664
|
+
} catch (err) {
|
|
665
|
+
const message = err instanceof Error ? err.message : "Login failed";
|
|
666
|
+
setError(message);
|
|
667
|
+
onError?.(err instanceof Error ? err : new Error(message));
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("form", { onSubmit: handleSubmit, className, children: [
|
|
671
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { color: "red", marginBottom: "1rem" }, children: error }),
|
|
672
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { marginBottom: "1rem" }, children: [
|
|
673
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { htmlFor: "email", children: "Email" }),
|
|
674
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
675
|
+
"input",
|
|
676
|
+
{
|
|
677
|
+
id: "email",
|
|
678
|
+
type: "email",
|
|
679
|
+
value: email,
|
|
680
|
+
onChange: (e) => setEmail(e.target.value),
|
|
681
|
+
required: true,
|
|
682
|
+
style: { display: "block", width: "100%", padding: "0.5rem" }
|
|
683
|
+
}
|
|
684
|
+
)
|
|
685
|
+
] }),
|
|
686
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { marginBottom: "1rem" }, children: [
|
|
687
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { htmlFor: "password", children: "Password" }),
|
|
688
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
689
|
+
"input",
|
|
690
|
+
{
|
|
691
|
+
id: "password",
|
|
692
|
+
type: "password",
|
|
693
|
+
value: password,
|
|
694
|
+
onChange: (e) => setPassword(e.target.value),
|
|
695
|
+
required: true,
|
|
696
|
+
style: { display: "block", width: "100%", padding: "0.5rem" }
|
|
697
|
+
}
|
|
698
|
+
)
|
|
699
|
+
] }),
|
|
700
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
701
|
+
"button",
|
|
702
|
+
{
|
|
703
|
+
type: "submit",
|
|
704
|
+
disabled: isLoading,
|
|
705
|
+
style: {
|
|
706
|
+
padding: "0.5rem 1rem",
|
|
707
|
+
background: isLoading ? "#ccc" : "#007bff",
|
|
708
|
+
color: "white",
|
|
709
|
+
border: "none",
|
|
710
|
+
borderRadius: "4px",
|
|
711
|
+
cursor: isLoading ? "not-allowed" : "pointer"
|
|
712
|
+
},
|
|
713
|
+
children: isLoading ? "Logging in..." : "Login"
|
|
714
|
+
}
|
|
715
|
+
)
|
|
716
|
+
] });
|
|
717
|
+
}
|
|
718
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
719
|
+
0 && (module.exports = {
|
|
720
|
+
EasyAuthClient,
|
|
721
|
+
EasyAuthError,
|
|
722
|
+
EasyAuthProvider,
|
|
723
|
+
LocalStorageTokenStorage,
|
|
724
|
+
LoginForm,
|
|
725
|
+
MemoryTokenStorage,
|
|
726
|
+
Protected,
|
|
727
|
+
ScopeError,
|
|
728
|
+
useAuthActions,
|
|
729
|
+
useEasyAuth,
|
|
730
|
+
useEasyAuthClient,
|
|
731
|
+
useIsAuthenticated,
|
|
732
|
+
useUser
|
|
733
|
+
});
|