@coreiam/auth 1.0.11 → 1.0.13

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.
@@ -4,7 +4,7 @@ export interface AuthResult {
4
4
  status: number;
5
5
  data: any;
6
6
  }
7
- export declare function getSession(): Promise<CoreIAMIdentity | null>;
7
+ export declare const getSession: () => Promise<CoreIAMIdentity | null>;
8
8
  export declare function loginActionSdk(identifier: string, password: string): Promise<AuthResult>;
9
9
  export declare function registerActionSdk(userData: any): Promise<AuthResult>;
10
10
  export declare function logoutActionSdk(): Promise<AuthResult>;
@@ -1,11 +1,103 @@
1
1
  "use strict";
2
- // src/nextjs/actions.ts
3
- // REMOVE 'use server' from here.
2
+ // // src/nextjs/actions.ts
3
+ // // REMOVE 'use server' from here.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.getSession = getSession;
5
+ exports.getSession = void 0;
6
6
  exports.loginActionSdk = loginActionSdk;
7
7
  exports.registerActionSdk = registerActionSdk;
8
8
  exports.logoutActionSdk = logoutActionSdk;
9
+ // import { cookies, headers } from 'next/headers';
10
+ // import { CoreIAM, CoreIAMIdentity } from '../core';
11
+ // export interface AuthResult {
12
+ // ok: boolean;
13
+ // status: number;
14
+ // data: any;
15
+ // }
16
+ // let _iam: CoreIAM | null = null;
17
+ // function getIam(): CoreIAM {
18
+ // if (!_iam) _iam = new CoreIAM();
19
+ // return _iam;
20
+ // }
21
+ // async function syncCookiesToNext(response: Response) {
22
+ // const cookieStore = await cookies();
23
+ // const setCookies = response.headers.getSetCookie();
24
+ // for (const cookieStr of setCookies) {
25
+ // const parts = cookieStr.split(';');
26
+ // const [nameValue, ...attributes] = parts;
27
+ // const [name, ...valueParts] = nameValue.split('=');
28
+ // const cookieOptions: any = {};
29
+ // let isDeleting = false;
30
+ // attributes.forEach(attr => {
31
+ // const [attrName, attrValue] = attr.trim().split('=');
32
+ // const lowerName = attrName?.toLowerCase();
33
+ // if (lowerName === 'httponly') cookieOptions.httpOnly = true;
34
+ // if (lowerName === 'secure') cookieOptions.secure = true;
35
+ // if (lowerName === 'path') cookieOptions.path = attrValue || '/';
36
+ // if (lowerName === 'max-age') {
37
+ // const maxAge = parseInt(attrValue, 10);
38
+ // if (maxAge <= 0) isDeleting = true;
39
+ // else cookieOptions.maxAge = maxAge;
40
+ // }
41
+ // if (lowerName === 'expires') {
42
+ // const expiryDate = new Date(attrValue);
43
+ // if (expiryDate.getTime() < Date.now()) isDeleting = true;
44
+ // else cookieOptions.expires = expiryDate;
45
+ // }
46
+ // if (lowerName === 'samesite') cookieOptions.sameSite = attrValue?.toLowerCase() || 'lax';
47
+ // });
48
+ // const cookieName = name.trim();
49
+ // const cookieValue = valueParts.join('=').trim();
50
+ // if (isDeleting || cookieValue === '') {
51
+ // cookieStore.delete(cookieName);
52
+ // } else {
53
+ // cookieStore.set(cookieName, cookieValue, cookieOptions);
54
+ // }
55
+ // }
56
+ // }
57
+ // // This is called by Server Components (e.g., layout.tsx)
58
+ // export async function getSession(): Promise<CoreIAMIdentity | null> {
59
+ // const reqHeaders = await headers();
60
+ // const response = await getIam().proxy('/auth/profile', { method: 'GET' }, reqHeaders);
61
+ // if (!response.ok) return null;
62
+ // const coreIamIdentity = await response.json();
63
+ // if (!coreIamIdentity?.sub) return null;
64
+ // return coreIamIdentity as CoreIAMIdentity;
65
+ // }
66
+ // // These are no longer called by the SDK's provider.tsx,
67
+ // // but can still be used by Qalam's Server Actions if needed.
68
+ // export async function loginActionSdk(identifier: string, password: string): Promise<AuthResult> {
69
+ // const reqHeaders = await headers();
70
+ // try {
71
+ // const response = await getIam().loginWithCsrfProtection(identifier, password, reqHeaders);
72
+ // await syncCookiesToNext(response);
73
+ // return { ok: response.ok, status: response.status, data: await response.json().catch(() => ({})) };
74
+ // } catch (error) {
75
+ // return { ok: false, status: 500, data: { error: error instanceof Error ? error.message : 'Login failed' } };
76
+ // }
77
+ // }
78
+ // export async function registerActionSdk(userData: any): Promise<AuthResult> {
79
+ // const reqHeaders = await headers();
80
+ // try {
81
+ // const response = await getIam().registerWithCsrfProtection(userData, reqHeaders);
82
+ // await syncCookiesToNext(response);
83
+ // return { ok: response.ok, status: response.status, data: await response.json().catch(() => ({})) };
84
+ // } catch (error) {
85
+ // return { ok: false, status: 500, data: { error: error instanceof Error ? error.message : 'Registration failed' } };
86
+ // }
87
+ // }
88
+ // export async function logoutActionSdk(): Promise<AuthResult> {
89
+ // const reqHeaders = await headers();
90
+ // try {
91
+ // const response = await getIam().logout(reqHeaders);
92
+ // await syncCookiesToNext(response);
93
+ // return { ok: response.ok, status: response.status, data: await response.json().catch(() => ({})) };
94
+ // } catch (error) {
95
+ // return { ok: false, status: 500, data: { error: 'Logout failed' } };
96
+ // }
97
+ // }
98
+ // src/nextjs/actions.ts
99
+ // NO 'use server' HERE. This is a module of server-safe functions.
100
+ const react_1 = require("react");
9
101
  const headers_1 = require("next/headers");
10
102
  const core_1 = require("../core");
11
103
  let _iam = null;
@@ -59,8 +151,7 @@ async function syncCookiesToNext(response) {
59
151
  }
60
152
  }
61
153
  }
62
- // This is called by Server Components (e.g., layout.tsx)
63
- async function getSession() {
154
+ exports.getSession = (0, react_1.cache)(async () => {
64
155
  const reqHeaders = await (0, headers_1.headers)();
65
156
  const response = await getIam().proxy('/auth/profile', { method: 'GET' }, reqHeaders);
66
157
  if (!response.ok)
@@ -69,9 +160,7 @@ async function getSession() {
69
160
  if (!coreIamIdentity?.sub)
70
161
  return null;
71
162
  return coreIamIdentity;
72
- }
73
- // These are no longer called by the SDK's provider.tsx,
74
- // but can still be used by Qalam's Server Actions if needed.
163
+ });
75
164
  async function loginActionSdk(identifier, password) {
76
165
  const reqHeaders = await (0, headers_1.headers)();
77
166
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coreiam/auth",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "main": "./dist/core/index.js",
5
5
  "types": "./dist/core/index.d.ts",
6
6
  "exports": {