@multiplatform.one/keycloak 0.0.7 → 0.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.
Files changed (61) hide show
  1. package/esm/hooks/useKeycloak/index.js +1 -1
  2. package/esm/hooks/useKeycloak/index.js.map +1 -1
  3. package/esm/hooks/useKeycloak/useKeycloak.native.js +2 -2
  4. package/esm/hooks/useKeycloak/useKeycloak.native.js.map +1 -1
  5. package/esm/provider/keycloakProvider.native.js +1 -1
  6. package/esm/provider/keycloakProvider.native.js.map +1 -1
  7. package/lib/hooks/useKeycloak/index.d.ts +1 -1
  8. package/lib/hooks/useKeycloak/index.js.map +1 -1
  9. package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +1 -1
  10. package/lib/hooks/useKeycloak/useKeycloak.native.js +6 -6
  11. package/lib/hooks/useKeycloak/useKeycloak.native.js.map +1 -1
  12. package/lib/provider/keycloakProvider.native.js +5 -5
  13. package/lib/provider/keycloakProvider.native.js.map +1 -1
  14. package/lib/tsconfig.build.tsbuildinfo +1 -1
  15. package/package.json +9 -3
  16. package/src/{expo/index.ts → @types/expoKeycloakAuth.d.ts} +7 -6
  17. package/src/hooks/useKeycloak/index.ts +1 -1
  18. package/src/hooks/useKeycloak/useKeycloak.native.ts +2 -2
  19. package/src/provider/keycloakProvider.native.tsx +1 -1
  20. package/esm/expo/ExpoKeycloakContext.js +0 -28
  21. package/esm/expo/ExpoKeycloakContext.js.map +0 -1
  22. package/esm/expo/ExpoKeycloakProvider.js +0 -113
  23. package/esm/expo/ExpoKeycloakProvider.js.map +0 -1
  24. package/esm/expo/const.js +0 -42
  25. package/esm/expo/const.js.map +0 -1
  26. package/esm/expo/helpers.js +0 -56
  27. package/esm/expo/helpers.js.map +0 -1
  28. package/esm/expo/index.js +0 -28
  29. package/esm/expo/index.js.map +0 -1
  30. package/esm/expo/useExpoKeycloak.js +0 -44
  31. package/esm/expo/useExpoKeycloak.js.map +0 -1
  32. package/esm/expo/useTokenStorage.js +0 -153
  33. package/esm/expo/useTokenStorage.js.map +0 -1
  34. package/lib/expo/ExpoKeycloakContext.d.ts +0 -32
  35. package/lib/expo/ExpoKeycloakContext.js +0 -47
  36. package/lib/expo/ExpoKeycloakContext.js.map +0 -1
  37. package/lib/expo/ExpoKeycloakProvider.d.ts +0 -37
  38. package/lib/expo/ExpoKeycloakProvider.js +0 -130
  39. package/lib/expo/ExpoKeycloakProvider.js.map +0 -1
  40. package/lib/expo/const.d.ts +0 -33
  41. package/lib/expo/const.js +0 -66
  42. package/lib/expo/const.js.map +0 -1
  43. package/lib/expo/helpers.d.ts +0 -41
  44. package/lib/expo/helpers.js +0 -77
  45. package/lib/expo/helpers.js.map +0 -1
  46. package/lib/expo/index.d.ts +0 -26
  47. package/lib/expo/index.js +0 -50
  48. package/lib/expo/index.js.map +0 -1
  49. package/lib/expo/useExpoKeycloak.d.ts +0 -31
  50. package/lib/expo/useExpoKeycloak.js +0 -62
  51. package/lib/expo/useExpoKeycloak.js.map +0 -1
  52. package/lib/expo/useTokenStorage.d.ts +0 -36
  53. package/lib/expo/useTokenStorage.js +0 -166
  54. package/lib/expo/useTokenStorage.js.map +0 -1
  55. package/src/expo/ExpoKeycloakContext.ts +0 -41
  56. package/src/expo/ExpoKeycloakProvider.tsx +0 -139
  57. package/src/expo/NOTES +0 -1
  58. package/src/expo/const.ts +0 -42
  59. package/src/expo/helpers.ts +0 -74
  60. package/src/expo/useExpoKeycloak.ts +0 -44
  61. package/src/expo/useTokenStorage.ts +0 -168
