@multiplatform.one/keycloak 0.1.9 → 0.2.0
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/esm/Authenticated.mjs +25 -0
- package/dist/esm/Authenticated.mjs.map +7 -0
- package/dist/esm/authConfig.mjs +10 -0
- package/dist/esm/authConfig.mjs.map +7 -0
- package/dist/esm/hooks/index.mjs +4 -0
- package/dist/esm/hooks/index.mjs.map +7 -0
- package/dist/esm/hooks/useAuthConfig.mjs +9 -0
- package/dist/esm/hooks/useAuthConfig.mjs.map +7 -0
- package/dist/esm/hooks/useCurrentRouteName/index.mjs +17 -0
- package/dist/esm/hooks/useCurrentRouteName/index.mjs.map +7 -0
- package/dist/esm/hooks/useCurrentRouteName/index.native.mjs +12 -0
- package/dist/esm/hooks/useCurrentRouteName/index.native.mjs.map +7 -0
- package/dist/esm/hooks/useKeycloak/index.mjs +2 -0
- package/dist/esm/hooks/useKeycloak/index.mjs.map +7 -0
- package/dist/esm/hooks/useKeycloak/useKeycloak.mjs +81 -0
- package/dist/esm/hooks/useKeycloak/useKeycloak.mjs.map +7 -0
- package/dist/esm/hooks/useKeycloak/useKeycloak.native.mjs +108 -0
- package/dist/esm/hooks/useKeycloak/useKeycloak.native.mjs.map +7 -0
- package/dist/esm/hooks/useLogin.mjs +35 -0
- package/dist/esm/hooks/useLogin.mjs.map +7 -0
- package/dist/esm/index.mjs +5 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/esm/provider/afterAuth.mjs +29 -0
- package/dist/esm/provider/afterAuth.mjs.map +7 -0
- package/dist/esm/provider/index.mjs +73 -0
- package/dist/esm/provider/index.mjs.map +7 -0
- package/dist/esm/provider/keycloakProvider.mjs +275 -0
- package/dist/esm/provider/keycloakProvider.mjs.map +7 -0
- package/dist/esm/provider/keycloakProvider.native.mjs +69 -0
- package/dist/esm/provider/keycloakProvider.native.mjs.map +7 -0
- package/dist/esm/state/index.mjs +14 -0
- package/dist/esm/state/index.mjs.map +7 -0
- package/dist/esm/types.mjs +5 -0
- package/dist/esm/types.mjs.map +7 -0
- package/dist/jsx/Authenticated.mjs +20 -0
- package/dist/jsx/Authenticated.mjs.map +7 -0
- package/dist/jsx/authConfig.mjs +10 -0
- package/dist/jsx/authConfig.mjs.map +7 -0
- package/dist/jsx/hooks/index.mjs +4 -0
- package/dist/jsx/hooks/index.mjs.map +7 -0
- package/dist/jsx/hooks/useAuthConfig.mjs +9 -0
- package/dist/jsx/hooks/useAuthConfig.mjs.map +7 -0
- package/dist/jsx/hooks/useCurrentRouteName/index.mjs +17 -0
- package/dist/jsx/hooks/useCurrentRouteName/index.mjs.map +7 -0
- package/dist/jsx/hooks/useCurrentRouteName/index.native.mjs +12 -0
- package/dist/jsx/hooks/useCurrentRouteName/index.native.mjs.map +7 -0
- package/dist/jsx/hooks/useKeycloak/index.mjs +2 -0
- package/dist/jsx/hooks/useKeycloak/index.mjs.map +7 -0
- package/dist/jsx/hooks/useKeycloak/useKeycloak.mjs +25 -0
- package/dist/jsx/hooks/useKeycloak/useKeycloak.mjs.map +7 -0
- package/dist/jsx/hooks/useKeycloak/useKeycloak.native.mjs +58 -0
- package/dist/jsx/hooks/useKeycloak/useKeycloak.native.mjs.map +7 -0
- package/dist/jsx/hooks/useLogin.mjs +33 -0
- package/dist/jsx/hooks/useLogin.mjs.map +7 -0
- package/dist/jsx/index.mjs +5 -0
- package/dist/jsx/index.mjs.map +7 -0
- package/dist/jsx/provider/afterAuth.mjs +28 -0
- package/dist/jsx/provider/afterAuth.mjs.map +7 -0
- package/dist/jsx/provider/index.mjs +20 -0
- package/dist/jsx/provider/index.mjs.map +7 -0
- package/dist/jsx/provider/keycloakProvider.mjs +158 -0
- package/dist/jsx/provider/keycloakProvider.mjs.map +7 -0
- package/dist/jsx/provider/keycloakProvider.native.mjs +14 -0
- package/dist/jsx/provider/keycloakProvider.native.mjs.map +7 -0
- package/dist/jsx/state/index.mjs +17 -0
- package/dist/jsx/state/index.mjs.map +7 -0
- package/dist/jsx/types.mjs +5 -0
- package/dist/jsx/types.mjs.map +7 -0
- package/{lib → dist/lib}/Authenticated.d.ts +1 -1
- package/dist/lib/Authenticated.js +60 -0
- package/dist/lib/Authenticated.js.map +7 -0
- package/dist/lib/authConfig.js +35 -0
- package/dist/lib/authConfig.js.map +7 -0
- package/{lib → dist/lib}/hooks/index.d.ts +3 -3
- package/dist/lib/hooks/index.js +21 -0
- package/dist/lib/hooks/index.js.map +7 -0
- package/dist/lib/hooks/useAuthConfig.js +33 -0
- package/dist/lib/hooks/useAuthConfig.js.map +7 -0
- package/{lib/hooks/useCurrentRouteName.d.ts → dist/lib/hooks/useCurrentRouteName/index.d.ts} +5 -5
- package/dist/lib/hooks/useCurrentRouteName/index.js +41 -0
- package/dist/lib/hooks/useCurrentRouteName/index.js.map +7 -0
- package/{esm/types.js → dist/lib/hooks/useCurrentRouteName/index.native.d.ts} +5 -7
- package/dist/lib/hooks/useCurrentRouteName/index.native.js +36 -0
- package/dist/lib/hooks/useCurrentRouteName/index.native.js.map +7 -0
- package/{lib → dist/lib}/hooks/useKeycloak/index.d.ts +2 -2
- package/dist/lib/hooks/useKeycloak/index.js +19 -0
- package/dist/lib/hooks/useKeycloak/index.js.map +7 -0
- package/{lib → dist/lib}/hooks/useKeycloak/useKeycloak.d.ts +2 -2
- package/dist/lib/hooks/useKeycloak/useKeycloak.js +63 -0
- package/dist/lib/hooks/useKeycloak/useKeycloak.js.map +7 -0
- package/{lib → dist/lib}/hooks/useKeycloak/useKeycloak.native.d.ts +4 -4
- package/dist/lib/hooks/useKeycloak/useKeycloak.native.js +93 -0
- package/dist/lib/hooks/useKeycloak/useKeycloak.native.js.map +7 -0
- package/{lib → dist/lib}/hooks/useLogin.d.ts +1 -1
- package/dist/lib/hooks/useLogin.js +57 -0
- package/dist/lib/hooks/useLogin.js.map +7 -0
- package/{lib → dist/lib}/index.d.ts +4 -4
- package/dist/lib/index.js +22 -0
- package/dist/lib/index.js.map +7 -0
- package/{lib → dist/lib}/provider/afterAuth.d.ts +1 -1
- package/dist/lib/provider/afterAuth.js +53 -0
- package/dist/lib/provider/afterAuth.js.map +7 -0
- package/{lib → dist/lib}/provider/index.d.ts +4 -4
- package/dist/lib/provider/index.js +61 -0
- package/dist/lib/provider/index.js.map +7 -0
- package/{lib → dist/lib}/provider/keycloakProvider.d.ts +2 -2
- package/dist/lib/provider/keycloakProvider.js +212 -0
- package/dist/lib/provider/keycloakProvider.js.map +7 -0
- package/{lib → dist/lib}/provider/keycloakProvider.native.d.ts +2 -2
- package/dist/lib/provider/keycloakProvider.native.js +55 -0
- package/dist/lib/provider/keycloakProvider.native.js.map +7 -0
- package/{esm/state/index.js → dist/lib/state/index.d.ts} +8 -13
- package/dist/lib/state/index.js +41 -0
- package/dist/lib/state/index.js.map +7 -0
- package/dist/lib/tsconfig.build.tsbuildinfo +1 -0
- package/dist/lib/types.js +27 -0
- package/dist/lib/types.js.map +7 -0
- package/package.json +43 -62
- package/src/@types/coreJsPure.d.ts +2 -2
- package/src/@types/expoKeycloakAuth.d.ts +1 -1
- package/src/Authenticated.tsx +5 -8
- package/src/authConfig.ts +1 -1
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useAuthConfig.ts +2 -2
- package/{esm/hooks/useCurrentRouteName.js → src/hooks/useCurrentRouteName/index.native.ts} +8 -13
- package/src/hooks/{useCurrentRouteName.ts → useCurrentRouteName/index.ts} +8 -8
- package/src/hooks/useKeycloak/index.ts +2 -6
- package/src/hooks/useKeycloak/useKeycloak.native.ts +18 -32
- package/src/hooks/useKeycloak/useKeycloak.ts +7 -7
- package/src/hooks/useLogin.ts +7 -7
- package/src/index.ts +4 -4
- package/src/provider/afterAuth.tsx +10 -11
- package/src/provider/index.tsx +9 -12
- package/src/provider/keycloakProvider.native.tsx +8 -10
- package/src/provider/keycloakProvider.tsx +44 -61
- package/src/state/index.ts +13 -10
- package/esm/Authenticated.js +0 -42
- package/esm/Authenticated.js.map +0 -1
- package/esm/authConfig.js +0 -30
- package/esm/authConfig.js.map +0 -1
- package/esm/hooks/index.js +0 -28
- package/esm/hooks/index.js.map +0 -1
- package/esm/hooks/useAuthConfig.js +0 -30
- package/esm/hooks/useAuthConfig.js.map +0 -1
- package/esm/hooks/useCurrentRouteName.js.map +0 -1
- package/esm/hooks/useKeycloak/index.js +0 -27
- package/esm/hooks/useKeycloak/index.js.map +0 -1
- package/esm/hooks/useKeycloak/useKeycloak.js +0 -55
- package/esm/hooks/useKeycloak/useKeycloak.js.map +0 -1
- package/esm/hooks/useKeycloak/useKeycloak.native.js +0 -91
- package/esm/hooks/useKeycloak/useKeycloak.native.js.map +0 -1
- package/esm/hooks/useLogin.js +0 -50
- package/esm/hooks/useLogin.js.map +0 -1
- package/esm/index.js +0 -29
- package/esm/index.js.map +0 -1
- package/esm/package.json +0 -1
- package/esm/provider/afterAuth.js +0 -58
- package/esm/provider/afterAuth.js.map +0 -1
- package/esm/provider/index.js +0 -43
- package/esm/provider/index.js.map +0 -1
- package/esm/provider/keycloakProvider.js +0 -175
- package/esm/provider/keycloakProvider.js.map +0 -1
- package/esm/provider/keycloakProvider.native.js +0 -41
- package/esm/provider/keycloakProvider.native.js.map +0 -1
- package/esm/state/index.js.map +0 -1
- package/esm/types.js.map +0 -1
- package/lib/Authenticated.js +0 -64
- package/lib/Authenticated.js.map +0 -1
- package/lib/authConfig.js +0 -51
- package/lib/authConfig.js.map +0 -1
- package/lib/hooks/index.js +0 -50
- package/lib/hooks/index.js.map +0 -1
- package/lib/hooks/useAuthConfig.js +0 -48
- package/lib/hooks/useAuthConfig.js.map +0 -1
- package/lib/hooks/useCurrentRouteName.js +0 -54
- package/lib/hooks/useCurrentRouteName.js.map +0 -1
- package/lib/hooks/useKeycloak/index.js +0 -30
- package/lib/hooks/useKeycloak/index.js.map +0 -1
- package/lib/hooks/useKeycloak/useKeycloak.js +0 -71
- package/lib/hooks/useKeycloak/useKeycloak.js.map +0 -1
- package/lib/hooks/useKeycloak/useKeycloak.native.js +0 -112
- package/lib/hooks/useKeycloak/useKeycloak.native.js.map +0 -1
- package/lib/hooks/useLogin.js +0 -65
- package/lib/hooks/useLogin.js.map +0 -1
- package/lib/index.js +0 -60
- package/lib/index.js.map +0 -1
- package/lib/package.json +0 -1
- package/lib/provider/afterAuth.js +0 -78
- package/lib/provider/afterAuth.js.map +0 -1
- package/lib/provider/index.js +0 -77
- package/lib/provider/index.js.map +0 -1
- package/lib/provider/keycloakProvider.js +0 -193
- package/lib/provider/keycloakProvider.js.map +0 -1
- package/lib/provider/keycloakProvider.native.js +0 -61
- package/lib/provider/keycloakProvider.native.js.map +0 -1
- package/lib/state/index.d.ts +0 -34
- package/lib/state/index.js +0 -55
- package/lib/state/index.js.map +0 -1
- package/lib/tsconfig.build.tsbuildinfo +0 -1
- package/lib/types.js +0 -46
- package/lib/types.js.map +0 -1
- /package/{lib → dist/lib}/authConfig.d.ts +0 -0
- /package/{lib → dist/lib}/hooks/useAuthConfig.d.ts +0 -0
- /package/{lib → dist/lib}/types.d.ts +0 -0
package/src/authConfig.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import { useContext } from
|
|
26
|
-
import { AuthConfigContext } from
|
|
25
|
+
import { useContext } from 'react';
|
|
26
|
+
import { AuthConfigContext } from '../authConfig';
|
|
27
27
|
|
|
28
28
|
export function useAuthConfig() {
|
|
29
29
|
return useContext(AuthConfigContext);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* File: /src/hooks/useCurrentRouteName.ts
|
|
3
|
-
* Project:
|
|
4
|
-
* File Created:
|
|
2
|
+
* File: /src/hooks/useCurrentRouteName/index.native.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 24-02-2023 06:30:36
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 24-02-2023 06:31:25
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
|
-
* Risser Labs LLC (c) Copyright
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
11
11
|
*
|
|
12
12
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
13
|
* you may not use this file except in compliance with the License.
|
|
@@ -22,16 +22,11 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import { MultiPlatform } from
|
|
26
|
-
import { useRoute } from
|
|
27
|
-
|
|
25
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
26
|
+
import { useRoute } from '@react-navigation/native';
|
|
27
|
+
|
|
28
28
|
export function useCurrentRouteName() {
|
|
29
29
|
if (MultiPlatform.isStorybook) return null;
|
|
30
|
-
if (MultiPlatform.isNext) {
|
|
31
|
-
const router = useRouter();
|
|
32
|
-
return router.route;
|
|
33
|
-
}
|
|
34
30
|
const route = useRoute();
|
|
35
31
|
return route.name;
|
|
36
32
|
}
|
|
37
|
-
//# sourceMappingURL=useCurrentRouteName.js.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* File: /src/hooks/useCurrentRouteName.ts
|
|
3
|
-
* Project:
|
|
4
|
-
* File Created:
|
|
2
|
+
* File: /src/hooks/useCurrentRouteName/index.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 24-02-2023 06:30:32
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 24-02-2023 06:30:54
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
|
-
* Risser Labs LLC (c) Copyright
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
11
11
|
*
|
|
12
12
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
13
|
* you may not use this file except in compliance with the License.
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import { MultiPlatform } from
|
|
26
|
-
import { useRoute } from
|
|
27
|
-
import { useRouter } from
|
|
25
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
26
|
+
import { useRoute } from '@react-navigation/native';
|
|
27
|
+
import { useRouter } from 'next/router';
|
|
28
28
|
|
|
29
29
|
export function useCurrentRouteName() {
|
|
30
30
|
if (MultiPlatform.isStorybook) return null;
|
|
@@ -22,12 +22,8 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import type {
|
|
26
|
-
|
|
27
|
-
KeycloakRoles,
|
|
28
|
-
KeycloakTokenParsed,
|
|
29
|
-
} from "keycloak-js";
|
|
30
|
-
export * from "./useKeycloak";
|
|
25
|
+
import type { KeycloakResourceAccess, KeycloakRoles, KeycloakTokenParsed } from 'keycloak-js';
|
|
26
|
+
export * from './useKeycloak';
|
|
31
27
|
|
|
32
28
|
export interface IKeycloak {
|
|
33
29
|
authenticated?: boolean;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* File Created: 08-11-2022 14:10:25
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 24-02-2023 06:29:25
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -22,30 +22,18 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import atob from
|
|
26
|
-
import escape from
|
|
27
|
-
import { AuthRequestPromptOptions, AuthSessionResult } from
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
KeycloakTokenParsed,
|
|
33
|
-
} from "keycloak-js";
|
|
34
|
-
import { useKeycloak as useExpoKeycloak } from "expo-keycloak-auth";
|
|
35
|
-
import { IKeycloak } from "./index";
|
|
25
|
+
import atob from 'core-js-pure/stable/atob';
|
|
26
|
+
import escape from 'core-js-pure/stable/escape';
|
|
27
|
+
import type { AuthRequestPromptOptions, AuthSessionResult } from 'expo-auth-session';
|
|
28
|
+
import type { IKeycloak } from './index';
|
|
29
|
+
import type { KeycloakResourceAccess, KeycloakRoles, KeycloakTokenParsed } from 'keycloak-js';
|
|
30
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
31
|
+
import { useKeycloak as useExpoKeycloak } from 'expo-keycloak-auth';
|
|
36
32
|
|
|
37
33
|
export function useKeycloak() {
|
|
38
34
|
if (MultiPlatform.isStorybook) return { authenticated: true } as IKeycloak;
|
|
39
|
-
const { ready, login, isLoggedIn, token, logout, refreshToken } =
|
|
40
|
-
|
|
41
|
-
const keycloak = new ExpoKeycloak(
|
|
42
|
-
ready,
|
|
43
|
-
isLoggedIn,
|
|
44
|
-
login,
|
|
45
|
-
logout,
|
|
46
|
-
token || undefined,
|
|
47
|
-
refreshToken || undefined
|
|
48
|
-
);
|
|
35
|
+
const { ready, login, isLoggedIn, token, logout, refreshToken } = useExpoKeycloak();
|
|
36
|
+
const keycloak = new ExpoKeycloak(ready, isLoggedIn, login, logout, token || undefined, refreshToken || undefined);
|
|
49
37
|
return keycloak as IKeycloak;
|
|
50
38
|
}
|
|
51
39
|
|
|
@@ -67,12 +55,10 @@ export class ExpoKeycloak implements IKeycloak {
|
|
|
67
55
|
constructor(
|
|
68
56
|
ready: boolean,
|
|
69
57
|
isLoggedIn: boolean | undefined,
|
|
70
|
-
login: (
|
|
71
|
-
options?: AuthRequestPromptOptions
|
|
72
|
-
) => Promise<AuthSessionResult | undefined>,
|
|
58
|
+
login: (options?: AuthRequestPromptOptions) => Promise<AuthSessionResult | undefined>,
|
|
73
59
|
public logout: () => unknown,
|
|
74
60
|
public token?: string,
|
|
75
|
-
public refreshToken?: string
|
|
61
|
+
public refreshToken?: string,
|
|
76
62
|
) {
|
|
77
63
|
if (ready) this.authenticated = isLoggedIn;
|
|
78
64
|
if (this.authenticated) {
|
|
@@ -91,20 +77,20 @@ export class ExpoKeycloak implements IKeycloak {
|
|
|
91
77
|
}
|
|
92
78
|
|
|
93
79
|
function decodeToken(str: string) {
|
|
94
|
-
str = str.split(
|
|
95
|
-
str = str.replace(/-/g,
|
|
96
|
-
str = str.replace(/_/g,
|
|
80
|
+
str = str.split('.')[1];
|
|
81
|
+
str = str.replace(/-/g, '+');
|
|
82
|
+
str = str.replace(/_/g, '/');
|
|
97
83
|
switch (str.length % 4) {
|
|
98
84
|
case 0:
|
|
99
85
|
break;
|
|
100
86
|
case 2:
|
|
101
|
-
str +=
|
|
87
|
+
str += '==';
|
|
102
88
|
break;
|
|
103
89
|
case 3:
|
|
104
|
-
str +=
|
|
90
|
+
str += '=';
|
|
105
91
|
break;
|
|
106
92
|
default:
|
|
107
|
-
throw
|
|
93
|
+
throw 'Invalid token';
|
|
108
94
|
}
|
|
109
95
|
str = decodeURIComponent(escape(atob(str)));
|
|
110
96
|
str = JSON.parse(str);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* File Created: 08-11-2022 06:04:59
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 24-02-2023 06:22:07
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import Keycloak from
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
25
|
+
import type Keycloak from 'keycloak-js';
|
|
26
|
+
import type { IKeycloak } from './index';
|
|
27
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
28
|
+
import { useAuthConfig } from '../useAuthConfig';
|
|
29
|
+
import { useKeycloak as useReactKeycloak } from '@react-keycloak/web';
|
|
30
|
+
import { useKeycloak as useSsrKeycloak } from '@react-keycloak/ssr';
|
|
31
31
|
|
|
32
32
|
export function useKeycloak() {
|
|
33
33
|
const authConfig = useAuthConfig();
|
package/src/hooks/useLogin.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* File Created: 08-11-2022 08:49:14
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 24-02-2023 06:22:27
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
import { useCurrentRouteName } from
|
|
28
|
-
import { useKeycloak } from
|
|
29
|
-
import {
|
|
25
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
26
|
+
import { useAuthConfig } from './useAuthConfig';
|
|
27
|
+
import { useCurrentRouteName } from './useCurrentRouteName';
|
|
28
|
+
import { useKeycloak } from './useKeycloak/useKeycloak';
|
|
29
|
+
import { useRouter } from 'solito/router';
|
|
30
30
|
|
|
31
31
|
export function useLogin(loginRoute?: string) {
|
|
32
32
|
if (MultiPlatform.isStorybook) return () => null;
|
|
@@ -42,7 +42,7 @@ export function useLogin(loginRoute?: string) {
|
|
|
42
42
|
}
|
|
43
43
|
const url = new URL(`x://${loginRoute}`);
|
|
44
44
|
const searchParams = new URLSearchParams(url.search);
|
|
45
|
-
if (currentRouteName) searchParams.append(
|
|
45
|
+
if (currentRouteName) searchParams.append('backTo', currentRouteName);
|
|
46
46
|
let query = searchParams.toString();
|
|
47
47
|
if (query.length) query = `?${query}`;
|
|
48
48
|
router.push(`${url.pathname.substring(2)}${query}`);
|
package/src/index.ts
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
export * from
|
|
26
|
-
export * from
|
|
27
|
-
export * from
|
|
28
|
-
export * from
|
|
25
|
+
export * from './Authenticated';
|
|
26
|
+
export * from './authConfig';
|
|
27
|
+
export * from './hooks';
|
|
28
|
+
export * from './provider';
|
|
@@ -22,10 +22,11 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import React, {
|
|
26
|
-
import {
|
|
27
|
-
import { useAuthConfig } from
|
|
28
|
-
import { useAuthState } from
|
|
25
|
+
import React, { useEffect } from 'react';
|
|
26
|
+
import type { FC, ReactNode } from 'react';
|
|
27
|
+
import { useAuthConfig } from '../hooks/useAuthConfig';
|
|
28
|
+
import { useAuthState } from '../state';
|
|
29
|
+
import { useKeycloak } from '../hooks';
|
|
29
30
|
|
|
30
31
|
export interface AfterAuthProps {
|
|
31
32
|
children: ReactNode;
|
|
@@ -36,21 +37,19 @@ const logger = console;
|
|
|
36
37
|
export const AfterAuth: FC<AfterAuthProps> = ({ children }: AfterAuthProps) => {
|
|
37
38
|
const authConfig = useAuthConfig();
|
|
38
39
|
const { token, refreshToken, authenticated } = useKeycloak();
|
|
39
|
-
const
|
|
40
|
+
const authState = useAuthState();
|
|
40
41
|
|
|
41
42
|
useEffect(() => {
|
|
42
43
|
if (!authenticated) return;
|
|
43
44
|
if (token) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
...(refreshToken ? { refreshToken } : {}),
|
|
47
|
-
});
|
|
45
|
+
authState.setToken(token);
|
|
46
|
+
if (refreshToken) authState.setRefreshToken(refreshToken);
|
|
48
47
|
}
|
|
49
48
|
}, [token, refreshToken]);
|
|
50
49
|
|
|
51
50
|
if (authConfig.debug) {
|
|
52
|
-
logger.debug(
|
|
53
|
-
logger.debug(
|
|
51
|
+
logger.debug('authenticated', authenticated);
|
|
52
|
+
logger.debug('token', token);
|
|
54
53
|
}
|
|
55
54
|
return <>{children}</>;
|
|
56
55
|
};
|
package/src/provider/index.tsx
CHANGED
|
@@ -22,21 +22,18 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import React, {
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
} from
|
|
31
|
-
import { KeycloakProvider, KeycloakProviderProps } from "./keycloakProvider";
|
|
25
|
+
import React, { useMemo } from 'react';
|
|
26
|
+
import type { AuthConfig } from '../authConfig';
|
|
27
|
+
import type { FC } from 'react';
|
|
28
|
+
import type { KeycloakProviderProps } from './keycloakProvider';
|
|
29
|
+
import { AuthConfigContext, defaultAuthConfig } from '../authConfig';
|
|
30
|
+
import { KeycloakProvider } from './keycloakProvider';
|
|
32
31
|
|
|
33
32
|
export interface AuthProviderProps extends KeycloakProviderProps {
|
|
34
33
|
authConfig?: Partial<AuthConfig>;
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
export const AuthProvider: FC<AuthProviderProps> = (
|
|
38
|
-
props: AuthProviderProps
|
|
39
|
-
) => {
|
|
36
|
+
export const AuthProvider: FC<AuthProviderProps> = (props: AuthProviderProps) => {
|
|
40
37
|
const { authConfig, debug } = props;
|
|
41
38
|
const authConfigValue = useMemo(
|
|
42
39
|
() => ({
|
|
@@ -44,7 +41,7 @@ export const AuthProvider: FC<AuthProviderProps> = (
|
|
|
44
41
|
...authConfig,
|
|
45
42
|
debug: !!debug,
|
|
46
43
|
}),
|
|
47
|
-
[]
|
|
44
|
+
[],
|
|
48
45
|
);
|
|
49
46
|
|
|
50
47
|
return (
|
|
@@ -54,4 +51,4 @@ export const AuthProvider: FC<AuthProviderProps> = (
|
|
|
54
51
|
);
|
|
55
52
|
};
|
|
56
53
|
|
|
57
|
-
export * from
|
|
54
|
+
export * from './keycloakProvider';
|
|
@@ -22,18 +22,16 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import React
|
|
26
|
-
import type {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
25
|
+
import React from 'react';
|
|
26
|
+
import type { FC } from 'react';
|
|
27
|
+
import type { KeycloakConfig } from 'keycloak-js';
|
|
28
|
+
import type { KeycloakProviderProps } from './keycloakProvider';
|
|
29
|
+
import { AfterAuth } from './afterAuth';
|
|
30
|
+
import { KeycloakProvider as ExpoKeycloakProvider } from 'expo-keycloak-auth';
|
|
30
31
|
|
|
31
|
-
export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
32
|
-
children,
|
|
33
|
-
keycloakConfig,
|
|
34
|
-
}: KeycloakProviderProps) => {
|
|
32
|
+
export const KeycloakProvider: FC<KeycloakProviderProps> = ({ children, keycloakConfig }: KeycloakProviderProps) => {
|
|
35
33
|
const clonedKeycloakConfig = {
|
|
36
|
-
...(keycloakConfig as Omit<KeycloakConfig,
|
|
34
|
+
...(keycloakConfig as Omit<KeycloakConfig, 'baseUrl'>),
|
|
37
35
|
url: keycloakConfig.baseUrl,
|
|
38
36
|
};
|
|
39
37
|
// @ts-ignore
|
|
@@ -22,22 +22,17 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import Keycloak
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
import
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
ComponentType,
|
|
37
|
-
} from "react";
|
|
38
|
-
import { useAuthConfig } from "../hooks/useAuthConfig";
|
|
39
|
-
import { useAuthState } from "../state";
|
|
40
|
-
import { AfterAuth } from "./afterAuth";
|
|
25
|
+
import Keycloak from 'keycloak-js';
|
|
26
|
+
import React, { useMemo, useEffect, useState } from 'react';
|
|
27
|
+
import type { FC, ReactNode, ComponentType } from 'react';
|
|
28
|
+
import type { KeycloakInitOptions } from 'keycloak-js';
|
|
29
|
+
import { AfterAuth } from './afterAuth';
|
|
30
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
31
|
+
import { ReactKeycloakProvider } from '@react-keycloak/web';
|
|
32
|
+
import { SSRKeycloakProvider, SSRCookies } from '@react-keycloak/ssr';
|
|
33
|
+
import { useAuthConfig } from '../hooks/useAuthConfig';
|
|
34
|
+
import { useAuthState } from '../state';
|
|
35
|
+
import { useRouter } from 'next/router';
|
|
41
36
|
|
|
42
37
|
export interface KeycloakProviderProps {
|
|
43
38
|
children: ReactNode;
|
|
@@ -59,59 +54,52 @@ export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
|
59
54
|
keycloakInitOptions,
|
|
60
55
|
loadingComponent,
|
|
61
56
|
}: KeycloakProviderProps) => {
|
|
62
|
-
const
|
|
63
|
-
const LoadingComponent =
|
|
64
|
-
loadingComponent || (() => <>{debug ? "authenticating" : null}</>);
|
|
57
|
+
const authState = useAuthState();
|
|
58
|
+
const LoadingComponent = loadingComponent || (() => <>{debug ? 'authenticating' : null}</>);
|
|
65
59
|
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
66
60
|
const authConfig = useAuthConfig();
|
|
67
61
|
const [token, setToken] = useState<string | boolean>(
|
|
68
|
-
(
|
|
69
|
-
auth.token ||
|
|
70
|
-
false
|
|
62
|
+
('token' in query && (query.token?.toString() || true)) || authState.token || false,
|
|
71
63
|
);
|
|
72
64
|
const [refreshToken, setRefreshToken] = useState<string | boolean>(
|
|
73
|
-
(
|
|
74
|
-
auth.refreshToken ||
|
|
75
|
-
false
|
|
65
|
+
('refreshToken' in query && (query.refreshToken?.toString() || true)) || authState.refreshToken || false,
|
|
76
66
|
);
|
|
77
67
|
|
|
78
68
|
useEffect(() => {
|
|
79
69
|
if (token !== true && refreshToken !== true) return;
|
|
80
|
-
if (debug) logger.debug(
|
|
70
|
+
if (debug) logger.debug('post message', { type: 'LOADED' });
|
|
81
71
|
(authConfig.messageHandlerKeys || []).forEach((key: string) => {
|
|
82
|
-
window?.webkit?.messageHandlers?.[key]?.postMessage(
|
|
83
|
-
JSON.stringify({ type: "LOADED" })
|
|
84
|
-
);
|
|
72
|
+
window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: 'LOADED' }));
|
|
85
73
|
});
|
|
86
|
-
window?.ReactNativeWebView?.postMessage(JSON.stringify({ type:
|
|
87
|
-
window?.parent?.postMessage(JSON.stringify({ type:
|
|
74
|
+
window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: 'LOADED' }));
|
|
75
|
+
window?.parent?.postMessage(JSON.stringify({ type: 'LOADED' }));
|
|
88
76
|
}, []);
|
|
89
77
|
|
|
90
78
|
useEffect(() => {
|
|
91
79
|
if (token !== true) return;
|
|
92
80
|
const messageCallback = (e: MessageEvent<any>) => {
|
|
93
81
|
let data = e?.data;
|
|
94
|
-
if (typeof data ===
|
|
82
|
+
if (typeof data === 'string') {
|
|
95
83
|
try {
|
|
96
84
|
data = JSON.parse(data);
|
|
97
85
|
} catch (err) {}
|
|
98
86
|
}
|
|
99
|
-
if (debug) logger.debug(
|
|
87
|
+
if (debug) logger.debug('received message', data);
|
|
100
88
|
const message: MessageSchema = data;
|
|
101
89
|
if (!message.type) return;
|
|
102
|
-
if (message.type.toUpperCase() ===
|
|
103
|
-
if (debug) logger.debug(
|
|
90
|
+
if (message.type.toUpperCase() === 'TOKEN' && message.payload) {
|
|
91
|
+
if (debug) logger.debug('setting token', message.payload);
|
|
104
92
|
setToken(message.payload);
|
|
105
93
|
}
|
|
106
94
|
};
|
|
107
95
|
if (debug)
|
|
108
|
-
logger.debug(
|
|
109
|
-
type:
|
|
110
|
-
payload:
|
|
96
|
+
logger.debug('listening for message', {
|
|
97
|
+
type: 'TOKEN',
|
|
98
|
+
payload: '<some_token>',
|
|
111
99
|
});
|
|
112
|
-
window.addEventListener(
|
|
100
|
+
window.addEventListener('message', messageCallback);
|
|
113
101
|
return () => {
|
|
114
|
-
window.removeEventListener(
|
|
102
|
+
window.removeEventListener('message', messageCallback);
|
|
115
103
|
};
|
|
116
104
|
}, []);
|
|
117
105
|
|
|
@@ -119,39 +107,39 @@ export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
|
119
107
|
if (refreshToken !== true) return;
|
|
120
108
|
const messageCallback = (e: MessageEvent<any>) => {
|
|
121
109
|
let data = e?.data;
|
|
122
|
-
if (typeof data ===
|
|
110
|
+
if (typeof data === 'string') {
|
|
123
111
|
try {
|
|
124
112
|
data = JSON.parse(data);
|
|
125
113
|
} catch (err) {}
|
|
126
114
|
}
|
|
127
|
-
if (debug) logger.debug(
|
|
115
|
+
if (debug) logger.debug('received message', data);
|
|
128
116
|
const message: MessageSchema = data;
|
|
129
117
|
if (!message.type) return;
|
|
130
|
-
if (message.type.toUpperCase() ===
|
|
131
|
-
if (debug) logger.debug(
|
|
118
|
+
if (message.type.toUpperCase() === 'REFRESH_TOKEN' && message.payload) {
|
|
119
|
+
if (debug) logger.debug('setting refresh token', message.payload);
|
|
132
120
|
setRefreshToken(message.payload);
|
|
133
121
|
}
|
|
134
122
|
};
|
|
135
123
|
if (debug)
|
|
136
|
-
logger.debug(
|
|
137
|
-
type:
|
|
138
|
-
payload:
|
|
124
|
+
logger.debug('listening for message', {
|
|
125
|
+
type: 'REFRESH_TOKEN',
|
|
126
|
+
payload: '<some_refresh_token>',
|
|
139
127
|
});
|
|
140
|
-
window.addEventListener(
|
|
128
|
+
window.addEventListener('message', messageCallback);
|
|
141
129
|
return () => {
|
|
142
|
-
window.removeEventListener(
|
|
130
|
+
window.removeEventListener('message', messageCallback);
|
|
143
131
|
};
|
|
144
132
|
}, []);
|
|
145
133
|
|
|
146
134
|
const keycloak = useMemo(
|
|
147
135
|
() =>
|
|
148
|
-
typeof window !==
|
|
136
|
+
typeof window !== 'undefined' &&
|
|
149
137
|
new Keycloak({
|
|
150
138
|
url: keycloakConfig.baseUrl,
|
|
151
139
|
realm: keycloakConfig.realm,
|
|
152
140
|
clientId: keycloakConfig.clientId,
|
|
153
141
|
}),
|
|
154
|
-
[]
|
|
142
|
+
[],
|
|
155
143
|
);
|
|
156
144
|
|
|
157
145
|
const initOptions = useMemo(() => {
|
|
@@ -159,10 +147,9 @@ export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
|
159
147
|
...defaultKeycloakInitOptions,
|
|
160
148
|
...keycloakInitOptions,
|
|
161
149
|
};
|
|
162
|
-
if (token && typeof token ===
|
|
150
|
+
if (token && typeof token === 'string') {
|
|
163
151
|
initOptions.token = token;
|
|
164
|
-
if (refreshToken && typeof refreshToken ===
|
|
165
|
-
initOptions.refreshToken = refreshToken;
|
|
152
|
+
if (refreshToken && typeof refreshToken === 'string') initOptions.refreshToken = refreshToken;
|
|
166
153
|
initOptions.checkLoginIframe = false;
|
|
167
154
|
initOptions.onLoad = undefined;
|
|
168
155
|
}
|
|
@@ -189,11 +176,7 @@ export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
|
189
176
|
}
|
|
190
177
|
if (!keycloak) return <>{children}</>;
|
|
191
178
|
return (
|
|
192
|
-
<ReactKeycloakProvider
|
|
193
|
-
initOptions={initOptions}
|
|
194
|
-
authClient={keycloak}
|
|
195
|
-
LoadingComponent={<LoadingComponent />}
|
|
196
|
-
>
|
|
179
|
+
<ReactKeycloakProvider initOptions={initOptions} authClient={keycloak} LoadingComponent={<LoadingComponent />}>
|
|
197
180
|
<AfterAuth>{children}</AfterAuth>
|
|
198
181
|
</ReactKeycloakProvider>
|
|
199
182
|
);
|
|
@@ -210,8 +193,8 @@ export const defaultKeycloakInitOptions: KeycloakInitOptions = {
|
|
|
210
193
|
checkLoginIframe: true,
|
|
211
194
|
checkLoginIframeInterval: 5,
|
|
212
195
|
enableLogging: false,
|
|
213
|
-
onLoad:
|
|
214
|
-
pkceMethod:
|
|
196
|
+
onLoad: 'check-sso',
|
|
197
|
+
pkceMethod: 'S256',
|
|
215
198
|
};
|
|
216
199
|
|
|
217
200
|
export interface MessageSchema {
|
package/src/state/index.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* File Created: 22-11-2022 17:40:53
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 24-02-2023 06:26:51
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -22,15 +22,18 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import {
|
|
26
|
-
|
|
25
|
+
import { createStateStore } from 'multiplatform.one/zustand';
|
|
26
|
+
|
|
27
|
+
const { useStore } = createStateStore(
|
|
28
|
+
'auth',
|
|
29
|
+
{
|
|
30
|
+
token: '',
|
|
31
|
+
refreshToken: '',
|
|
32
|
+
},
|
|
33
|
+
undefined,
|
|
34
|
+
{ persist: true },
|
|
35
|
+
);
|
|
27
36
|
|
|
28
37
|
export function useAuthState() {
|
|
29
|
-
return
|
|
38
|
+
return useStore();
|
|
30
39
|
}
|
|
31
|
-
|
|
32
|
-
export const authState = atom<{ token?: string; refreshToken?: string }>({
|
|
33
|
-
key: "auth",
|
|
34
|
-
default: {},
|
|
35
|
-
effects: [persistAtom],
|
|
36
|
-
});
|