Skip to content

Commit

Permalink
Merge pull request #272 from iHub-PTI/fix-keycloak-auth
Browse files Browse the repository at this point in the history
Fix keycloak auth
  • Loading branch information
sebasrival authored May 24, 2024
2 parents 3a3f832 + 625e801 commit 13f8a8d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
43 changes: 9 additions & 34 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,18 @@ const App = () => {
if (keycloak.token) {
config.headers.Authorization = `Bearer ${keycloak.token}`
config.headers['Access-Control-Allow-Origin'] = '*'
}else if(!keycloak.authenticated){
//NOTE: de esta manera se puede volver a pedir iniciar sesión.
keycloak.login()
}
return config
})

useEffect(() => {
if (!ALLOWED_ROUTES.includes(window.location.pathname)) {
axios.interceptors.response.use(
response => response,
async error => {
if (error.response) {
if (error.response.status === 401 && error.response.data?.message) {
window.location.href = error.response.data.message
delete error.response.data.message
}
// The response was made and the server responded with a
// status code that is outside the 2xx range.
Sentry.setTags({
data: error.response.data,
headers: error.response.headers,
status_code: error.response.status,
})
} else if (error.request) {
// The request was made but no response was received
Sentry.setTag('request', error.request)
} else {
// Something happened while preparing the request that threw an Error
Sentry.setTag('message', error.message)
}
if (error?.response?.status !== 401) Sentry.captureException(error)
return Promise.reject(error)
}
)
if (!keycloak.authenticated) {
keycloak.login()
}
} else {
setError(false)
}
Expand Down Expand Up @@ -128,6 +108,9 @@ const App = () => {

// load the organization
useEffect(() => {

if (ALLOWED_ROUTES.includes(window.location.pathname)) return

const url = '/profile/doctor/organizations'
axios
.get(url)
Expand Down Expand Up @@ -224,14 +207,6 @@ const App = () => {
<Route>
<Redirect to='/' />
</Route>

{/* <Route exact path='/settingsnew'>
<SettingsNew />
</Route>
<Route exact path='/home'>
<Home />
</Route> */}
</Switch>
</div>
</RoomsProvider>
Expand Down
5 changes: 3 additions & 2 deletions src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Layout: React.FC<Props> = ({ children, isLoading }) => {
const { keycloak } = useKeycloak();
const { pathname } = useLocation()
const main = useRef<HTMLElement>(null)
const logout = () => keycloak.logout()

const [mobileOpen, setMobileOpen] = useState(false)
const [waitingroomOpen, setWaitingroomOpen] = useState(false)
Expand Down Expand Up @@ -278,7 +279,7 @@ const Layout: React.FC<Props> = ({ children, isLoading }) => {
className='block w-full px-4 py-2 text-sm leading-5 text-left text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900'
role='menuitem'
style={{ cursor: "pointer" }}
onClick={() => keycloak.logout()}
onClick={logout}
>
Cerrar Sesión
</button>
Expand Down Expand Up @@ -518,7 +519,7 @@ const Layout: React.FC<Props> = ({ children, isLoading }) => {
<button
className='block w-full px-4 py-2 text-sm leading-5 text-left text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900'
role='menuitem'
onClick={() => keycloak.logout()}
onClick={logout}
>
Cerrar Sesión
</button>
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ ReactDOM.render(
onLoad: 'check-sso',
silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html',
pkceMethod: 'S256',
//NOTE: no funciona con el checkLoginIframe porque renderiza y se pierde la autenticación
checkLoginIframe: false,
}}
>
<BrowserRouter>
Expand Down
File renamed without changes.

0 comments on commit 13f8a8d

Please sign in to comment.