@meistrari/auth-core 1.1.0 → 1.2.0

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.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.1.0";
8
+ const version = "1.2.0";
9
9
 
10
10
  const statements = {
11
11
  ...defaultStatements,
@@ -47,37 +47,21 @@ 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
- function createAPIClient(apiUrl, headers) {
56
+ const handshakePluginClient = () => {
57
+ return {
58
+ id: "handshake",
59
+ $InferServerPlugin: {}
60
+ };
61
+ };
62
+ function createAPIClient(apiUrl, fetchOptions = {}) {
63
+ const serviceName = typeof process !== "undefined" ? process.env.SERVICE_NAME : "";
64
+ const userAgent = `auth-sdk:core:${version}${serviceName ? `@${serviceName}` : ""}`;
81
65
  return createAuthClient({
82
66
  baseURL: apiUrl,
83
67
  plugins: [
@@ -101,19 +85,22 @@ function createAPIClient(apiUrl, headers) {
101
85
  // })
102
86
  }),
103
87
  customEndpointsPluginClient(),
88
+ handshakePluginClient(),
104
89
  ssoClient(),
105
90
  twoFactorClient(),
106
91
  jwtClient(),
92
+ apiKeyClient(),
107
93
  adminClient(),
108
94
  inferAdditionalFields({
109
95
  user: userAdditionalFields
110
96
  })
111
97
  ],
112
98
  fetchOptions: {
99
+ ...fetchOptions,
113
100
  credentials: "include",
114
101
  headers: {
115
- "User-Agent": `auth-sdk:core:${version}`,
116
- ...headers
102
+ "User-Agent": userAgent,
103
+ ...fetchOptions.headers ?? {}
117
104
  },
118
105
  throw: true
119
106
  }
@@ -354,7 +341,7 @@ class OrganizationService {
354
341
  * @param userId - The user ID to add
355
342
  */
356
343
  async addTeamMember(teamId, userId) {
357
- await this.client.organization.addTeamMember({
344
+ return this.client.organization.addTeamMember({
358
345
  teamId,
359
346
  userId
360
347
  });
@@ -373,6 +360,30 @@ class OrganizationService {
373
360
  }
374
361
  }
375
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
+
376
387
  function isValidUrl(url) {
377
388
  try {
378
389
  new URL(url);
@@ -392,6 +403,30 @@ class SessionService {
392
403
  this.client = client;
393
404
  this.apiUrl = apiUrl;
394
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
+ }
395
430
  /**
396
431
  * Initiates social authentication flow with Google or Microsoft.
397
432
  *
@@ -506,6 +541,19 @@ class SessionService {
506
541
  newPassword: password
507
542
  });
508
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
+ }
509
557
  }
510
558
 
511
559
  class AuthClient {
@@ -522,10 +570,10 @@ class AuthClient {
522
570
  * Creates a new AuthClient instance.
523
571
  *
524
572
  * @param apiUrl - The base URL of the authentication API
525
- * @param headers - Custom headers to include in all API requests
573
+ * @param fetchOptions - Custom fetch options to include in all API requests
526
574
  */
527
- constructor(apiUrl, headers) {
528
- this.client = createAPIClient(apiUrl, headers);
575
+ constructor(apiUrl, fetchOptions = {}) {
576
+ this.client = createAPIClient(apiUrl, fetchOptions);
529
577
  this.session = new SessionService(this.client, apiUrl);
530
578
  this.organization = new OrganizationService(this.client);
531
579
  }
@@ -551,5 +599,9 @@ async function validateToken(token, apiUrl) {
551
599
  return false;
552
600
  }
553
601
  }
602
+ function extractTokenPayload(token) {
603
+ const payload = decodeJwt(token);
604
+ return payload;
605
+ }
554
606
 
555
- 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
- "name": "@meistrari/auth-core",
3
- "version": "1.1.0",
4
- "type": "module",
5
- "exports": {
6
- ".": {
7
- "types": "./dist/index.d.ts",
8
- "import": "./dist/index.mjs"
2
+ "name": "@meistrari/auth-core",
3
+ "version": "1.2.0",
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.5"
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
+ }