From 37188ff30ae65435d3b0e86c48ac917d783f34ba Mon Sep 17 00:00:00 2001 From: Hossein Salari Date: Thu, 26 Feb 2026 13:22:23 +0330 Subject: [PATCH] active sso feature --- lib/api.js | 37 +++++++++++++++++------------------ lib/keycloak.js | 2 +- pages/_app.js | 46 +++++++++++++++++++++++++++----------------- pages/index.js | 51 +++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 91 insertions(+), 45 deletions(-) diff --git a/lib/api.js b/lib/api.js index 6e4f5b9..7ff8e23 100644 --- a/lib/api.js +++ b/lib/api.js @@ -9,10 +9,10 @@ const axiosInstance = axios.create({ axiosInstance.interceptors.request.use( async (config) => { - // if (keycloak?.authenticated) { - // config.headers.Authorization = `Bearer ${keycloak.token}`; - // await keycloak.updateToken(30); - // } + if (keycloak?.authenticated) { + config.headers.Authorization = `Bearer ${keycloak.token}`; + await keycloak.updateToken(30); + } return config; }, (error) => Promise.reject(error), @@ -23,22 +23,21 @@ axiosInstance.interceptors.response.use( async (error) => { const originalRequest = error.config; - // if (error?.response?.status === 401 && !originalRequest._retry) { - // originalRequest._retry = true; - // keycloak.login(); - // try { - // await keycloak.updateToken(30); - // const newToken = keycloak.token; + if (error?.response?.status === 401 && !originalRequest._retry) { + originalRequest._retry = true; + keycloak.login(); + try { + await keycloak.updateToken(30); + const newToken = keycloak.token; - // if (newToken) { - // originalRequest.headers["Authorization"] = `Bearer ${newToken}`; - // return axiosInstance(originalRequest); - // } - // } catch (refreshError) { - // localStorage.removeItem("access_account"); - // keycloak.logout({ redirectUri: window.location.origin }); - // } - // } + if (newToken) { + originalRequest.headers["Authorization"] = `Bearer ${newToken}`; + return axiosInstance(originalRequest); + } + } catch (refreshError) { + keycloak.logout({ redirectUri: window.location.origin }); + } + } return Promise.reject(error); }, diff --git a/lib/keycloak.js b/lib/keycloak.js index a7fa485..8870573 100644 --- a/lib/keycloak.js +++ b/lib/keycloak.js @@ -3,7 +3,7 @@ import Keycloak from "keycloak-js"; const keycloak = new Keycloak({ url: "https://auth.ibagher.ir", realm: "bi", - clientId: "rasad", + clientId: "generative", }); export const initKeycloak = () => diff --git a/pages/_app.js b/pages/_app.js index cd6662d..4caa406 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -1,32 +1,44 @@ import "@/styles/globals.css"; -import { ChakraProvider } from "@chakra-ui/react"; +import { ChakraProvider, Spinner } from "@chakra-ui/react"; import { QueryParamProvider } from "use-query-params"; import { NextAdapter } from "next-query-params"; import { theme } from "@/components/theme"; -import { useKeycloak } from "@/contexts/KeycloakContext"; +import { KeycloakProvider, useKeycloak } from "@/contexts/KeycloakContext"; import { SWRConfig } from "swr"; import { fetcher } from "@/lib/api"; import { Fonts } from "@/components/base/global"; const Adapter = (props) => ; -export default function App({ Component, pageProps }) { - // const { initialized, authenticated } = useKeycloak(); +function AppWrapper({ Component, pageProps }) { + const { initialized, authenticated } = useKeycloak(); - // if (!initialized) return ; - // if (!authenticated) return
Redirecting to login...
; + if (!initialized) return ; + if (!authenticated) return
Redirecting to login...
; return ( - - - - - - - - + + + + + + + + + + ); } + +function App(props) { + return ( + + + + ); +} + +export default App; diff --git a/pages/index.js b/pages/index.js index 30446e8..4994cc1 100644 --- a/pages/index.js +++ b/pages/index.js @@ -30,6 +30,8 @@ import { withDefault, } from "use-query-params"; import { BsChatText } from "react-icons/bs"; +import { AiOutlinePoweroff } from "react-icons/ai"; +import keycloak from "@/lib/keycloak"; export default function Home() { const [filters, setFilters] = useQueryParams({ @@ -52,15 +54,19 @@ export default function Home() { borderLeft="1px solid" borderColor="gray.200" py={8} - overflowY={"auto"} - maxH={"calc(100vh - 92px)"} - sx={{ - "::-webkit-scrollbar": { display: "none" }, - "scrollbar-width": "none", - "-ms-overflow-style": "none", - }} + position={"relative"} > - + {/* Menu */} + + + keycloak.logout()} + > + + خروج از حساب + + {/* Content */}