@multiplatform.one/keycloak 0.7.14 → 0.7.16

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 (155) hide show
  1. package/package.json +28 -28
  2. package/routes/index.d.ts +22 -0
  3. package/routes/index.js +22 -0
  4. package/src/Authenticated.tsx +27 -17
  5. package/src/authConfig.ts +3 -9
  6. package/src/hooks/index.ts +0 -3
  7. package/src/hooks/useTokensFromQuery/index.ts +1 -0
  8. package/src/hooks/useTokensFromState.ts +1 -1
  9. package/src/index.ts +3 -0
  10. package/src/{@types/coreJsPure.d.ts → keycloak/context.ts} +6 -11
  11. package/{lib/@types/expoKeycloakAuth.d.d.mts → src/keycloak/index.ts} +4 -7
  12. package/src/keycloak/keycloak.native.ts +148 -0
  13. package/src/keycloak/keycloak.ts +175 -0
  14. package/src/provider/AfterAuth.tsx +63 -0
  15. package/src/provider/{keycloakProvider.native.tsx → AuthProvider/AuthProvider.native.tsx} +8 -16
  16. package/src/provider/{keycloakProvider.tsx → AuthProvider/AuthProvider.tsx} +81 -94
  17. package/{lib/@types/expoKeycloakAuth.d.d.ts → src/provider/AuthProvider/index.ts} +4 -7
  18. package/src/provider/KeycloakProvider.tsx +53 -0
  19. package/src/{hooks/useCurrentRouteName/index.native.ts → provider/index.ts} +6 -11
  20. package/src/routes.ts +163 -0
  21. package/src/session/index.ts +22 -0
  22. package/src/session/session.native.ts +31 -0
  23. package/src/session/session.ts +68 -0
  24. package/src/{state/index.ts → state.ts} +5 -4
  25. package/src/{util.ts → token.ts} +25 -11
  26. package/src/types.ts +4 -1
  27. package/lib/@types/coreJsPure.d.d.mts +0 -2
  28. package/lib/@types/coreJsPure.d.d.ts +0 -2
  29. package/lib/@types/coreJsPure.d.js +0 -1
  30. package/lib/@types/coreJsPure.d.mjs +0 -0
  31. package/lib/@types/expoKeycloakAuth.d.js +0 -1
  32. package/lib/@types/expoKeycloakAuth.d.mjs +0 -0
  33. package/lib/Authenticated.d.mts +0 -33
  34. package/lib/Authenticated.d.ts +0 -33
  35. package/lib/Authenticated.js +0 -19
  36. package/lib/Authenticated.mjs +0 -20
  37. package/lib/authConfig.d.mts +0 -14
  38. package/lib/authConfig.d.ts +0 -14
  39. package/lib/authConfig.js +0 -8
  40. package/lib/authConfig.mjs +0 -9
  41. package/lib/chunk-2GUOEPFY.mjs +0 -0
  42. package/lib/chunk-3ZPHNEJL.js +0 -11
  43. package/lib/chunk-64PPU7Q3.mjs +0 -13
  44. package/lib/chunk-6NDWOV55.js +0 -1
  45. package/lib/chunk-7MTPH6XK.mjs +0 -26
  46. package/lib/chunk-A7UBOIAL.js +0 -13
  47. package/lib/chunk-AD6VPMID.mjs +0 -20
  48. package/lib/chunk-AUUZ6BI7.js +0 -78
  49. package/lib/chunk-AYKQVWMR.mjs +0 -0
  50. package/lib/chunk-BVB33X7V.mjs +0 -265
  51. package/lib/chunk-C2FNSVJO.js +0 -17
  52. package/lib/chunk-J4CCMILN.mjs +0 -41
  53. package/lib/chunk-K33RKJZX.js +0 -443
  54. package/lib/chunk-KLLABHIC.js +0 -13
  55. package/lib/chunk-L5HMOY4M.mjs +0 -18
  56. package/lib/chunk-MZ6E62HK.js +0 -33
  57. package/lib/chunk-NWRANDZ6.js +0 -98
  58. package/lib/chunk-NXTVYIBR.mjs +0 -11
  59. package/lib/chunk-OYWAYMFN.js +0 -17
  60. package/lib/chunk-RPUOO7AV.mjs +0 -10
  61. package/lib/chunk-RYM7KERK.mjs +0 -52
  62. package/lib/chunk-UBVYEZI3.js +0 -1
  63. package/lib/chunk-UHTKOFS6.mjs +0 -84
  64. package/lib/chunk-UL6L5ONI.js +0 -460
  65. package/lib/chunk-UOKDJT5L.mjs +0 -16
  66. package/lib/chunk-W5DJLFTQ.js +0 -56
  67. package/lib/chunk-WT5W6BSZ.js +0 -161
  68. package/lib/chunk-XEO3YXBM.js +0 -22
  69. package/lib/chunk-XO4W2SMN.mjs +0 -50
  70. package/lib/chunk-XVPWAAZ4.mjs +0 -76
  71. package/lib/chunk-Y4XMF3R6.mjs +0 -13
  72. package/lib/chunk-YGNNHTFE.js +0 -12
  73. package/lib/hooks/index.d.mts +0 -9
  74. package/lib/hooks/index.d.ts +0 -9
  75. package/lib/hooks/index.js +0 -23
  76. package/lib/hooks/index.mjs +0 -34
  77. package/lib/hooks/useAuthConfig.d.mts +0 -6
  78. package/lib/hooks/useAuthConfig.d.ts +0 -6
  79. package/lib/hooks/useAuthConfig.js +0 -8
  80. package/lib/hooks/useAuthConfig.mjs +0 -8
  81. package/lib/hooks/useCurrentRouteName/index.d.mts +0 -3
  82. package/lib/hooks/useCurrentRouteName/index.d.ts +0 -3
  83. package/lib/hooks/useCurrentRouteName/index.js +0 -7
  84. package/lib/hooks/useCurrentRouteName/index.mjs +0 -7
  85. package/lib/hooks/useCurrentRouteName/index.native.d.mts +0 -3
  86. package/lib/hooks/useCurrentRouteName/index.native.d.ts +0 -3
  87. package/lib/hooks/useCurrentRouteName/index.native.js +0 -14
  88. package/lib/hooks/useCurrentRouteName/index.native.mjs +0 -14
  89. package/lib/hooks/useKeycloak/index.d.mts +0 -46
  90. package/lib/hooks/useKeycloak/index.d.ts +0 -46
  91. package/lib/hooks/useKeycloak/index.js +0 -11
  92. package/lib/hooks/useKeycloak/index.mjs +0 -12
  93. package/lib/hooks/useKeycloak/useKeycloak.d.mts +0 -2
  94. package/lib/hooks/useKeycloak/useKeycloak.d.ts +0 -2
  95. package/lib/hooks/useKeycloak/useKeycloak.js +0 -10
  96. package/lib/hooks/useKeycloak/useKeycloak.mjs +0 -11
  97. package/lib/hooks/useKeycloak/useKeycloak.native.d.mts +0 -20
  98. package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +0 -20
  99. package/lib/hooks/useKeycloak/useKeycloak.native.js +0 -80
  100. package/lib/hooks/useKeycloak/useKeycloak.native.mjs +0 -69
  101. package/lib/hooks/useLogin.d.mts +0 -3
  102. package/lib/hooks/useLogin.d.ts +0 -3
  103. package/lib/hooks/useLogin.js +0 -11
  104. package/lib/hooks/useLogin.mjs +0 -11
  105. package/lib/hooks/useTokensFromQuery/index.d.mts +0 -3
  106. package/lib/hooks/useTokensFromQuery/index.d.ts +0 -3
  107. package/lib/hooks/useTokensFromQuery/index.js +0 -7
  108. package/lib/hooks/useTokensFromQuery/index.mjs +0 -7
  109. package/lib/hooks/useTokensFromQuery/index.native.d.mts +0 -3
  110. package/lib/hooks/useTokensFromQuery/index.native.d.ts +0 -3
  111. package/lib/hooks/useTokensFromQuery/index.native.js +0 -10
  112. package/lib/hooks/useTokensFromQuery/index.native.mjs +0 -7
  113. package/lib/hooks/useTokensFromState.d.mts +0 -3
  114. package/lib/hooks/useTokensFromState.d.ts +0 -3
  115. package/lib/hooks/useTokensFromState.js +0 -9
  116. package/lib/hooks/useTokensFromState.mjs +0 -9
  117. package/lib/index.d.mts +0 -13
  118. package/lib/index.d.ts +0 -13
  119. package/lib/index.js +0 -34
  120. package/lib/index.mjs +0 -56
  121. package/lib/provider/afterAuth.d.mts +0 -29
  122. package/lib/provider/afterAuth.d.ts +0 -29
  123. package/lib/provider/afterAuth.js +0 -18
  124. package/lib/provider/afterAuth.mjs +0 -18
  125. package/lib/provider/index.d.mts +0 -39
  126. package/lib/provider/index.d.ts +0 -39
  127. package/lib/provider/index.js +0 -22
  128. package/lib/provider/index.mjs +0 -25
  129. package/lib/provider/keycloakProvider.d.mts +0 -42
  130. package/lib/provider/keycloakProvider.d.ts +0 -42
  131. package/lib/provider/keycloakProvider.js +0 -20
  132. package/lib/provider/keycloakProvider.mjs +0 -21
  133. package/lib/provider/keycloakProvider.native.d.mts +0 -29
  134. package/lib/provider/keycloakProvider.native.d.ts +0 -29
  135. package/lib/provider/keycloakProvider.native.js +0 -87
  136. package/lib/provider/keycloakProvider.native.mjs +0 -30
  137. package/lib/state/index.d.mts +0 -11
  138. package/lib/state/index.d.ts +0 -11
  139. package/lib/state/index.js +0 -7
  140. package/lib/state/index.mjs +0 -7
  141. package/lib/types.d.mts +0 -17
  142. package/lib/types.d.ts +0 -17
  143. package/lib/types.js +0 -8
  144. package/lib/types.mjs +0 -7
  145. package/lib/util.d.mts +0 -4
  146. package/lib/util.d.ts +0 -4
  147. package/lib/util.js +0 -8
  148. package/lib/util.mjs +0 -9
  149. package/src/hooks/useCurrentRouteName/index.ts +0 -34
  150. package/src/hooks/useKeycloak/index.ts +0 -47
  151. package/src/hooks/useKeycloak/useKeycloak.native.ts +0 -95
  152. package/src/hooks/useKeycloak/useKeycloak.ts +0 -106
  153. package/src/hooks/useLogin.ts +0 -47
  154. package/src/provider/afterAuth.tsx +0 -65
  155. package/src/provider/index.tsx +0 -117
