@coreiam/auth 1.0.12 → 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.
- package/dist/nextjs/actions.js +92 -12
- package/package.json +1 -1
package/dist/nextjs/actions.js
CHANGED
|
@@ -1,11 +1,102 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// // src/nextjs/actions.ts
|
|
3
|
+
// // REMOVE 'use server' from here.
|
|
2
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
5
|
exports.getSession = void 0;
|
|
4
6
|
exports.loginActionSdk = loginActionSdk;
|
|
5
7
|
exports.registerActionSdk = registerActionSdk;
|
|
6
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
|
+
// }
|
|
7
98
|
// src/nextjs/actions.ts
|
|
8
|
-
//
|
|
99
|
+
// NO 'use server' HERE. This is a module of server-safe functions.
|
|
9
100
|
const react_1 = require("react");
|
|
10
101
|
const headers_1 = require("next/headers");
|
|
11
102
|
const core_1 = require("../core");
|
|
@@ -60,15 +151,6 @@ async function syncCookiesToNext(response) {
|
|
|
60
151
|
}
|
|
61
152
|
}
|
|
62
153
|
}
|
|
63
|
-
// // This is called by Server Components (e.g., layout.tsx)
|
|
64
|
-
// export async function getSession(): Promise<CoreIAMIdentity | null> {
|
|
65
|
-
// const reqHeaders = await headers();
|
|
66
|
-
// const response = await getIam().proxy('/auth/profile', { method: 'GET' }, reqHeaders);
|
|
67
|
-
// if (!response.ok) return null;
|
|
68
|
-
// const coreIamIdentity = await response.json();
|
|
69
|
-
// if (!coreIamIdentity?.sub) return null;
|
|
70
|
-
// return coreIamIdentity as CoreIAMIdentity;
|
|
71
|
-
// }
|
|
72
154
|
exports.getSession = (0, react_1.cache)(async () => {
|
|
73
155
|
const reqHeaders = await (0, headers_1.headers)();
|
|
74
156
|
const response = await getIam().proxy('/auth/profile', { method: 'GET' }, reqHeaders);
|
|
@@ -79,8 +161,6 @@ exports.getSession = (0, react_1.cache)(async () => {
|
|
|
79
161
|
return null;
|
|
80
162
|
return coreIamIdentity;
|
|
81
163
|
});
|
|
82
|
-
// These are no longer called by the SDK's provider.tsx,
|
|
83
|
-
// but can still be used by Qalam's Server Actions if needed.
|
|
84
164
|
async function loginActionSdk(identifier, password) {
|
|
85
165
|
const reqHeaders = await (0, headers_1.headers)();
|
|
86
166
|
try {
|