@coreiam/auth 1.0.6 → 1.0.8

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.
@@ -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 core_1.CoreIAM();
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 iam.proxy('/auth/profile', { method: 'GET' }, reqHeaders);
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 iam.loginWithCsrfProtection(identifier, password, reqHeaders);
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 iam.registerWithCsrfProtection(userData, reqHeaders);
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 iam.logout(reqHeaders);
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
  }
@@ -16,9 +16,9 @@ const AuthContext = (0, react_1.createContext)({
16
16
  register: async () => ({ error: 'CoreIAMProvider is not mounted' }),
17
17
  refetchIdentity: async () => { },
18
18
  });
19
- function CoreIAMProvider({ children, initialIdentity = null, redirects = {}, }) {
20
- const [identity, setIdentity] = (0, react_1.useState)(initialIdentity);
21
- const [isLoading, setIsLoading] = (0, react_1.useState)(initialIdentity === null);
19
+ function CoreIAMProvider({ children, initialIdentity, redirects = {}, }) {
20
+ const [identity, setIdentity] = (0, react_1.useState)(initialIdentity ?? null);
21
+ const [isLoading, setIsLoading] = (0, react_1.useState)(initialIdentity === undefined);
22
22
  const router = (0, navigation_1.useRouter)();
23
23
  const refetchIdentity = async () => {
24
24
  try {
@@ -33,7 +33,16 @@ function CoreIAMProvider({ children, initialIdentity = null, redirects = {}, })
33
33
  }
34
34
  };
35
35
  (0, react_1.useEffect)(() => {
36
- if (initialIdentity === null) {
36
+ // FIX: was `initialIdentity === null`, which can't tell "SSR ran and
37
+ // confirmed you're logged out" (a correct, final null) apart from "no
38
+ // SSR value was ever provided" (a real undefined). Since layout.tsx
39
+ // always calls getSession() and passes its result — even when that
40
+ // result legitimately is null — every logged-out page view was
41
+ // treated as "no SSR value, fetch again," firing a second, wholly
42
+ // redundant /auth/profile call for something SSR had already
43
+ // determined. undefined (the prop genuinely omitted) is the only case
44
+ // that should trigger this fallback fetch.
45
+ if (initialIdentity === undefined) {
37
46
  refetchIdentity();
38
47
  }
39
48
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coreiam/auth",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "main": "./dist/core/index.js",
5
5
  "types": "./dist/core/index.d.ts",
6
6
  "exports": {