@@ -1,265 +0,0 @@
1
- import {
2
- AfterAuth
3
- } from "./chunk-XO4W2SMN.mjs";
4
- import {
5
- useAuthConfig
6
- } from "./chunk-Y4XMF3R6.mjs";
7
- import {
8
- useAuthState
9
- } from "./chunk-AD6VPMID.mjs";
10
- import {
11
- validToken
12
- } from "./chunk-7MTPH6XK.mjs";
13
-
14
- // src/provider/keycloakProvider.tsx
15
- import Keycloak from "@bitspur/keycloak-js";
16
- import React, { useMemo, useEffect, useState, useCallback } from "react";
17
- import { MultiPlatform } from "multiplatform.one";
18
- import { ReactKeycloakProvider } from "@bitspur/react-keycloak-web";
19
- import { SSRKeycloakProvider, SSRCookies } from "@bitspur/react-keycloak-ssr";
20
- import { useRouter } from "next/router";
21
- var logger = console;
22
- function KeycloakProvider({
23
- children,
24
- cookies,
25
- keycloakConfig,
26
- keycloakInitOptions,
27
- loadingComponent,
28
- onEvent,
29
- onTokens
30
- }) {
31
- const { debug, ensureFreshness, persist, messageHandlerKeys, ssr } = useAuthConfig();
32
- const authState = useAuthState();
33
- const LoadingComponent = loadingComponent || (() => /* @__PURE__ */ React.createElement(React.Fragment, null, debug ? "authenticating" : null));
34
- const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
35
- const [refreshToken, setRefreshToken] = useState(
36
- validToken(
37
- ensureFreshness && ("refreshToken" in query && (query.refreshToken?.toString() || true) || persist && authState.refreshToken) || false
38
- )
39
- );
40
- const [idToken, setIdToken] = useState(
41
- validToken(
42
- "idToken" in query && (query.idToken?.toString() || true) || persist && authState.idToken || false,
43
- refreshToken
44
- )
45
- );
46
- const [token, setToken] = useState(
47
- validToken(
48
- "token" in query && (query.token?.toString() || true) || persist && authState.token || false,
49
- refreshToken
50
- )
51
- );
52
- useEffect(() => {
53
- if (token !== true && refreshToken !== true && idToken !== true)
54
- return;
55
- if (debug)
56
- logger.debug("post message", JSON.stringify({ type: "LOADED" }));
57
- (messageHandlerKeys || []).forEach((key) => {
58
- window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: "LOADED" }));
59
- });
60
- window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: "LOADED" }));
61
- window?.parent?.postMessage(JSON.stringify({ type: "LOADED" }));
62
- }, []);
63
- useEffect(() => {
64
- if (refreshToken !== true)
65
- return;
66
- const messageCallback = (e) => {
67
- let data = e?.data || null;
68
- if (typeof data === "string") {
69
- try {
70
- data = JSON.parse(data);
71
- } catch (err) {
72
- }
73
- }
74
- if (debug)
75
- logger.debug("received message", JSON.stringify(data));
76
- const message = data;
77
- if (!message.type)
78
- return;
79
- if (message.type.toUpperCase() === "REFRESH_TOKEN" && message.payload) {
80
- if (debug)
81
- logger.debug("setting refresh token", message.payload);
82
- setRefreshToken(validToken(message.payload));
83
- window.removeEventListener("message", messageCallback);
84
- }
85
- };
86
- if (debug) {
87
- logger.debug(
88
- "listening for message",
89
- JSON.stringify({
90
- type: "REFRESH_TOKEN",
91
- payload: "<some_refresh_token>"
92
- })
93
- );
94
- }
95
- window.addEventListener("message", messageCallback);
96
- return () => {
97
- window.removeEventListener("message", messageCallback);
98
- };
99
- }, []);
100
- useEffect(() => {
101
- if (token !== true)
102
- return;
103
- const messageCallback = (e) => {
104
- let data = e?.data || null;
105
- if (typeof data === "string") {
106
- try {
107
- data = JSON.parse(data);
108
- } catch (err) {
109
- }
110
- }
111
- if (debug)
112
- logger.debug("received message", JSON.stringify(data));
113
- const message = data;
114
- if (!message.type)
115
- return;
116
- if (message.type.toUpperCase() === "TOKEN" && message.payload) {
117
- if (debug)
118
- logger.debug("setting token", message.payload);
119
- setToken(validToken(message.payload, refreshToken));
120
- window.removeEventListener("message", messageCallback);
121
- }
122
- };
123
- if (debug) {
124
- logger.debug(
125
- "listening for message",
126
- JSON.stringify({
127
- type: "TOKEN",
128
- payload: "<some_token>"
129
- })
130
- );
131
- }
132
- window.addEventListener("message", messageCallback);
133
- return () => {
134
- window.removeEventListener("message", messageCallback);
135
- };
136
- }, []);
137
- useEffect(() => {
138
- if (idToken !== true)
139
- return;
140
- const messageCallback = (e) => {
141
- let data = e?.data || null;
142
- if (typeof data === "string") {
143
- try {
144
- data = JSON.parse(data);
145
- } catch (err) {
146
- }
147
- }
148
- if (debug)
149
- logger.debug("received message", JSON.stringify(data));
150
- const message = data;
151
- if (!message.type)
152
- return;
153
- if (message.type.toUpperCase() === "ID_TOKEN" && message.payload) {
154
- if (debug)
155
- logger.debug("setting id token", message.payload);
156
- setIdToken(validToken(message.payload, refreshToken));
157
- window.removeEventListener("message", messageCallback);
158
- }
159
- };
160
- if (debug) {
161
- logger.debug(
162
- "listening for message",
163
- JSON.stringify({
164
- type: "ID_TOKEN",
165
- payload: "<some_id_token>"
166
- })
167
- );
168
- }
169
- window.addEventListener("message", messageCallback);
170
- return () => {
171
- window.removeEventListener("message", messageCallback);
172
- };
173
- }, []);
174
- const handleEvent = useCallback((eventType, error) => {
175
- if (error) {
176
- if (typeof error === "string")
177
- throw new Error(error);
178
- const err = new Error(error.error);
179
- err.errorDescription = error.error_description;
180
- throw err;
181
- }
182
- if (onEvent)
183
- onEvent(eventType, error);
184
- }, []);
185
- const keycloak = useMemo(
186
- () => typeof window !== "undefined" && new Keycloak({
187
- url: keycloakConfig.url,
188
- realm: keycloakConfig.realm,
189
- clientId: keycloakConfig.clientId
190
- }),
191
- [keycloakInitOptions, token, refreshToken, idToken]
192
- );
193
- const initOptions = useMemo(() => {
194
- const initOptions2 = {
195
- ...defaultKeycloakInitOptions,
196
- ...keycloakInitOptions
197
- };
198
- if (debug)
199
- initOptions2.enableLogging = true;
200
- if (token && typeof token === "string") {
201
- initOptions2.token = token;
202
- initOptions2.timeSkew = 0;
203
- if (idToken && typeof idToken === "string")
204
- initOptions2.idToken = idToken;
205
- if (refreshToken && typeof refreshToken === "string")
206
- initOptions2.refreshToken = refreshToken;
207
- }
208
- if (typeof window !== "undefined" && window.self !== window.top || token && !refreshToken) {
209
- initOptions2.checkLoginIframe = false;
210
- initOptions2.flow = "implicit";
211
- initOptions2.onLoad = void 0;
212
- }
213
- return initOptions2;
214
- }, [keycloakInitOptions, token, refreshToken, idToken]);
215
- if (token === true || idToken === true || refreshToken === true)
216
- return /* @__PURE__ */ React.createElement(LoadingComponent, null);
217
- if (cookies && ssr) {
218
- return (
219
- // @ts-ignore
220
- /* @__PURE__ */ React.createElement(
221
- SSRKeycloakProvider,
222
- {
223
- LoadingComponent: /* @__PURE__ */ React.createElement(LoadingComponent, null),
224
- autoRefreshToken: initOptions.checkLoginIframe,
225
- initOptions,
226
- onEvent: handleEvent,
227
- onTokens,
228
- persistor: SSRCookies(cookies),
229
- keycloakConfig: {
230
- clientId: keycloakConfig.clientId,
231
- realm: keycloakConfig.realm,
232
- url: keycloakConfig.url
233
- }
234
- },
235
- /* @__PURE__ */ React.createElement(AfterAuth, null, children)
236
- )
237
- );
238
- }
239
- if (!keycloak)
240
- return /* @__PURE__ */ React.createElement(LoadingComponent, null);
241
- return /* @__PURE__ */ React.createElement(
242
- ReactKeycloakProvider,
243
- {
244
- LoadingComponent: /* @__PURE__ */ React.createElement(LoadingComponent, null),
245
- authClient: keycloak,
246
- autoRefreshToken: initOptions.checkLoginIframe,
247
- initOptions,
248
- onEvent: handleEvent,
249
- onTokens
250
- },
251
- /* @__PURE__ */ React.createElement(AfterAuth, null, children)
252
- );
253
- }
254
- var defaultKeycloakInitOptions = {
255
- checkLoginIframe: true,
256
- checkLoginIframeInterval: 5,
257
- enableLogging: false,
258
- onLoad: "check-sso",
259
- pkceMethod: "S256"
260
- };
261
-
262
- export {
263
- KeycloakProvider,
264
- defaultKeycloakInitOptions
265
- };
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- }); // src/state/index.ts
5
- var _zustand = require("multiplatform.one/zustand");
6
- var _config = require("app/config");
7
- var useStore = _zustand.createStateStore.call(void 0, "auth", {
8
- idToken: "",
9
- refreshToken: "",
10
- token: ""
11
- }, void 0, {
12
- persist: _config.config.get("KEYCLOAK_PERSIST") === "1"
13
- }).useStore;
14
- function useAuthState() {
15
- return useStore();
16
- }
17
- exports.useAuthState = useAuthState;
@@ -1,41 +0,0 @@
1
- import {
2
- useCurrentRouteName
3
- } from "./chunk-L5HMOY4M.mjs";
4
- import {
5
- useKeycloak
6
- } from "./chunk-UHTKOFS6.mjs";
7
- import {
8
- useAuthConfig
9
- } from "./chunk-Y4XMF3R6.mjs";
10
-
11
- // src/hooks/useLogin.ts
12
- import { MultiPlatform } from "multiplatform.one";
13
- import { useRouter } from "solito/router";
14
- function useLogin(loginRoute) {
15
- if (MultiPlatform.isStorybook)
16
- return () => null;
17
- const keycloak = useKeycloak();
18
- const authConfig = useAuthConfig();
19
- const currentRouteName = useCurrentRouteName();
20
- const router = useRouter();
21
- return () => {
22
- if (!loginRoute)
23
- loginRoute = authConfig.loginRoute;
24
- if (!loginRoute) {
25
- keycloak.login();
26
- return;
27
- }
28
- const url = new URL(`x://${loginRoute}`);
29
- const searchParams = new URLSearchParams(url.search);
30
- if (currentRouteName)
31
- searchParams.append("backTo", currentRouteName);
32
- let query = searchParams.toString();
33
- if (query.length)
34
- query = `?${query}`;
35
- router.push(`${url.pathname.substring(2)}${query}`);
36
- };
37
- }
38
-
39
- export {
40
- useLogin
41
- };