@civic/auth 0.0.1-beta.17 → 0.0.1-beta.19

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.
Files changed (67) hide show
  1. package/dist/AuthProvider-Bj_Prt1x.d.ts +21 -0
  2. package/dist/AuthProvider-DUAoX4G9.d.mts +21 -0
  3. package/dist/{index-DTimUlkB.d.ts → UserProvider-CMLaYOUD.d.ts} +1 -2
  4. package/dist/{index-DvjkKpkk.d.mts → UserProvider-Cbm8MZkJ.d.mts} +1 -2
  5. package/dist/chunk-5UQQYXCX.js +1 -0
  6. package/dist/chunk-5UQQYXCX.js.map +1 -0
  7. package/dist/chunk-BFESCRFK.mjs +118 -0
  8. package/dist/chunk-BFESCRFK.mjs.map +1 -0
  9. package/dist/{chunk-G3P5TIO2.mjs → chunk-CBQ3HKRV.mjs} +123 -232
  10. package/dist/chunk-CBQ3HKRV.mjs.map +1 -0
  11. package/dist/{chunk-SEKF2WZX.js → chunk-CZ3AVCKD.js} +16 -71
  12. package/dist/chunk-CZ3AVCKD.js.map +1 -0
  13. package/dist/chunk-DJFTZS4P.js +118 -0
  14. package/dist/chunk-DJFTZS4P.js.map +1 -0
  15. package/dist/chunk-HTTTZ2BP.mjs +223 -0
  16. package/dist/chunk-HTTTZ2BP.mjs.map +1 -0
  17. package/dist/{chunk-RF23Q4V6.js → chunk-O2SODTR3.js} +114 -223
  18. package/dist/chunk-O2SODTR3.js.map +1 -0
  19. package/dist/chunk-O6DPCPRH.js +223 -0
  20. package/dist/chunk-O6DPCPRH.js.map +1 -0
  21. package/dist/chunk-PMJAV4JJ.mjs +1 -0
  22. package/dist/chunk-PMJAV4JJ.mjs.map +1 -0
  23. package/dist/chunk-UADVRCHY.mjs +710 -0
  24. package/dist/chunk-UADVRCHY.mjs.map +1 -0
  25. package/dist/chunk-VJVRFKDH.js +710 -0
  26. package/dist/chunk-VJVRFKDH.js.map +1 -0
  27. package/dist/{chunk-5XL2ST72.mjs → chunk-X3FQBE22.mjs} +15 -70
  28. package/dist/chunk-X3FQBE22.mjs.map +1 -0
  29. package/dist/index.d.mts +2 -2
  30. package/dist/index.d.ts +2 -2
  31. package/dist/index.js +2 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/index.mjs +1 -0
  34. package/dist/nextjs/client.css +335 -0
  35. package/dist/nextjs/client.css.map +1 -0
  36. package/dist/nextjs/client.d.mts +12 -0
  37. package/dist/nextjs/client.d.ts +12 -0
  38. package/dist/nextjs/client.js +179 -0
  39. package/dist/nextjs/client.js.map +1 -0
  40. package/dist/nextjs/client.mjs +179 -0
  41. package/dist/nextjs/client.mjs.map +1 -0
  42. package/dist/nextjs.d.mts +35 -7
  43. package/dist/nextjs.d.ts +35 -7
  44. package/dist/nextjs.js +130 -43
  45. package/dist/nextjs.js.map +1 -1
  46. package/dist/nextjs.mjs +117 -30
  47. package/dist/nextjs.mjs.map +1 -1
  48. package/dist/react.d.mts +7 -31
  49. package/dist/react.d.ts +7 -31
  50. package/dist/react.js +15 -835
  51. package/dist/react.js.map +1 -1
  52. package/dist/react.mjs +47 -867
  53. package/dist/react.mjs.map +1 -1
  54. package/dist/server.d.mts +3 -24
  55. package/dist/server.d.ts +3 -24
  56. package/dist/server.js +4 -2
  57. package/dist/server.js.map +1 -1
  58. package/dist/server.mjs +4 -2
  59. package/dist/storage-B2eAQNdv.d.ts +25 -0
  60. package/dist/storage-BJPUpxhm.d.mts +25 -0
  61. package/dist/{types-b4c1koXj.d.mts → types-Bqm9OCZN.d.mts} +5 -2
  62. package/dist/{types-b4c1koXj.d.ts → types-Bqm9OCZN.d.ts} +5 -2
  63. package/package.json +11 -2
  64. package/dist/chunk-5XL2ST72.mjs.map +0 -1
  65. package/dist/chunk-G3P5TIO2.mjs.map +0 -1
  66. package/dist/chunk-RF23Q4V6.js.map +0 -1
  67. package/dist/chunk-SEKF2WZX.js.map +0 -1
package/dist/react.js CHANGED
@@ -2,23 +2,21 @@
2
2
 
3
3
 
4
4
 
5
- var _chunkSEKF2WZXjs = require('./chunk-SEKF2WZX.js');
6
5
 
7
6
 
8
7
 
9
8
 
10
9
 
11
10
 
