parent
bc7db43a1a
commit
2d9ff8831a
@ -1,21 +1,17 @@
|
|||||||
import { useStore } from "@nanostores/react";
|
import { useKeycloak } from "@react-keycloak/web";
|
||||||
import { Spin } from "antd";
|
|
||||||
|
|
||||||
import { Navigate } from "react-router-dom";
|
|
||||||
|
|
||||||
import { isAuthorized$, userInfoLoading$ } from "./stores/auth";
|
|
||||||
|
|
||||||
export function WithAuth(props) {
|
export function WithAuth(props) {
|
||||||
const isAuthorized = useStore(isAuthorized$);
|
const { keycloak } = useKeycloak();
|
||||||
const userInfoLoading = useStore(userInfoLoading$);
|
|
||||||
|
|
||||||
if (userInfoLoading) {
|
const login = async () => {
|
||||||
return <Spin className="user-info-loader" />;
|
await keycloak.login();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAuthorized) {
|
if (keycloak.authenticated) {
|
||||||
return <>{props.children}</>;
|
return <>{props.children}</>;
|
||||||
|
} else {
|
||||||
|
login()
|
||||||
}
|
}
|
||||||
|
|
||||||
return <Navigate to="/signin" replace={true} />;
|
return <></>;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
import Keycloak from "keycloak-js";
|
||||||
|
|
||||||
|
export const keycloak = new Keycloak({
|
||||||
|
url: import.meta.env.VITE_KEYCLOAK_URL,
|
||||||
|
realm: "SST",
|
||||||
|
clientId: import.meta.env.VITE_KEYCLOAK_CLIENT_ID,
|
||||||
|
});
|
||||||
Loading…
Reference in new issue