@coreiam/auth 1.0.6 → 1.0.7
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 +22 -5
- package/package.json +1 -1
package/dist/nextjs/actions.js
CHANGED
|
@@ -8,7 +8,24 @@ exports.logoutActionSdk = logoutActionSdk;
|
|
|
8
8
|
const react_1 = require("react");
|
|
9
9
|
const headers_1 = require("next/headers");
|
|
10
10
|
const core_1 = require("../core");
|
|
11
|
-
const iam = new
|
|
11
|
+
// FIX: lazy singleton instead of `const iam = new CoreIAM();` at module
|
|
12
|
+
// scope. The constructor throws if COREIAM_API_KEY/COREIAM_API_URL are
|
|
13
|
+
// missing — fine on the server, where those env vars exist — but a
|
|
14
|
+
// module-level throw happens the instant the module is evaluated, before
|
|
15
|
+
// any function in it is even called. If this module's 'use server'
|
|
16
|
+
// boundary is ever not recognized correctly by the bundler (see the note
|
|
17
|
+
// on the browser error below), the real module gets included in the client
|
|
18
|
+
// bundle instead of being swapped for action-reference stubs, and that
|
|
19
|
+
// throw fires in the browser instead, where those env vars are never
|
|
20
|
+
// defined. Making this lazy doesn't fix a lost 'use server' boundary by
|
|
21
|
+
// itself, but it does mean import alone can never crash — only an actual
|
|
22
|
+
// invocation can, which is strictly better either way.
|
|
23
|
+
let _iam = null;
|
|
24
|
+
function getIam() {
|
|
25
|
+
if (!_iam)
|
|
26
|
+
_iam = new core_1.CoreIAM();
|
|
27
|
+
return _iam;
|
|
28
|
+
}
|
|
12
29
|
async function syncCookiesToNext(response) {
|
|
13
30
|
const cookieStore = await (0, headers_1.cookies)();
|
|
14
31
|
const setCookies = response.headers.getSetCookie();
|
|
@@ -76,7 +93,7 @@ async function syncCookiesToNext(response) {
|
|
|
76
93
|
// bug to work around.
|
|
77
94
|
exports.getSession = (0, react_1.cache)(async () => {
|
|
78
95
|
const reqHeaders = await (0, headers_1.headers)();
|
|
79
|
-
const response = await
|
|
96
|
+
const response = await getIam().proxy('/auth/profile', { method: 'GET' }, reqHeaders);
|
|
80
97
|
if (!response.ok)
|
|
81
98
|
return null;
|
|
82
99
|
const coreIamIdentity = await response.json();
|
|
@@ -87,7 +104,7 @@ exports.getSession = (0, react_1.cache)(async () => {
|
|
|
87
104
|
async function loginActionSdk(identifier, password) {
|
|
88
105
|
const reqHeaders = await (0, headers_1.headers)();
|
|
89
106
|
try {
|
|
90
|
-
const response = await
|
|
107
|
+
const response = await getIam().loginWithCsrfProtection(identifier, password, reqHeaders);
|
|
91
108
|
await syncCookiesToNext(response);
|
|
92
109
|
return { ok: response.ok, status: response.status, data: await response.json().catch(() => ({})) };
|
|
93
110
|
}
|
|
@@ -98,7 +115,7 @@ async function loginActionSdk(identifier, password) {
|
|
|
98
115
|
async function registerActionSdk(userData) {
|
|
99
116
|
const reqHeaders = await (0, headers_1.headers)();
|
|
100
117
|
try {
|
|
101
|
-
const response = await
|
|
118
|
+
const response = await getIam().registerWithCsrfProtection(userData, reqHeaders);
|
|
102
119
|
await syncCookiesToNext(response);
|
|
103
120
|
return { ok: response.ok, status: response.status, data: await response.json().catch(() => ({})) };
|
|
104
121
|
}
|
|
@@ -109,7 +126,7 @@ async function registerActionSdk(userData) {
|
|
|
109
126
|
async function logoutActionSdk() {
|
|
110
127
|
const reqHeaders = await (0, headers_1.headers)();
|
|
111
128
|
try {
|
|
112
|
-
const response = await
|
|
129
|
+
const response = await getIam().logout(reqHeaders);
|
|
113
130
|
await syncCookiesToNext(response);
|
|
114
131
|
return { ok: response.ok, status: response.status, data: await response.json().catch(() => ({})) };
|
|
115
132
|
}
|