@@ -1,168 +0,0 @@
1
- /**
2
- * File: /src/expo/useTokenStorage.ts
3
- * Project: @multiplatform.one/keycloak
4
- * File Created: 26-11-2022 06:58:16
5
- * Author: Clay Risser
6
- * -----
7
- * Last Modified: 26-11-2022 08:08:00
8
- * Modified By: Clay Risser
9
- * -----
10
- * Risser Labs LLC (c) Copyright 2022
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- import { AppState, AppStateStatus, Platform } from "react-native";
26
- import { DiscoveryDocument, refreshAsync } from "expo-auth-session";
27
- import { REFRESH_TIME_BUFFER, TOKEN_STORAGE_KEY } from "./const";
28
- import { dismiss, revokeAsync, TokenResponse } from "expo-auth-session";
29
- import { getCurrentTimeInSeconds } from "./helpers";
30
- import { useAsyncStorage } from "@react-native-async-storage/async-storage";
31
- import { useEffect, useRef, useState } from "react";
32
-
33
- const logger = console;
34
-
35
- export function useTokenStorage(
36
- {
37
- disableAutoRefresh = false,
38
- refreshTimeBuffer = REFRESH_TIME_BUFFER,
39
- tokenStorageKey = TOKEN_STORAGE_KEY,
40
- },
41
- config: {
42
- clientId: string;
43
- extraParams?: Record<string, string>;
44
- realm: string;
45
- redirectUri: string;
46
- url: string;
47
- },
48
- discovery: DiscoveryDocument
49
- ): [TokenResponse | null, (token: TokenResponse | null) => Promise<void>] {
50
- const [token, setToken] = useState<TokenResponse | null>();
51
- const { getItem, setItem, removeItem } = useAsyncStorage(tokenStorageKey);
52
- const refreshHandler = useRef<NodeJS.Timeout>(null);
53
- const appState = useRef(AppState.currentState);
54
- const refreshTime = useRef<number>(null);
55
- const tokenData = useRef<TokenResponse>(null);
56
-
57
- async function updateAndSaveToken(newToken: TokenResponse | null) {
58
- try {
59
- setToken(newToken);
60
- if (newToken !== null) {
61
- const stringifiedValue = JSON.stringify(newToken);
62
- await setItem(stringifiedValue);
63
- } else {
64
- await removeItem();
65
- }
66
- } catch (error) {
67
- logger.error(error);
68
- }
69
- }
70
-
71
- async function handleTokenRefresh(token: TokenResponse | null) {
72
- try {
73
- const tokenResponse = await refreshAsync(
74
- { refreshToken: token?.refreshToken, ...config },
75
- discovery
76
- );
77
- updateAndSaveToken(tokenResponse);
78
- } catch (err) {
79
- updateAndSaveToken(null);
80
- }
81
- }
82
-
83
- useEffect(() => {
84
- function handleAppState(nextAppState: AppStateStatus) {
85
- if (
86
- appState.current.match(/inactive|background/) &&
87
- nextAppState === "active"
88
- ) {
89
- if (refreshHandler.current !== null) {
90
- clearTimeout(refreshHandler.current);
91
- const now = getCurrentTimeInSeconds();
92
- if (Number(refreshTime.current) <= now) {
93
- setToken(null);
94
- } else {
95
- const timeout = 1000 * (Number(refreshTime.current) - now);
96
- // @ts-ignore
97
- refreshHandler.current = setTimeout(() => {
98
- handleTokenRefresh(tokenData.current);
99
- }, timeout);
100
- }
101
- }
102
- }
103
- appState.current = nextAppState;
104
- }
105
- const subscription = AppState.addEventListener("change", handleAppState);
106
- return () => {
107
- if (subscription) {
108
- subscription.remove();
109
- } else {
110
- // @ts-ignore
111
- AppState.removeEventListener("change", handleAppState);
112
- }
113
- };
114
- }, []);
115
-
116
- useEffect(() => {
117
- async function getTokenFromStorage() {
118
- try {
119
- const tokenFromStorage = await getItem();
120
- if (!tokenFromStorage) throw new Error("No token in storage");
121
- const token = JSON.parse(tokenFromStorage);
122
- if (!TokenResponse.isTokenFresh(token, -refreshTimeBuffer)) {
123
- handleTokenRefresh(token);
124
- } else {
125
- setToken(token);
126
- }
127
- } catch (error) {
128
- setToken(null);
129
- }
130
- }
131
- if (discovery) getTokenFromStorage();
132
- }, [discovery]);
133
-
134
- useEffect(() => {
135
- // trigger every token update
136
- // @ts-ignore
137
- tokenData.current = token || null;
138
- if (token !== undefined && !disableAutoRefresh) {
139
- if (refreshHandler.current !== null) {
140
- clearTimeout(refreshHandler.current);
141
- }
142
- if (token?.expiresIn) {
143
- const now = getCurrentTimeInSeconds();
144
- // @ts-ignore
145
- refreshTime.current =
146
- token.issuedAt + token.expiresIn - refreshTimeBuffer;
147
- const timeout = 1000 * (refreshTime.current - now);
148
- // @ts-ignore
149
- refreshHandler.current = setTimeout(() => {
150
- handleTokenRefresh(token);
151
- }, timeout);
152
- }
153
- if (token === null && tokenData.current !== null) {
154
- revokeAsync(
155
- { token: tokenData.current?.accessToken, ...config },
156
- discovery
157
- );
158
- Platform.OS === "ios" && dismiss();
159
- // @ts-ignore
160
- refreshTime.current = null;
161
- // @ts-ignore
162
- tokenData.current = null;
163
- }
164
- }
165
- }, [token]);
166
-
167
- return [token || null, updateAndSaveToken];
168
- }