11
+ var _chunkVJVRFKDHjs = require('./chunk-VJVRFKDH.js');
12
12
 
13
13
 
14
+ var _chunkCZ3AVCKDjs = require('./chunk-CZ3AVCKD.js');
14
15
 
15
16
 
16
17
 
17
18
 
18
-
19
-
20
-
21
- var _chunkRF23Q4V6js = require('./chunk-RF23Q4V6.js');
19
+ var _chunkO2SODTR3js = require('./chunk-O2SODTR3.js');
22
20
 
23
21
 
24
22
 
@@ -29,846 +27,31 @@ var _chunkCRTRMMJ7js = require('./chunk-CRTRMMJ7.js');
29
27
  // src/react/hooks/useUser.tsx
30
28
  var _react = require('react');
31
29
 
32
- // src/shared/UserProvider.tsx
33
-
34
- var _reactquery = require('@tanstack/react-query');
35
-
36
- // src/react/hooks/useAuth.tsx
37
-
38
-
39
- // src/shared/AuthContext.tsx
40
-
41
- var AuthContext = _react.createContext.call(void 0, null);
42
-
43
- // src/react/hooks/useAuth.tsx
44
- var useAuth = () => {
45
- const context = _react.useContext.call(void 0, AuthContext);
46
- if (!context) {
47
- throw new Error("useAuth must be used within an AuthProvider");
48
- }
49
- return context;
50
- };
51
-
52
- // src/react/hooks/useToken.tsx
53
-
54
-
55
- // src/react/providers/TokenProvider.tsx
56
-
57
-
58
-
59
- // src/react/hooks/useSession.tsx
60
-
61
-
62
- // src/react/providers/SessionProvider.tsx
63
-
64
- var _jsxruntime = require('react/jsx-runtime');
65
- var defaultSession = {
66
- authenticated: false,
67
- idToken: void 0,
68
- accessToken: void 0,
69
- displayMode: "iframe"
70
- };
71
- var SessionContext = _react.createContext.call(void 0, defaultSession);
72
- var SessionProvider = ({ children, session }) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, SessionContext.Provider, { value: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultSession), session || {}), children });
73
-
74
- // src/react/hooks/useSession.tsx
75
- var useSession = () => {
76
- const context = _react.useContext.call(void 0, SessionContext);
77
- if (!context) {
78
- throw new Error("useSession must be used within an SessionProvider");
79
- }
80
- return context;
81
- };
82
-
83
- // src/react/providers/TokenProvider.tsx
84
- var _jwt = require('oslo/jwt');
85
-
86
- var TokenContext = _react.createContext.call(void 0, void 0);
87
- var TokenProvider = ({ children }) => {
88
- const { isLoading, error: authError } = useAuth();
89
- const session = useSession();
90
- const queryClient3 = _reactquery.useQueryClient.call(void 0, );
91
- const refreshTokenMutation = _reactquery.useMutation.call(void 0, {
92
- mutationFn: () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
93
- throw new Error("Method not implemented.");
94
- }),
95
- onSuccess: () => {
96
- queryClient3.invalidateQueries({ queryKey: ["session"] });
97
- }
98
- });
99
- const decodeTokens = _react.useMemo.call(void 0, () => {
100
- if (!(session == null ? void 0 : session.idToken)) return null;
101
- const parsedJWT = _jwt.parseJWT.call(void 0, session.idToken);
102
- if (!parsedJWT) return null;
103
- const { forwardedTokens } = parsedJWT.payload;
104
- return forwardedTokens ? _chunkRF23Q4V6js.convertForwardedTokenFormat.call(void 0, forwardedTokens) : null;
105
- }, [session == null ? void 0 : session.idToken]);
106
- const value = _react.useMemo.call(void 0,
107
- () => ({
108
- accessToken: session.accessToken || null,
109
- idToken: session.idToken || null,
110
- forwardedTokens: decodeTokens || {},
111
- refreshToken: refreshTokenMutation.mutateAsync,
112
- isLoading,
113
- error: authError || refreshTokenMutation.error
114
- }),
115
- [
116
- session.accessToken,
117
- session.idToken,
118
- decodeTokens,
119
- refreshTokenMutation.mutateAsync,
120
- refreshTokenMutation.error,
121
- isLoading,
122
- authError
123
- ]
124
- );
125
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenContext.Provider, { value, children });
126
- };
127
-
128
- // src/react/hooks/useToken.tsx
129
- var useToken = () => {
130
- const context = _react.useContext.call(void 0, TokenContext);
131
- if (!context) {
132
- throw new Error("useToken must be used within a TokenProvider");
133
- }
134
- return context;
135
- };
136
-
137
- // src/shared/UserProvider.tsx
138
-
139
- var UserContext = _react.createContext.call(void 0, null);
140
- var UserProvider = ({
141
- children,
142
- storage,
143
- user: inputUser,
144
- signOut: inputSignOut
145
- }) => {
146
- var _a;
147
- const { isLoading: authLoading, error: authError } = useAuth();
148
- const session = useSession();
149
- const { accessToken } = useToken();
150
- const { signIn, signOut } = useAuth();
151
- const fetchUser = () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
152
- if (!accessToken) {
153
- return null;
154
- }
155
- const userSession = new (0, _chunkRF23Q4V6js.GenericUserSession)(storage);
156
- return userSession.get();
157
- });
158
- const {
159
- data: user,
160
- isLoading: userLoading,
161
- error: userError
162
- } = _reactquery.useQuery.call(void 0, {
163
- queryKey: ["user", session == null ? void 0 : session.idToken],
164
- queryFn: fetchUser,
165
- enabled: !!(session == null ? void 0 : session.idToken)
166
- // Only run the query if we have an access token
167
- });
168
- const isLoading = authLoading || userLoading;
169
- const error = authError || userError;
170
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
171
- UserContext.Provider,
172
- {
173
- value: {
174
- user: (_a = inputUser || user) != null ? _a : null,
175
- isLoading,
176
- error,
177
- signIn,
178
- signOut: inputSignOut || signOut
179
- },
180
- children
181
- }
182
- );
183
- };
184
-
185
- // src/shared/AuthProvider.tsx
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
- // src/react/components/CivicAuthIframeContainer.tsx
196
-
197
-
198
- // src/react/components/LoadingIcon.tsx
199
-
200
- var LoadingIcon = () => /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { role: "status", children: [
201
- /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
202
- "svg",
203
- {
204
- "aria-hidden": "true",
205
- className: "cac-inline cac-h-8 cac-w-8 cac-animate-spin cac-fill-neutral-600 cac-text-neutral-200 dark:cac-fill-neutral-300 dark:cac-text-neutral-600",
206
- viewBox: "0 0 100 101",
207
- fill: "none",
208
- xmlns: "http://www.w3.org/2000/svg",
209
- children: [
210
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
211
- "path",
212
- {
213
- d: "M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z",
214
- fill: "currentColor"
215
- }
216
- ),
217
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
218
- "path",
219
- {
220
- d: "M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z",
221
- fill: "currentFill"
222
- }
223
- )
224
- ]
225
- }
226
- ),
227
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "cac-sr-only", children: "Loading..." })
228
- ] });
229
-
230
- // src/react/components/CloseIcon.tsx
231
-
232
- var CloseIcon = () => /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
233
- "svg",
234
- {
235
- xmlns: "http://www.w3.org/2000/svg",
236
- width: "24",
237
- height: "24",
238
- viewBox: "0 0 24 24",
239
- fill: "none",
240
- stroke: "currentColor",
241
- strokeWidth: "2",
242
- strokeLinecap: "round",
243
- strokeLinejoin: "round",
244
- className: "lucide lucide-x",
245
- children: [
246
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "path", { d: "M18 6 6 18" }),
247
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "path", { d: "m6 6 12 12" })
248
- ]
249
- }
250
- );
251
-
252
- // src/react/components/CivicAuthIframe.tsx
253
-
254
-
255
- var CivicAuthIframe = _react.forwardRef.call(void 0,
256
- ({ onLoad }, ref) => {
257
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
258
- "iframe",
259
- {
260
- id: _chunkRF23Q4V6js.IFRAME_ID,
261
- ref,
262
- className: "cac-h-[26rem] cac-w-80 cac-border-none",
263
- onLoad
264
- }
265
- );
266
- }
267
- );
268
- CivicAuthIframe.displayName = "CivicAuthIframe";
269
-
270
- // src/react/components/CivicAuthIframeContainer.tsx
271
-
272
- function NoChrome({
273
- children
274
- }) {
275
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-relative", children });
276
- }
277
- function IframeChrome({
278
- children,
279
- onClose
280
- }) {
281
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
282
- "div",
283
- {
284
- className: "cac-absolute cac-left-0 cac-top-0 cac-z-50 cac-flex cac-h-screen cac-w-screen cac-items-center cac-justify-center cac-bg-neutral-950 cac-bg-opacity-50",
285
- onClick: onClose,
286
- children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
287
- "div",
288
- {
289
- className: "cac-relative cac-rounded-3xl cac-bg-white cac-p-6 cac-shadow-lg",
290
- onClick: (e) => e.stopPropagation(),
291
- children: [
292
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
293
- "button",
294
- {
295
- className: "cac-absolute cac-right-4 cac-top-4 cac-flex cac-cursor-pointer cac-items-center cac-justify-center cac-border-none cac-bg-transparent cac-p-1 cac-text-neutral-400",
296
- onClick: onClose,
297
- children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, CloseIcon, {})
298
- }
299
- ),
300
- children
301
- ]
302
- }
303
- )
304
- }
305
- );
306
- }
307
- var CivicAuthIframeContainer = ({
308
- onClose,
309
- closeOnRedirect = true
310
- }) => {
311
- var _a;
312
- const [isLoading, setIsLoading] = _react.useState.call(void 0, true);
313
- const { isLoading: isAuthLoading } = useAuth();
314
- const config = useConfig();
315
- const { setAuthResponseUrl, iframeRef } = useIframe();
316
- const processIframeUrl = _react.useCallback.call(void 0, () => {
317
- if (iframeRef && iframeRef.current && iframeRef.current.contentWindow) {
318
- try {
319
- const iframeUrl = iframeRef.current.contentWindow.location.href;
320
- if (iframeUrl.startsWith(config.redirectUrl)) {
321
- setIsLoading(true);
322
- const iframeBody = iframeRef.current.contentWindow.document.body.innerHTML;
323
- if (iframeBody.includes(_chunkRF23Q4V6js.TOKEN_EXCHANGE_TRIGGER_TEXT)) {
324
- console.log(
325
- `${_chunkRF23Q4V6js.TOKEN_EXCHANGE_TRIGGER_TEXT}, calling callback URL again...`
326
- );
327
- const params = new URL(iframeUrl).searchParams;
328
- fetch(`${config.redirectUrl}?${params.toString()}`);
329
- } else {
330
- setAuthResponseUrl(iframeUrl);
331
- }
332
- if (closeOnRedirect) onClose == null ? void 0 : onClose();
333
- return true;
334
- }
335
- } catch (e) {
336
- console.log("Waiting for redirect...");
337
- }
338
- }
339
- return false;
340
- }, [
341
- closeOnRedirect,
342
- config.redirectUrl,
343
- iframeRef,
344
- onClose,
345
- setAuthResponseUrl
346
- ]);
347
- const intervalId = _react.useRef.call(void 0, );
348
- const handleEscape = _react.useCallback.call(void 0,
349
- (event) => {
350
- if (event.key === "Escape") {
351
- onClose == null ? void 0 : onClose();
352
- }
353
- },
354
- [onClose]
355
- );
356
- _react.useEffect.call(void 0, () => {
357
- window.addEventListener("keydown", handleEscape);
358
- return () => window.removeEventListener("keydown", handleEscape);
359
- });
360
- const handleIframeLoad = () => {
361
- setIsLoading(false);
362
- console.log("handleIframeLoad");
363
- if (processIframeUrl() && intervalId.current) {
364
- clearInterval(intervalId.current);
365
- }
366
- };
367
- const showLoadingIcon = isLoading || isAuthLoading || !((_a = iframeRef == null ? void 0 : iframeRef.current) == null ? void 0 : _a.getAttribute("src"));
368
- const WrapperComponent = config.modalIframe ? IframeChrome : NoChrome;
369
- return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, WrapperComponent, { onClose, children: [
370
- showLoadingIcon && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-absolute cac-inset-0 cac-flex cac-items-center cac-justify-center cac-rounded-3xl cac-bg-white", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LoadingIcon, {}) }),
371
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, CivicAuthIframe, { ref: iframeRef, onLoad: handleIframeLoad })
372
- ] });
373
- };
374
-
375
- // src/config.ts
376
- var authConfig = {
377
- // TODO change this to the production URL once we're out of beta
378
- oauthServer: "https://auth-dev.civic.com/oauth/"
379
- };
380
-
381
- // src/react/providers/ConfigProvider.tsx
382
-
383
-
384
- var defaultConfig = {
385
- config: authConfig,
386
- redirectUrl: "",
387
- modalIframe: true,
388
- serverTokenExchange: false
389
- };
390
- var ConfigContext = _react.createContext.call(void 0, defaultConfig);
391
- var ConfigProvider = ({
392
- children,
393
- config,
394
- redirectUrl,
395
- modalIframe,
396
- serverTokenExchange
397
- }) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
398
- ConfigContext.Provider,
399
- {
400
- value: {
401
- config,
402
- redirectUrl,
403
- modalIframe: !!modalIframe,
404
- serverTokenExchange
405
- },
406
- children
407
- }
408
- );
409
-
410
- // src/react/providers/IframeProvider.tsx
411
-
412
-
413
-
414
-
415
- var defaultIframe = {
416
- iframeRef: null,
417
- setAuthResponseUrl: () => {
418
- }
419
- };
420
- var IframeContext = _react.createContext.call(void 0, defaultIframe);
421
- var IframeProvider = ({
422
- children,
423
- iframeRef,
424
- setAuthResponseUrl
425
- }) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, IframeContext.Provider, { value: { iframeRef, setAuthResponseUrl }, children });
426
-
427
- // src/shared/AuthProvider.tsx
428
-
429
- var globalThisObject;
430
- if (typeof window !== "undefined") {
431
- globalThisObject = window;
432
- } else if (typeof global !== "undefined") {
433
- globalThisObject = global;
434
- } else {
435
- globalThisObject = Function("return this")();
436
- }
437
- globalThisObject.globalThis = globalThisObject;
438
- function BlockDisplay({ children }) {
439
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-relative cac-left-0 cac-top-0 cac-z-50 cac-flex cac-h-screen cac-w-screen cac-items-center cac-justify-center cac-bg-white", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-absolute cac-inset-0 cac-flex cac-items-center cac-justify-center cac-bg-white", children }) });
440
- }
441
- var AuthProvider = ({
442
- children,
443
- clientId,
444
- redirectUrl: inputRedirectUrl,
445
- config = authConfig,
446
- onSignIn,
447
- onSignOut,
448
- pkceConsumer,
449
- nonce,
450
- modalIframe = true,
451
- sessionData: inputSessionData
452
- }) => {
453
- const [iframeUrl, setIframeUrl] = _react.useState.call(void 0, null);
454
- const [currentUrl, setCurrentUrl] = _react.useState.call(void 0, null);
455
- const [isInIframe, setIsInIframe] = _react.useState.call(void 0, false);
456
- const [authResponseUrl, setAuthResponseUrl] = _react.useState.call(void 0, null);
457
- const [tokenExchangeError, setTokenExchangeError] = _react.useState.call(void 0, );
458
- const [displayMode, setDisplayMode] = _react.useState.call(void 0, "iframe");
459
- const [browserAuthenticationInitiator, setBrowserAuthenticationInitiator] = _react.useState.call(void 0, );
460
- const [showIFrame, setShowIFrame] = _react.useState.call(void 0, false);
461
- const [isRedirecting, setIsRedirecting] = _react.useState.call(void 0, false);
462
- const queryClient3 = _reactquery.useQueryClient.call(void 0, );
463
- const iframeRef = _react.useRef.call(void 0, null);
464
- const serverTokenExchange = pkceConsumer instanceof _chunkRF23Q4V6js.ConfidentialClientPKCEConsumer;
465
- _react.useEffect.call(void 0, () => {
466
- if (typeof globalThis.window !== "undefined") {
467
- setCurrentUrl(globalThis.window.location.href);
468
- const isInIframeVal = _chunkRF23Q4V6js.isWindowInIframe.call(void 0, globalThis.window);
469
- setIsInIframe(isInIframeVal);
470
- }
471
- }, []);
472
- const redirectUrl = _react.useMemo.call(void 0,
473
- () => (inputRedirectUrl || currentUrl || "").split("?")[0],
474
- [currentUrl, inputRedirectUrl]
475
- );
476
- const [authService, setAuthService] = _react.useState.call(void 0, );
477
- _react.useEffect.call(void 0, () => {
478
- if (!currentUrl) return;
479
- _chunkRF23Q4V6js.BrowserAuthenticationService.build({
480
- clientId,
481
- redirectUrl,
482
- oauthServer: config.oauthServer,
483
- scopes: _chunkRF23Q4V6js.DEFAULT_SCOPES,
484
- displayMode
485
- }).then(setAuthService);
486
- }, [currentUrl, clientId, redirectUrl, config, displayMode]);
487
- const {
488
- data: session,
489
- isLoading,
490
- error
491
- } = _reactquery.useQuery.call(void 0, {
492
- queryKey: [
493
- "session",
494
- authResponseUrl,
495
- iframeUrl,
496
- currentUrl,
497
- isInIframe,
498
- authService
499
- ],
500
- queryFn: () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
501
- if (!authService) {
502
- return { authenticated: false };
503
- }
504
- if (inputSessionData) {
505
- return inputSessionData;
506
- }
507
- const url = new URL(
508
- authResponseUrl ? authResponseUrl : globalThis.window.location.href || ""
509
- );
510
- const existingSessionData = yield authService.validateExistingSession();
511
- if (existingSessionData.authenticated) {
512
- return existingSessionData;
513
- }
514
- const code = url.searchParams.get("code");
515
- const state = url.searchParams.get("state");
516
- if (!serverTokenExchange && code && state && !isInIframe) {
517
- try {
518
- console.log("AuthProvider useQuery code", {
519
- isInIframe,
520
- code,
521
- state
522
- });
523
- yield authService.tokenExchange(code, state);
524
- const clientStorage = new (0, _chunkRF23Q4V6js.LocalStorageAdapter)();
525
- const user = yield _chunkRF23Q4V6js.getUser.call(void 0, clientStorage);
526
- if (!user) {
527
- throw new Error("Failed to get user info");
528
- }
529
- const userSession = new (0, _chunkRF23Q4V6js.GenericUserSession)(clientStorage);
530
- userSession.set(user);
531
- onSignIn == null ? void 0 : onSignIn();
532
- return authService.getSessionData();
533
- } catch (error2) {
534
- setTokenExchangeError(error2);
535
- onSignIn == null ? void 0 : onSignIn(
536
- error2 instanceof Error ? error2 : new Error("Failed to sign in")
537
- );
538
- return { authenticated: false };
539
- }
540
- }
541
- return existingSessionData;
542
- })
543
- });
544
- const signOutMutation = _reactquery.useMutation.call(void 0, {
545
- mutationFn: () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
546
- const authInitiator = getAuthInitiator();
547
- authInitiator == null ? void 0 : authInitiator.signOut();
548
- setIframeUrl(null);
549
- setShowIFrame(false);
550
- setAuthResponseUrl(null);
551
- onSignOut == null ? void 0 : onSignOut();
552
- }),
553
- onSuccess: () => {
554
- queryClient3.setQueryData(
555
- [
556
- "session",
557
- authResponseUrl,
558
- iframeUrl,
559
- currentUrl,
560
- isInIframe,
561
- authService
562
- ],
563
- null
564
- );
565
- }
566
- });
567
- const getAuthInitiator = _react.useCallback.call(void 0,
568
- (overrideDisplayMode) => {
569
- const useDisplayMode = overrideDisplayMode || displayMode;
570
- if (!pkceConsumer) {
571
- return null;
572
- }
573
- return browserAuthenticationInitiator || new (0, _chunkRF23Q4V6js.BrowserAuthenticationInitiator)({
574
- pkceConsumer,
575
- // generate and retrieve the challenge client-side
576
- clientId,
577
- redirectUrl,
578
- state: _chunkRF23Q4V6js.generateState.call(void 0, useDisplayMode),
579
- scopes: _chunkRF23Q4V6js.DEFAULT_SCOPES,
580
- displayMode: useDisplayMode,
581
- oauthServer: config.oauthServer,
582
- // the endpoints to use for the login (if not obtained from the auth server
583
- endpointOverrides: config.endpoints,
584
- nonce
585
- });
586
- },
587
- [
588
- displayMode,
589
- browserAuthenticationInitiator,
590
- clientId,
591
- redirectUrl,
592
- config.oauthServer,
593
- config.endpoints,
594
- pkceConsumer,
595
- nonce
596
- ]
597
- );
598
- const signIn = _react.useCallback.call(void 0,
599
- (overrideDisplayMode = "iframe") => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
600
- setDisplayMode(overrideDisplayMode);
601
- const authInitiator = getAuthInitiator(overrideDisplayMode);
602
- setBrowserAuthenticationInitiator(authInitiator);
603
- if (overrideDisplayMode === "iframe") {
604
- setShowIFrame(true);
605
- } else if (overrideDisplayMode === "redirect") {
606
- setIsRedirecting(true);
607
- }
608
- authInitiator == null ? void 0 : authInitiator.signIn(iframeRef.current);
609
- }),
610
- [getAuthInitiator]
611
- );
612
- const isAuthenticated = _react.useMemo.call(void 0,
613
- () => session ? session.authenticated : false,
614
- [session]
615
- );
616
- _reactquery.useQuery.call(void 0, {
617
- queryKey: ["autoSignIn", modalIframe, redirectUrl, isAuthenticated],
618
- queryFn: () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
619
- if (!modalIframe && redirectUrl && !isAuthenticated && iframeRef.current) {
620
- signIn("iframe");
621
- }
622
- return true;
623
- }),
624
- refetchOnWindowFocus: false
625
- });
626
- const value = _react.useMemo.call(void 0,
627
- () => ({
628
- isLoading,
629
- error,
630
- signOut: () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
631
- yield signOutMutation.mutateAsync();
632
- }),
633
- isAuthenticated,
634
- signIn
635
- }),
636
- [isLoading, error, signOutMutation, isAuthenticated, signIn]
637
- );
638
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, AuthContext.Provider, { value, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
639
- ConfigProvider,
640
- {
641
- config,
642
- redirectUrl,
643
- modalIframe,
644
- serverTokenExchange,
645
- children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
646
- IframeProvider,
647
- {
648
- setAuthResponseUrl,
649
- iframeRef,
650
- children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, SessionProvider, { session, children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, TokenProvider, { children: [
651
- modalIframe && !isInIframe && !(session == null ? void 0 : session.authenticated) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
652
- "div",
653
- {
654
- style: showIFrame ? { display: "block" } : { display: "none" },
655
- children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
656
- CivicAuthIframeContainer,
657
- {
658
- onClose: () => setShowIFrame(false)
659
- }
660
- )
661
- }
662
- ),
663
- modalIframe && (isInIframe || isRedirecting || isLoading && !serverTokenExchange) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, BlockDisplay, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LoadingIcon, {}) }),
664
- (tokenExchangeError || error) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, BlockDisplay, { children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { children: [
665
- "Error: ",
666
- (tokenExchangeError || error).message
667
- ] }) }),
668
- children
669
- ] }) })
670
- }
671
- )
672
- }
673
- ) });
674
- };
675
-
676
30
  // src/shared/CivicAuthProvider.tsx
