@coreiam/auth 1.0.0 → 1.0.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.
@@ -3,6 +3,13 @@ export interface CoreIAMConfig {
3
3
  apiKey?: string;
4
4
  tenantId?: string;
5
5
  }
6
+ export interface CoreIAMIdentity {
7
+ sub: string;
8
+ email?: string;
9
+ tenantId?: string;
10
+ roles?: string[];
11
+ [key: string]: any;
12
+ }
6
13
  export declare class CoreIAM {
7
14
  private apiKey;
8
15
  private baseUrl;
@@ -1,13 +1,10 @@
1
+ import { CoreIAMIdentity } from '../core';
1
2
  export interface AuthResult {
2
3
  ok: boolean;
3
4
  status: number;
4
5
  data: any;
5
6
  }
6
- /**
7
- * NEW: Securely gets the user session.
8
- * Handles 401 by attempting a silent refresh internally.
9
- */
10
- export declare function getSession(): Promise<any | null>;
7
+ export declare function getSession(): Promise<CoreIAMIdentity | null>;
11
8
  export declare function loginActionSdk(identifier: string, password: string): Promise<AuthResult>;
12
9
  export declare function registerActionSdk(userData: any): Promise<AuthResult>;
13
10
  export declare function logoutActionSdk(): Promise<AuthResult>;
@@ -9,6 +9,8 @@ exports.logoutActionSdk = logoutActionSdk;
9
9
  const headers_1 = require("next/headers");
10
10
  const core_1 = require("../core");
11
11
  const iam = new core_1.CoreIAM();
12
+ // GLOBAL LOCK: Prevents concurrent refresh requests
13
+ let refreshLock = null;
12
14
  async function syncCookiesToNext(response) {
13
15
  const cookieStore = await (0, headers_1.cookies)();
14
16
  const setCookies = response.headers.getSetCookie();
@@ -58,20 +60,22 @@ async function syncCookiesToNext(response) {
58
60
  }
59
61
  }
60
62
  }
61
- /**
62
- * NEW: Securely gets the user session.
63
- * Handles 401 by attempting a silent refresh internally.
64
- */
65
63
  async function getSession() {
66
64
  const reqHeaders = await (0, headers_1.headers)();
67
65
  let response = await iam.proxy('/auth/profile', { method: 'GET' }, reqHeaders);
68
66
  if (response.status === 401) {
69
- const refreshResponse = await iam.refreshTokens(reqHeaders);
70
- if (refreshResponse && refreshResponse.length > 0) {
71
- // Sync the new cookies to the browser
72
- const fakeRes = new Response(null, { headers: { 'set-cookie': refreshResponse.join(', ') } });
67
+ // 1. Use the lock! If another component is already refreshing, wait for it.
68
+ if (!refreshLock) {
69
+ refreshLock = iam.proxy('/auth/refresh-token', { method: 'POST' }, reqHeaders)
70
+ .then(res => res.ok ? res.headers.getSetCookie() : null)
71
+ .finally(() => { refreshLock = null; }); // Clear lock when done
72
+ }
73
+ const setCookies = await refreshLock;
74
+ if (setCookies && setCookies.length > 0) {
75
+ // 2. Sync the new cookies to the browser
76
+ const fakeRes = new Response(null, { headers: { 'set-cookie': setCookies.join(', ') } });
73
77
  await syncCookiesToNext(fakeRes);
74
- // Build new headers with the refreshed cookies and retry
78
+ // 3. Build new headers with the refreshed cookies and retry
75
79
  const cookieStore = await (0, headers_1.cookies)();
76
80
  const newJwt = cookieStore.get('jwt')?.value;
77
81
  const newRefresh = cookieStore.get('refreshToken')?.value;
@@ -82,10 +86,20 @@ async function getSession() {
82
86
  }
83
87
  response = await iam.proxy('/auth/profile', { method: 'GET' }, retryHeaders);
84
88
  }
89
+ else {
90
+ // 4. CRITICAL: If refresh failed, CLEAR THE COOKIES to break the infinite loop!
91
+ const cookieStore = await (0, headers_1.cookies)();
92
+ cookieStore.delete('jwt');
93
+ cookieStore.delete('refreshToken');
94
+ return null;
95
+ }
85
96
  }
86
97
  if (!response.ok)
87
98
  return null;
88
- return response.json();
99
+ const coreIamIdentity = await response.json();
100
+ if (!coreIamIdentity?.sub)
101
+ return null;
102
+ return coreIamIdentity;
89
103
  }
90
104
  async function loginActionSdk(identifier, password) {
91
105
  const reqHeaders = await (0, headers_1.headers)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coreiam/auth",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "./dist/core/index.js",
5
5
  "types": "./dist/core/index.d.ts",
6
6
  "exports": {