Skip to content

Commit

Permalink
Merge pull request #151 from ajithsimons/update/keycloak
Browse files Browse the repository at this point in the history
fix: set state issue fix in app layout
  • Loading branch information
almadigabor authored Jul 11, 2024
2 parents afd131b + 5cece1a commit deeee05
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
5 changes: 0 additions & 5 deletions src/components/layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import { Box } from '@mui/material';

import { useGetPermissionsQuery, useGetUseCasesQuery } from '../../features/app/apiSlice';
import { setLoggedInUser } from '../../features/app/slice';
import { useAppDispatch } from '../../features/store';
import Nav from '../Nav';
import Sidebar from '../sidebar';

Expand All @@ -30,9 +28,6 @@ export default function AppLayout(props: any) {
useGetUseCasesQuery({});
useGetPermissionsQuery({});

const dispatch = useAppDispatch();
dispatch(setLoggedInUser(props.loggedUser));

return (
<Box sx={{ my: 0, mx: 'auto', overflowY: 'auto', overflowX: 'hidden', height: '100vh' }}>
<Box>
Expand Down
2 changes: 0 additions & 2 deletions src/features/app/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const initialState: IAppSlice = {
company: '',
bpn: '',
tenant: '',
token: '',
parsedToken: {},
},
permissions: [],
useCases: [],
Expand Down
4 changes: 0 additions & 4 deletions src/features/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
import { KeycloakTokenParsed } from 'keycloak-js';

import { IAlertColors } from '../notifiication/types';
export interface IUser {
userName: string;
Expand All @@ -27,8 +25,6 @@ export interface IUser {
company: string;
bpn: string;
tenant: string;
token: string;
parsedToken: KeycloakTokenParsed;
}
export interface IAppSlice {
pageLoading: boolean;
Expand Down
22 changes: 11 additions & 11 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/********************************************************************************
* Copyright (c) 2021,2022,2023 T-Systems International GmbH
* Copyright (c) 2022,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022,2024 T-Systems International GmbH
* Copyright (c) 2022,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -17,10 +17,11 @@
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

import Keycloak from 'keycloak-js';

import { setLoggedInUser } from '../features/app/slice';
import { IUser } from '../features/app/types';
import { store } from '../features/store';
import { getCentralIdp, getClientId, getClientRealm } from './EnvironmentService';

const keycloakConfig: Keycloak.KeycloakConfig = {
Expand Down Expand Up @@ -66,34 +67,33 @@ const getLoggedUser = () => ({
company: getCompany(),
bpn: getBpn(),
tenant: getTenant(),
token: getToken(),
parsedToken: getParsedToken(),
});

const update = () => {
KC.updateToken(50)
KC.updateToken(600)
.then((refreshed: boolean) => {
if (refreshed) console.log(`${getUsername()} token refreshed ${refreshed}`);
console.log(`${getUsername()} token refreshed ${refreshed}`);
})
.catch(() => {
console.log(`${getUsername()} token refresh failed`);
});
};

const initKeycloak = (onAuthenticatedCallback: (loggedUser: IUser) => unknown) => {
const initKeycloak = (onAuthenticatedCallback: (loggedUser: IUser) => void) => {
KC.init({
onLoad: 'login-required',
pkceMethod: 'S256',
enableLogging: true,
})
.then(authenticated => {
if (authenticated) {
onAuthenticatedCallback(getLoggedUser());
setInterval(update, 50000);
store.dispatch(setLoggedInUser(getLoggedUser()));
} else {
doLogin();
console.log(`${getUsername()} authentication failed`);
}
})
.catch(console.error);
.catch(err => console.log(err));
};

KC.onTokenExpired = () => {
Expand Down

0 comments on commit deeee05

Please sign in to comment.