677
-
31
+ var _reactquery = require('@tanstack/react-query');
678
32
  require('@civic/auth/styles.css');
679
-
33
+ var _jsxruntime = require('react/jsx-runtime');
680
34
  var queryClient = new (0, _reactquery.QueryClient)();
681
35
  var CivicAuthProvider = (_a) => {
682
36
  var _b = _a, { children } = _b, props = _chunkCRTRMMJ7js.__objRest.call(void 0, _b, ["children"]);
683
37
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _reactquery.QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
684
- AuthProvider,
685
- _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, props), {
686
- pkceConsumer: new (0, _chunkRF23Q4V6js.BrowserPublicClientPKCEProducer)(),
687
- children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, UserProvider, { storage: new (0, _chunkRF23Q4V6js.LocalStorageAdapter)(), children })
688
- })
689
- ) });
690
- };
691
-
692
- // src/react/providers/NextAuthProvider.tsx
693
-
694
-
695
-
696
-
697
- // src/react/hooks/useTokenCookie.ts
698
-
699
- var _navigationjs = require('next/navigation.js');
700
-
701
-
702
- // src/lib/cookies.ts
703
- var getWindowCookieValue = ({
704
- key,
705
- window: window2,
706
- parseJson = false
707
- }) => {
708
- const cookie = window2.document.cookie;
709
- if (!cookie) return null;
710
- const cookies = cookie.split(";");
711
- for (const c of cookies) {
712
- const [name, value] = c.trim().split("=");
713
- if (value && name === key) {
714
- try {
715
- const decodeURIComponentValue = decodeURIComponent(value);
716
- return parseJson === true ? JSON.parse(decodeURIComponentValue) : decodeURIComponentValue;
717
- } catch (e) {
718
- return value;
719
- }
720
- }
721
- }
722
- return null;
723
- };
724
-
725
- // src/react/hooks/useTokenCookie.ts
726
- var getTokenFromCookie = (tokenName) => {
727
- return getWindowCookieValue({
728
- key: tokenName,
729
- window: globalThis.window,
730
- parseJson: false
731
- });
732
- };
733
- var useTokenCookie = (tokenName) => {
734
- const hasRunRef = _react.useRef.call(void 0, false);
735
- const router = _navigationjs.useRouter.call(void 0, );
736
- const { data: token } = _reactquery.useQuery.call(void 0, {
737
- queryKey: ["token", tokenName],
738
- queryFn: () => getTokenFromCookie(tokenName) || null,
739
- refetchInterval: 2e3,
740
- refetchIntervalInBackground: true,
741
- enabled: !hasRunRef.current,
742
- refetchOnWindowFocus: true
743
- });
744
- _react.useEffect.call(void 0, () => {
745
- if (token) {
746
- if (!hasRunRef.current) {
747
- hasRunRef.current = true;
748
- router.refresh();
749
- }
750
- } else {
751
- hasRunRef.current = false;
752
- }
753
- }, [token, router]);
754
- return token != null ? token : null;
755
- };
756
-
757
- // src/react/providers/NextAuthProvider.tsx
758
-
759
- var queryClient2 = new (0, _reactquery.QueryClient)();
760
- var CivicNextAuthProvider = (_a) => {
761
- var _b = _a, {
762
- children
763
- } = _b, props = _chunkCRTRMMJ7js.__objRest.call(void 0, _b, [
764
- "children"
765
- ]);
766
- const [redirectUrl, setRedirectUrl] = _react.useState.call(void 0, "");
767
- const { clientId, oauthServer, callbackUrl, challengeUrl, logoutUrl } = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, );
768
- _react.useEffect.call(void 0, () => {
769
- if (typeof globalThis.window !== "undefined") {
770
- const currentUrl = globalThis.window.location.href;
771
- setRedirectUrl(_chunkSEKF2WZXjs.resolveCallbackUrl.call(void 0, _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, ), currentUrl));
772
- }
773
- }, [callbackUrl]);
774
- const user = useUserCookie();
775
- const idToken = useTokenCookie("id_token" /* ID_TOKEN */);
776
- const combinedUser = user ? _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, user || {}), { idToken }) : null;
777
- const sessionData = _chunkCRTRMMJ7js.__spreadValues.call(void 0, {
778
- authenticated: !!user
779
- }, idToken ? { idToken } : {});
780
- const signOut = () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
781
- var _a2;
782
- (_a2 = props.onSignOut) == null ? void 0 : _a2.call(props);
783
- window.location.href = logoutUrl;
784
- return;
785
- });
786
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _reactquery.QueryClientProvider, { client: queryClient2, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
787
- AuthProvider,
38
+ _chunkVJVRFKDHjs.AuthProvider,
788
39
  _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, props), {
789
- redirectUrl,
790
- config: { oauthServer },
791
- clientId,
792
- pkceConsumer: new (0, _chunkRF23Q4V6js.ConfidentialClientPKCEConsumer)(challengeUrl),
793
- sessionData,
794
- children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
795
- UserProvider,
796
- {
797
- storage: new (0, _chunkSEKF2WZXjs.NextjsClientStorage)(),
798
- user: combinedUser,
799
- signOut,
800
- children
801
- }
802
- )
40
+ pkceConsumer: new (0, _chunkO2SODTR3js.BrowserPublicClientPKCEProducer)(),
41
+ children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkVJVRFKDHjs.UserProvider, { storage: new (0, _chunkO2SODTR3js.LocalStorageAdapter)(), children })
803
42
  })
