@meistrari/auth-core 1.1.1 → 1.2.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/dist/index.d.mts +779 -210
- package/dist/index.d.ts +779 -210
- package/dist/index.mjs +85 -35
- package/package.json +30 -29
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { createRemoteJWKSet, jwtVerify } from 'jose';
|
|
2
|
-
import { createAuthClient } from 'better-auth/client';
|
|
3
|
-
import { organizationClient, twoFactorClient, jwtClient, adminClient, inferAdditionalFields } from 'better-auth/client/plugins';
|
|
1
|
+
import { createRemoteJWKSet, jwtVerify, decodeJwt } from 'jose';
|
|
4
2
|
import { ssoClient } from '@better-auth/sso/client';
|
|
3
|
+
import { createAuthClient } from 'better-auth/client';
|
|
4
|
+
import { organizationClient, twoFactorClient, jwtClient, apiKeyClient, adminClient, inferAdditionalFields } from 'better-auth/client/plugins';
|
|
5
5
|
import { createAccessControl } from 'better-auth/plugins/access';
|
|
6
6
|
import { defaultStatements } from 'better-auth/plugins/organization/access';
|
|
7
7
|
|
|
8
|
-
const version = "1.
|
|
8
|
+
const version = "1.2.1";
|
|
9
9
|
|
|
10
10
|
const statements = {
|
|
11
11
|
...defaultStatements,
|
|
@@ -47,37 +47,19 @@ const organizationAdditionalFields = {
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
class BaseError extends Error {
|
|
51
|
-
code;
|
|
52
|
-
constructor(code, message, options) {
|
|
53
|
-
super(message, options);
|
|
54
|
-
this.code = code;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class InvalidSocialProvider extends BaseError {
|
|
59
|
-
constructor(message) {
|
|
60
|
-
super("INVALID_SOCIAL_PROVIDER", message);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
class InvalidCallbackURL extends BaseError {
|
|
64
|
-
constructor(message) {
|
|
65
|
-
super("INVALID_CALLBACK_URL", message);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
class EmailRequired extends BaseError {
|
|
69
|
-
constructor(message) {
|
|
70
|
-
super("EMAIL_REQUIRED", message);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
50
|
const customEndpointsPluginClient = () => {
|
|
75
51
|
return {
|
|
76
52
|
id: "custom-endpoints",
|
|
77
53
|
$InferServerPlugin: {}
|
|
78
54
|
};
|
|
79
55
|
};
|
|
80
|
-
|
|
56
|
+
const handshakePluginClient = () => {
|
|
57
|
+
return {
|
|
58
|
+
id: "handshake",
|
|
59
|
+
$InferServerPlugin: {}
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
function createAPIClient(apiUrl, fetchOptions = {}) {
|
|
81
63
|
const serviceName = typeof process !== "undefined" ? process.env.SERVICE_NAME : "";
|
|
82
64
|
const userAgent = `auth-sdk:core:${version}${serviceName ? `@${serviceName}` : ""}`;
|
|
83
65
|
return createAuthClient({
|
|
@@ -103,19 +85,22 @@ function createAPIClient(apiUrl, headers) {
|
|
|
103
85
|
// })
|
|
104
86
|
}),
|
|
105
87
|
customEndpointsPluginClient(),
|
|
88
|
+
handshakePluginClient(),
|
|
106
89
|
ssoClient(),
|
|
107
90
|
twoFactorClient(),
|
|
108
91
|
jwtClient(),
|
|
92
|
+
apiKeyClient(),
|
|
109
93
|
adminClient(),
|
|
110
94
|
inferAdditionalFields({
|
|
111
95
|
user: userAdditionalFields
|
|
112
96
|
})
|
|
113
97
|
],
|
|
114
98
|
fetchOptions: {
|
|
99
|
+
...fetchOptions,
|
|
115
100
|
credentials: "include",
|
|
116
101
|
headers: {
|
|
117
102
|
"User-Agent": userAgent,
|
|
118
|
-
...headers
|
|
103
|
+
...fetchOptions.headers ?? {}
|
|
119
104
|
},
|
|
120
105
|
throw: true
|
|
121
106
|
}
|
|
@@ -356,7 +341,7 @@ class OrganizationService {
|
|
|
356
341
|
* @param userId - The user ID to add
|
|
357
342
|
*/
|
|
358
343
|
async addTeamMember(teamId, userId) {
|
|
359
|
-
|
|
344
|
+
return this.client.organization.addTeamMember({
|
|
360
345
|
teamId,
|
|
361
346
|
userId
|
|
362
347
|
});
|
|
@@ -375,6 +360,30 @@ class OrganizationService {
|
|
|
375
360
|
}
|
|
376
361
|
}
|
|
377
362
|
|
|
363
|
+
class BaseError extends Error {
|
|
364
|
+
code;
|
|
365
|
+
constructor(code, message, options) {
|
|
366
|
+
super(message, options);
|
|
367
|
+
this.code = code;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
class InvalidSocialProvider extends BaseError {
|
|
372
|
+
constructor(message) {
|
|
373
|
+
super("INVALID_SOCIAL_PROVIDER", message);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
class InvalidCallbackURL extends BaseError {
|
|
377
|
+
constructor(message) {
|
|
378
|
+
super("INVALID_CALLBACK_URL", message);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
class EmailRequired extends BaseError {
|
|
382
|
+
constructor(message) {
|
|
383
|
+
super("EMAIL_REQUIRED", message);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
378
387
|
function isValidUrl(url) {
|
|
379
388
|
try {
|
|
380
389
|
new URL(url);
|
|
@@ -394,6 +403,30 @@ class SessionService {
|
|
|
394
403
|
this.client = client;
|
|
395
404
|
this.apiUrl = apiUrl;
|
|
396
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* Retrieves the current user session.
|
|
408
|
+
*
|
|
409
|
+
* @returns The current user session
|
|
410
|
+
*/
|
|
411
|
+
async getSession(token) {
|
|
412
|
+
return this.client.getSession({
|
|
413
|
+
...token && {
|
|
414
|
+
fetchOptions: {
|
|
415
|
+
headers: {
|
|
416
|
+
Authorization: `Bearer ${token}`
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Retrieves a valid JWT token.
|
|
424
|
+
*
|
|
425
|
+
* @returns The JWT token
|
|
426
|
+
*/
|
|
427
|
+
async getToken() {
|
|
428
|
+
return this.client.token();
|
|
429
|
+
}
|
|
397
430
|
/**
|
|
398
431
|
* Initiates social authentication flow with Google or Microsoft.
|
|
399
432
|
*
|
|
@@ -508,6 +541,19 @@ class SessionService {
|
|
|
508
541
|
newPassword: password
|
|
509
542
|
});
|
|
510
543
|
}
|
|
544
|
+
/**
|
|
545
|
+
* Retrieves the JWT token for a nonce.
|
|
546
|
+
*
|
|
547
|
+
* @param nonce - The nonce to retrieve the token for
|
|
548
|
+
* @returns The JWT token for the nonce
|
|
549
|
+
*/
|
|
550
|
+
async getNoncePayload(nonce) {
|
|
551
|
+
return this.client.handshake.noncePayload({
|
|
552
|
+
query: {
|
|
553
|
+
nonce
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
}
|
|
511
557
|
}
|
|
512
558
|
|
|
513
559
|
class AuthClient {
|
|
@@ -524,10 +570,10 @@ class AuthClient {
|
|
|
524
570
|
* Creates a new AuthClient instance.
|
|
525
571
|
*
|
|
526
572
|
* @param apiUrl - The base URL of the authentication API
|
|
527
|
-
* @param
|
|
573
|
+
* @param fetchOptions - Custom fetch options to include in all API requests
|
|
528
574
|
*/
|
|
529
|
-
constructor(apiUrl,
|
|
530
|
-
this.client = createAPIClient(apiUrl,
|
|
575
|
+
constructor(apiUrl, fetchOptions = {}) {
|
|
576
|
+
this.client = createAPIClient(apiUrl, fetchOptions);
|
|
531
577
|
this.session = new SessionService(this.client, apiUrl);
|
|
532
578
|
this.organization = new OrganizationService(this.client);
|
|
533
579
|
}
|
|
@@ -553,5 +599,9 @@ async function validateToken(token, apiUrl) {
|
|
|
553
599
|
return false;
|
|
554
600
|
}
|
|
555
601
|
}
|
|
602
|
+
function extractTokenPayload(token) {
|
|
603
|
+
const payload = decodeJwt(token);
|
|
604
|
+
return payload;
|
|
605
|
+
}
|
|
556
606
|
|
|
557
|
-
export { AuthClient, Roles, ac, isTokenExpired, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
|
|
607
|
+
export { AuthClient, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, Roles, ac, extractTokenPayload, isTokenExpired, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
|
package/package.json
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
"name": "@meistrari/auth-core",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.mjs"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "unbuild"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@better-auth/sso": "1.4.1",
|
|
21
|
+
"better-auth": "1.4.1",
|
|
22
|
+
"jose": "6.1.0",
|
|
23
|
+
"nanostores": "1.0.1",
|
|
24
|
+
"@better-fetch/fetch": "1.1.18",
|
|
25
|
+
"better-call": "1.1.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "latest",
|
|
29
|
+
"typescript": "5.9.2",
|
|
30
|
+
"unbuild": "3.6.1"
|
|
9
31
|
}
|
|
10
|
-
|
|
11
|
-
"main": "./dist/index.mjs",
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"files": [
|
|
14
|
-
"dist"
|
|
15
|
-
],
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": "unbuild"
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@better-auth/sso": "1.4.1",
|
|
21
|
-
"better-auth": "1.4.1",
|
|
22
|
-
"jose": "6.1.0",
|
|
23
|
-
"nanostores": "1.0.1",
|
|
24
|
-
"@better-fetch/fetch": "1.1.18"
|
|
25
|
-
},
|
|
26
|
-
"devDependencies": {
|
|
27
|
-
"@types/node": "latest",
|
|
28
|
-
"typescript": "5.9.2",
|
|
29
|
-
"unbuild": "3.6.1"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
+
}
|