@multiplatform.one/keycloak 0.0.1
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/README.md +3 -0
- package/esm/Authenticated.js +53 -0
- package/esm/Authenticated.js.map +1 -0
- package/esm/authConfig.js +30 -0
- package/esm/authConfig.js.map +1 -0
- package/esm/hooks/index.js +28 -0
- package/esm/hooks/index.js.map +1 -0
- package/esm/hooks/useAuthConfig.js +30 -0
- package/esm/hooks/useAuthConfig.js.map +1 -0
- package/esm/hooks/useCurrentRouteName.js +38 -0
- package/esm/hooks/useCurrentRouteName.js.map +1 -0
- package/esm/hooks/useKeycloak/index.js +27 -0
- package/esm/hooks/useKeycloak/index.js.map +1 -0
- package/esm/hooks/useKeycloak/useKeycloak.js +62 -0
- package/esm/hooks/useKeycloak/useKeycloak.js.map +1 -0
- package/esm/hooks/useKeycloak/useKeycloak.native.js +50 -0
- package/esm/hooks/useKeycloak/useKeycloak.native.js.map +1 -0
- package/esm/hooks/useLogin.js +68 -0
- package/esm/hooks/useLogin.js.map +1 -0
- package/esm/index.js +29 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +1 -0
- package/esm/provider/afterAuth.js +76 -0
- package/esm/provider/afterAuth.js.map +1 -0
- package/esm/provider/index.js +60 -0
- package/esm/provider/index.js.map +1 -0
- package/esm/provider/keycloakProvider.js +220 -0
- package/esm/provider/keycloakProvider.js.map +1 -0
- package/esm/provider/keycloakProvider.native.js +40 -0
- package/esm/provider/keycloakProvider.native.js.map +1 -0
- package/esm/state/index.js +35 -0
- package/esm/state/index.js.map +1 -0
- package/esm/types.js +26 -0
- package/esm/types.js.map +1 -0
- package/lib/Authenticated.d.ts +32 -0
- package/lib/Authenticated.js +52 -0
- package/lib/Authenticated.js.map +1 -0
- package/lib/authConfig.d.ts +32 -0
- package/lib/authConfig.js +53 -0
- package/lib/authConfig.js.map +1 -0
- package/lib/hooks/index.d.ts +26 -0
- package/lib/hooks/index.js +56 -0
- package/lib/hooks/index.js.map +1 -0
- package/lib/hooks/useAuthConfig.d.ts +24 -0
- package/lib/hooks/useAuthConfig.js +50 -0
- package/lib/hooks/useAuthConfig.js.map +1 -0
- package/lib/hooks/useCurrentRouteName.d.ts +24 -0
- package/lib/hooks/useCurrentRouteName.js +56 -0
- package/lib/hooks/useCurrentRouteName.js.map +1 -0
- package/lib/hooks/useKeycloak/index.d.ts +32 -0
- package/lib/hooks/useKeycloak/index.js +36 -0
- package/lib/hooks/useKeycloak/index.js.map +1 -0
- package/lib/hooks/useKeycloak/useKeycloak.d.ts +25 -0
- package/lib/hooks/useKeycloak/useKeycloak.js +56 -0
- package/lib/hooks/useKeycloak/useKeycloak.js.map +1 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +32 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.js +73 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.js.map +1 -0
- package/lib/hooks/useLogin.d.ts +24 -0
- package/lib/hooks/useLogin.js +74 -0
- package/lib/hooks/useLogin.js.map +1 -0
- package/lib/index.d.ts +27 -0
- package/lib/index.js +66 -0
- package/lib/index.js.map +1 -0
- package/lib/package.json +1 -0
- package/lib/provider/afterAuth.d.ts +28 -0
- package/lib/provider/afterAuth.js +78 -0
- package/lib/provider/afterAuth.js.map +1 -0
- package/lib/provider/index.d.ts +31 -0
- package/lib/provider/index.js +76 -0
- package/lib/provider/index.js.map +1 -0
- package/lib/provider/keycloakProvider.d.ts +46 -0
- package/lib/provider/keycloakProvider.js +214 -0
- package/lib/provider/keycloakProvider.js.map +1 -0
- package/lib/provider/keycloakProvider.native.d.ts +26 -0
- package/lib/provider/keycloakProvider.native.js +37 -0
- package/lib/provider/keycloakProvider.native.js.map +1 -0
- package/lib/state/index.d.ts +34 -0
- package/lib/state/index.js +59 -0
- package/lib/state/index.js.map +1 -0
- package/lib/tsconfig.build.tsbuildinfo +1 -0
- package/lib/types.d.ts +39 -0
- package/lib/types.js +48 -0
- package/lib/types.js.map +1 -0
- package/package.json +141 -0
- package/src/@types/expoKeycloakAuth.d.ts +28 -0
- package/src/Authenticated.tsx +56 -0
- package/src/authConfig.ts +38 -0
- package/src/hooks/index.ts +27 -0
- package/src/hooks/useAuthConfig.ts +30 -0
- package/src/hooks/useCurrentRouteName.ts +37 -0
- package/src/hooks/useKeycloak/index.ts +34 -0
- package/src/hooks/useKeycloak/useKeycloak.native.ts +48 -0
- package/src/hooks/useKeycloak/useKeycloak.ts +45 -0
- package/src/hooks/useLogin.ts +50 -0
- package/src/index.ts +28 -0
- package/src/provider/afterAuth.tsx +56 -0
- package/src/provider/index.tsx +57 -0
- package/src/provider/keycloakProvider.native.tsx +42 -0
- package/src/provider/keycloakProvider.tsx +220 -0
- package/src/state/index.ts +36 -0
- package/src/types.ts +40 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/@types/expoKeycloakAuth.d.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 25-11-2022 10:22:18
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:22:24
|
|
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
|
+
declare module "expo-keycloak-auth" {
|
|
26
|
+
export const KeycloakProvider: any;
|
|
27
|
+
export const useKeycloak: any;
|
|
28
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /auth/Authenticated.tsx
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 05:58:23
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 22-11-2022 06:26:38
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 React, { ComponentType, FC, ReactNode } from "react";
|
|
26
|
+
import { useLogin, useKeycloak } from "./hooks";
|
|
27
|
+
|
|
28
|
+
export interface AuthenticatedProps {
|
|
29
|
+
children: ReactNode;
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
loggedOutComponent?: ComponentType;
|
|
32
|
+
loginRoute?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const Authenticated: FC<AuthenticatedProps> = (
|
|
36
|
+
props: AuthenticatedProps
|
|
37
|
+
) => {
|
|
38
|
+
const { authenticated } = useKeycloak();
|
|
39
|
+
const login = useLogin(props.loginRoute);
|
|
40
|
+
if (!props.disabled && !authenticated) {
|
|
41
|
+
if (authenticated === false) login();
|
|
42
|
+
const LoggedOutComponent = props.loggedOutComponent || (() => null);
|
|
43
|
+
return <LoggedOutComponent />;
|
|
44
|
+
}
|
|
45
|
+
return <>{props.children}</>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export function withAuthenticated<P extends object>(
|
|
49
|
+
Component: ComponentType<P>
|
|
50
|
+
) {
|
|
51
|
+
return (props: P) => (
|
|
52
|
+
<Authenticated>
|
|
53
|
+
<Component {...props} />
|
|
54
|
+
</Authenticated>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/authConfig.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 09:21:15
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:29:29
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 { createContext } from "react";
|
|
26
|
+
|
|
27
|
+
export const defaultAuthConfig: AuthConfig = {
|
|
28
|
+
ssr: true,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const AuthConfigContext = createContext<AuthConfig>(defaultAuthConfig);
|
|
32
|
+
|
|
33
|
+
export interface AuthConfig {
|
|
34
|
+
debug?: boolean;
|
|
35
|
+
loginRoute?: string;
|
|
36
|
+
messageHandlerKeys?: string[];
|
|
37
|
+
ssr?: boolean;
|
|
38
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/index.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 06:34:56
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 09:52:31
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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
|
+
export * from "./useCurrentRouteName";
|
|
26
|
+
export * from "./useKeycloak";
|
|
27
|
+
export * from "./useLogin";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useAuthConfig.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 09:20:41
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 09:52:31
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 { useContext } from "react";
|
|
26
|
+
import { AuthConfigContext } from "../authConfig";
|
|
27
|
+
|
|
28
|
+
export function useAuthConfig() {
|
|
29
|
+
return useContext(AuthConfigContext);
|
|
30
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useCurrentRouteName.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 08:38:23
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:24:27
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 { MultiPlatform } from "multiplatform.one";
|
|
26
|
+
import { useRoute } from "@react-navigation/native";
|
|
27
|
+
import { useRouter } from "next/router";
|
|
28
|
+
|
|
29
|
+
export function useCurrentRouteName() {
|
|
30
|
+
if (MultiPlatform.isStorybook()) return null;
|
|
31
|
+
if (MultiPlatform.isNext()) {
|
|
32
|
+
const router = useRouter();
|
|
33
|
+
return router.route;
|
|
34
|
+
}
|
|
35
|
+
const route = useRoute();
|
|
36
|
+
return route.name;
|
|
37
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useKeycloak/index.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 14:10:44
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 09:52:31
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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
|
+
export * from "./useKeycloak";
|
|
26
|
+
|
|
27
|
+
export interface IKeycloak {
|
|
28
|
+
authenticated?: boolean;
|
|
29
|
+
token?: string;
|
|
30
|
+
refreshToken?: string;
|
|
31
|
+
tokenParsed?: any;
|
|
32
|
+
login: () => unknown;
|
|
33
|
+
logout: () => unknown;
|
|
34
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useKeycloak/useKeycloak.native.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 14:10:25
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:21:56
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 { useKeycloak as useKeycloakExpo } from "expo-keycloak-auth";
|
|
26
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
27
|
+
import { IKeycloak } from "./index";
|
|
28
|
+
|
|
29
|
+
export function useKeycloak() {
|
|
30
|
+
if (MultiPlatform.isStorybook()) return { authenticated: true } as IKeycloak;
|
|
31
|
+
const { ready, login, isLoggedIn, token, logout } = useKeycloakExpo();
|
|
32
|
+
const keycloak = new ExpoKeycloak(ready, isLoggedIn, login, logout, token);
|
|
33
|
+
return keycloak as IKeycloak;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class ExpoKeycloak implements IKeycloak {
|
|
37
|
+
authenticated?: boolean;
|
|
38
|
+
|
|
39
|
+
constructor(
|
|
40
|
+
ready: boolean,
|
|
41
|
+
isLoggedIn: boolean,
|
|
42
|
+
public login: () => unknown,
|
|
43
|
+
public logout: () => unknown,
|
|
44
|
+
public token?: string
|
|
45
|
+
) {
|
|
46
|
+
if (ready) this.authenticated = isLoggedIn;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useKeycloak/useKeycloak.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 06:04:59
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:22:57
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 Keycloak from "keycloak-js";
|
|
26
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
27
|
+
import { useKeycloak as useReactKeycloak } from "@react-keycloak/web";
|
|
28
|
+
import { useKeycloak as useSsrKeycloak } from "@react-keycloak/ssr";
|
|
29
|
+
import { IKeycloak } from "./index";
|
|
30
|
+
import { useAuthConfig } from "../useAuthConfig";
|
|
31
|
+
|
|
32
|
+
export function useKeycloak() {
|
|
33
|
+
const authConfig = useAuthConfig();
|
|
34
|
+
if (MultiPlatform.isStorybook()) return { authenticated: true } as IKeycloak;
|
|
35
|
+
if (MultiPlatform.isNext() && authConfig.ssr) {
|
|
36
|
+
const { keycloak: ssrKeycloak, initialized } = useSsrKeycloak();
|
|
37
|
+
const keycloak = { ...ssrKeycloak } as Keycloak;
|
|
38
|
+
if (!initialized) keycloak.authenticated = undefined;
|
|
39
|
+
return keycloak as IKeycloak;
|
|
40
|
+
}
|
|
41
|
+
const { keycloak: reactKeycloak, initialized } = useReactKeycloak();
|
|
42
|
+
const keycloak = { ...reactKeycloak } as Keycloak;
|
|
43
|
+
if (!initialized) keycloak.authenticated = undefined;
|
|
44
|
+
return keycloak as IKeycloak;
|
|
45
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useLogin.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 08:49:14
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:36:32
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 { useRouter } from "@risserlabs/solito/router";
|
|
26
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
27
|
+
import { useCurrentRouteName } from "./useCurrentRouteName";
|
|
28
|
+
import { useKeycloak } from "./useKeycloak/useKeycloak";
|
|
29
|
+
import { useAuthConfig } from "./useAuthConfig";
|
|
30
|
+
|
|
31
|
+
export function useLogin(loginRoute?: string) {
|
|
32
|
+
if (MultiPlatform.isStorybook()) return () => null;
|
|
33
|
+
const keycloak = useKeycloak();
|
|
34
|
+
const authConfig = useAuthConfig();
|
|
35
|
+
const currentRouteName = useCurrentRouteName();
|
|
36
|
+
const router = useRouter();
|
|
37
|
+
return () => {
|
|
38
|
+
if (!loginRoute) loginRoute = authConfig.loginRoute;
|
|
39
|
+
if (!loginRoute) {
|
|
40
|
+
keycloak.login();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const url = new URL(`x://${loginRoute}`);
|
|
44
|
+
const searchParams = new URLSearchParams(url.search);
|
|
45
|
+
if (currentRouteName) searchParams.append("backTo", currentRouteName);
|
|
46
|
+
let query = searchParams.toString();
|
|
47
|
+
if (query.length) query = `?${query}`;
|
|
48
|
+
router.push(`${url.pathname.substring(2)}${query}`);
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/index.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 25-11-2022 09:51:55
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 09:52:06
|
|
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
|
+
export * from "./Authenticated";
|
|
26
|
+
export * from "./authConfig";
|
|
27
|
+
export * from "./hooks";
|
|
28
|
+
export * from "./provider";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /auth/provider/afterAuth.tsx
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 22-11-2022 17:49:58
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 22-11-2022 18:00:39
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 React, { FC, ReactNode, useEffect } from "react";
|
|
26
|
+
import { useKeycloak } from "../hooks";
|
|
27
|
+
import { useAuthConfig } from "../hooks/useAuthConfig";
|
|
28
|
+
import { useAuthState } from "../state";
|
|
29
|
+
|
|
30
|
+
export interface AfterAuthProps {
|
|
31
|
+
children: ReactNode;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const logger = console;
|
|
35
|
+
|
|
36
|
+
export const AfterAuth: FC<AfterAuthProps> = ({ children }: AfterAuthProps) => {
|
|
37
|
+
const authConfig = useAuthConfig();
|
|
38
|
+
const { token, refreshToken, authenticated } = useKeycloak();
|
|
39
|
+
const [, setAuth] = useAuthState();
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!authenticated) return;
|
|
43
|
+
if (token) {
|
|
44
|
+
setAuth({
|
|
45
|
+
token,
|
|
46
|
+
...(refreshToken ? { refreshToken } : {}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}, [token, refreshToken]);
|
|
50
|
+
|
|
51
|
+
if (authConfig.debug) {
|
|
52
|
+
logger.debug("authenticated", authenticated);
|
|
53
|
+
logger.debug("token", token);
|
|
54
|
+
}
|
|
55
|
+
return <>{children}</>;
|
|
56
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /auth/provider/index.tsx
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 06:33:07
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 22-11-2022 18:00:13
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 React, { FC, useMemo } from "react";
|
|
26
|
+
import {
|
|
27
|
+
AuthConfig,
|
|
28
|
+
AuthConfigContext,
|
|
29
|
+
defaultAuthConfig,
|
|
30
|
+
} from "../authConfig";
|
|
31
|
+
import { KeycloakProvider, KeycloakProviderProps } from "./keycloakProvider";
|
|
32
|
+
|
|
33
|
+
export interface AuthProviderProps extends KeycloakProviderProps {
|
|
34
|
+
authConfig?: Partial<AuthConfig>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const AuthProvider: FC<AuthProviderProps> = (
|
|
38
|
+
props: AuthProviderProps
|
|
39
|
+
) => {
|
|
40
|
+
const { authConfig, debug } = props;
|
|
41
|
+
const authConfigValue = useMemo(
|
|
42
|
+
() => ({
|
|
43
|
+
...defaultAuthConfig,
|
|
44
|
+
...authConfig,
|
|
45
|
+
debug: !!debug,
|
|
46
|
+
}),
|
|
47
|
+
[]
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<AuthConfigContext.Provider value={authConfigValue}>
|
|
52
|
+
<KeycloakProvider {...props} />
|
|
53
|
+
</AuthConfigContext.Provider>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export * from "./keycloakProvider";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /auth/provider/keycloakProvider.native.tsx
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 03:12:37
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 22-11-2022 17:52:31
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 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 { KeycloakProvider as ExpoKeycloakProvider } from "expo-keycloak-auth";
|
|
26
|
+
import React, { FC } from "react";
|
|
27
|
+
import type { KeycloakProviderProps } from "./keycloakProvider";
|
|
28
|
+
import { AfterAuth } from "./afterAuth";
|
|
29
|
+
|
|
30
|
+
export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
31
|
+
children,
|
|
32
|
+
keycloakConfig,
|
|
33
|
+
}: KeycloakProviderProps & { keycloakConfig: { url?: string } }) => {
|
|
34
|
+
keycloakConfig.url = keycloakConfig.baseUrl;
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
delete keycloakConfig.baseUrl;
|
|
37
|
+
return (
|
|
38
|
+
<ExpoKeycloakProvider {...keycloakConfig}>
|
|
39
|
+
<AfterAuth>{children}</AfterAuth>
|
|
40
|
+
</ExpoKeycloakProvider>
|
|
41
|
+
);
|
|
42
|
+
};
|