804
43
  ) });
805
44
  };
806
45
 
807
46
  // src/react/hooks/useUser.tsx
808
47
  var useUser = () => {
809
- const context = _react.useContext.call(void 0, UserContext);
48
+ const context = _react.useContext.call(void 0, _chunkVJVRFKDHjs.UserContext);
810
49
  if (!context) {
811
50
  throw new Error("useUser must be used within a UserProvider");
812
51
  }
813
52
  return context;
814
53
  };
815
54
 
816
- // src/react/hooks/useUserCookie.ts
817
-
818
-
819
-
820
- var getUserFromCookie = () => {
821
- const userCookie = getWindowCookieValue({
822
- key: "user" /* USER */,
823
- window: globalThis.window,
824
- parseJson: true
825
- });
826
- return userCookie;
827
- };
828
- var useUserCookie = () => {
829
- const hasRunRef = _react.useRef.call(void 0, false);
830
- const router = _navigationjs.useRouter.call(void 0, );
831
- const { data: user } = _reactquery.useQuery.call(void 0, {
832
- queryKey: ["user"],
833
- queryFn: () => getUserFromCookie(),
834
- refetchInterval: 2e3,
835
- refetchIntervalInBackground: true,
836
- enabled: !hasRunRef.current,
837
- refetchOnWindowFocus: true
838
- });
839
- _react.useEffect.call(void 0, () => {
840
- if (user) {
841
- if (!hasRunRef.current) {
842
- hasRunRef.current = true;
843
- router.refresh();
844
- }
845
- } else {
846
- hasRunRef.current = false;
847
- }
848
- }, [user, router]);
849
- return user != null ? user : null;
850
- };
851
-
852
- // src/react/hooks/useConfig.tsx
853
-
854
- var useConfig = () => {
855
- const context = _react.useContext.call(void 0, ConfigContext);
856
- if (!context) {
857
- throw new Error("useConfig must be used within an ConfigProvider");
858
- }
859
- return context;
860
- };
861
-
862
- // src/react/hooks/useIframe.tsx
863
-
864
- var useIframe = () => {
865
- const context = _react.useContext.call(void 0, IframeContext);
866
- if (!context) {
867
- throw new Error("useIframe must be used within an IframeProvider");
868
- }
869
- return context;
870
- };
871
-
872
55
  // src/react/components/UserButton.tsx
