File tree Expand file tree Collapse file tree 2 files changed +25
-17
lines changed
Expand file tree Collapse file tree 2 files changed +25
-17
lines changed Original file line number Diff line number Diff line change @@ -6,24 +6,31 @@ const keycloak = new Keycloak({
66 clientId : import . meta. env . VITE_KEYCLOAK_CLIENT_ID as string ,
77} )
88
9- try {
10- console . log ( "loading keycloak" )
11- await keycloak
12- . init ( {
9+ // Initialize function that can be awaited by importing code
10+ export const initKeycloak = async ( ) : Promise < boolean > => {
11+ try {
12+ console . log ( "loading keycloak" )
13+ const authenticated = await keycloak . init ( {
1314 onLoad : "login-required" ,
1415 flow : "implicit" ,
1516 } )
16- . then ( ( authenticated ) => {
17- if ( authenticated ) {
18- console . log ( "User is authenticated" )
19- } else {
20- console . log ( "User is not authenticated" )
21- }
22- if ( keycloak . token ) {
23- localStorage . setItem ( "token" , keycloak . token )
24- }
25- } )
26- } catch ( error ) {
27- console . error ( "Failed to initialize adapter:" , error )
17+
18+ if ( authenticated ) {
19+ console . log ( "User is authenticated" )
20+ } else {
21+ console . log ( "User is not authenticated" )
22+ }
23+
24+ if ( keycloak . token ) {
25+ localStorage . setItem ( "token" , keycloak . token )
26+ }
27+
28+ return authenticated
29+ } catch ( error : unknown ) {
30+ // Properly type the error and safely log it
31+ console . error ( "Failed to initialize adapter:" , error instanceof Error ? error . message : String ( error ) )
32+ return false
33+ }
2834}
35+
2936export default keycloak
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ import OfferedQuotesPage from "./pages/quotes/OfferedQuotesPage"
2020import DeniedQuotesPage from "./pages/quotes/DeniedQuotesPage"
2121// import ExpiredQuotesPage from "./pages/quotes/ExpiredQuotesPage"
2222import RejectedQuotesPage from "./pages/quotes/RejectedQuotesPage"
23- import "./keycloak"
23+ import { initKeycloak } from "./keycloak"
2424import "./lib/api-client"
2525
2626const queryClient = new QueryClient ( {
@@ -32,6 +32,7 @@ const queryClient = new QueryClient({
3232} )
3333
3434const prepare = async ( ) => {
35+ await initKeycloak ( )
3536 if ( meta . apiMocksEnabled ) {
3637 const { worker } = await import ( "./mocks/browser" )
3738 await worker . start ( )
You can’t perform that action at this time.
0 commit comments