873
56
 
874
57
 
@@ -944,7 +127,7 @@ var UserButton = ({
944
127
  /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
945
128
  "button",
946
129
  {
947
- className: _chunkRF23Q4V6js.cn.call(void 0,
130
+ className: _chunkO2SODTR3js.cn.call(void 0,
948
131
  "cac-flex cac-w-full cac-items-center cac-justify-between cac-gap-2 cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-text-neutral-500 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50",
949
132
  className
950
133
  ),
@@ -983,7 +166,7 @@ var UserButton = ({
983
166
  "button",
984
167
  {
985
168
  "data-testid": "sign-in-button",
986
- className: _chunkRF23Q4V6js.cn.call(void 0,
169
+ className: _chunkO2SODTR3js.cn.call(void 0,
987
170
  "cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50",
988
171
  className
989
172
  ),
@@ -1004,7 +187,7 @@ var SignInButton = ({
1004
187
  "button",
1005
188
  {
1006
189
  "data-testid": "sign-in-button",
1007
- className: _chunkRF23Q4V6js.cn.call(void 0,
190
+ className: _chunkO2SODTR3js.cn.call(void 0,
1008
191
  "cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50",
1009
192
  className
1010
193
  ),
@@ -1021,7 +204,7 @@ var SignOutButton = ({ className }) => {
1021
204
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
1022
205
  "button",
1023
206
  {
1024
- className: _chunkRF23Q4V6js.cn.call(void 0,
207
+ className: _chunkO2SODTR3js.cn.call(void 0,
1025
208
  "cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50",
1026
209
  className
1027
210
  ),
@@ -1034,7 +217,7 @@ var SignOutButton = ({ className }) => {
1034
217
  // src/react/components/NextLogOut.tsx
1035
218
 
1036
219
  var NextLogOut = ({ children }) => {
1037
- const config = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, );
220
+ const config = _chunkCZ3AVCKDjs.resolveAuthConfig.call(void 0, );
1038
221
  const logoutUrl = `${config.logoutUrl}`;
1039
222
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "a", { href: logoutUrl, children });
1040
223
  };
@@ -1051,8 +234,5 @@ var NextLogOut = ({ children }) => {
1051
234
 
1052
235
 
1053
236
 
1054
-
1055
-
1056
-
1057
- exports.CivicAuthIframeContainer = CivicAuthIframeContainer; exports.CivicAuthProvider = CivicAuthProvider; exports.CivicNextAuthProvider = CivicNextAuthProvider; exports.NextLogOut = NextLogOut; exports.SignInButton = SignInButton; exports.SignOutButton = SignOutButton; exports.UserButton = UserButton; exports.useAuth = useAuth; exports.useConfig = useConfig; exports.useIframe = useIframe; exports.useSession = useSession; exports.useToken = useToken; exports.useTokenCookie = useTokenCookie; exports.useUser = useUser; exports.useUserCookie = useUserCookie;
237
+ exports.CivicAuthIframeContainer = _chunkVJVRFKDHjs.CivicAuthIframeContainer; exports.CivicAuthProvider = CivicAuthProvider; exports.NextLogOut = NextLogOut; exports.SignInButton = SignInButton; exports.SignOutButton = SignOutButton; exports.UserButton = UserButton; exports.useAuth = _chunkVJVRFKDHjs.useAuth; exports.useConfig = _chunkVJVRFKDHjs.useConfig; exports.useIframe = _chunkVJVRFKDHjs.useIframe; exports.useSession = _chunkVJVRFKDHjs.useSession; exports.useToken = _chunkVJVRFKDHjs.useToken; exports.useUser = useUser;
1058
238
  //# sourceMappingURL=react.js.map