@@ -33,7 +33,7 @@ export class AuthService {
3333 async login ( ) {
3434 const codeVerifier = this . generateRandomString ( ) ;
3535 const codeChallenge = await this . generateCodeChallenge ( codeVerifier ) ;
36- localStorage . setItem ( 'code_verifier' , codeVerifier ) ;
36+ sessionStorage . setItem ( 'code_verifier' , codeVerifier ) ;
3737 const url =
3838 `${ this . authUrl } ` +
3939 `?client_id=${ encodeURIComponent ( this . clientId ) } ` +
@@ -105,56 +105,51 @@ export class AuthService {
105105 return base64 ;
106106 }
107107
108- exchangeCodeForTokens ( code : string , codeVerifier : string | null ) {
108+ async exchangeCodeForTokens ( code : string , codeVerifier : string | null ) : Promise < void > {
109109 const payload = {
110110 code : code ,
111111 code_verifier : codeVerifier || ''
112112 } ;
113113
114- fetch ( this . api + 'authentication/token' , {
115- method : 'POST' ,
116- headers : {
117- 'Content-Type' : 'application/json'
118- } ,
119- body : JSON . stringify ( payload )
120- } )
121- . then ( ( res ) => {
122- if ( ! res . ok ) {
123- throw new Error ( `Error exchanging code: ${ res . status } ${ res . statusText } ` ) ;
124- }
125- return res . json ( ) ;
126- } )
127- . then (
128- ( tokens : {
129- access_token : string ;
130- id_token : string ;
131- refresh_token : string ;
132- expires_in : number ;
133- token_type : string ;
134- } ) => {
135- const token : OAuth2Token = {
136- access_token : tokens . access_token ,
137- token_type : tokens . token_type ,
138- refresh_token : tokens . refresh_token ,
139- expires_in : tokens . expires_in ,
140- scope : 'Bearer'
141- } ;
142-
143- localStorage . setItem ( 'id_token' , tokens . id_token ) ;
144- localStorage . setItem ( 'mifosXZitadel' , 'true' ) ;
145- sessionStorage . setItem ( 'mifosXZitadelTokenDetails' , JSON . stringify ( token ) ) ;
146- localStorage . setItem ( 'refresh_token' , tokens . refresh_token ) ;
147- this . scheduleRefresh ( tokens . expires_in ) ;
148- localStorage . removeItem ( 'auth_code' ) ;
149- localStorage . removeItem ( 'code_verifier' ) ;
150- this . userdetails ( ) ;
151- }
152- )
153- . catch ( ( error ) => {
154- localStorage . removeItem ( 'auth_code' ) ;
155- localStorage . removeItem ( 'code_verifier' ) ;
156- window . location . href = '/#/login' ;
114+ try {
115+ const response = await fetch ( this . api + 'authentication/token' , {
116+ method : 'POST' ,
117+ headers : {
118+ 'Content-Type' : 'application/json'
119+ } ,
120+ body : JSON . stringify ( payload )
157121 } ) ;
122+
123+ if ( ! response . ok ) {
124+ throw new Error ( `Error exchanging code: ${ response . status } ${ response . statusText } ` ) ;
125+ }
126+
127+ const tokens : {
128+ access_token : string ;
129+ id_token : string ;
130+ refresh_token : string ;
131+ expires_in : number ;
132+ token_type : string ;
133+ } = await response . json ( ) ;
134+
135+ const token : OAuth2Token = {
136+ access_token : tokens . access_token ,
137+ token_type : tokens . token_type ,
138+ refresh_token : tokens . refresh_token ,
139+ expires_in : tokens . expires_in ,
140+ scope : 'Bearer'
141+ } ;
142+
143+ sessionStorage . setItem ( 'mifosXZitadelTokenDetails' , JSON . stringify ( token ) ) ;
144+ localStorage . setItem ( 'id_token' , tokens . id_token ) ;
145+ localStorage . setItem ( 'refresh_token' , tokens . refresh_token ) ;
146+ localStorage . setItem ( 'mifosXZitadel' , 'true' ) ;
147+ this . scheduleRefresh ( tokens . expires_in ) ;
148+ await this . userdetails ( ) ;
149+ } catch ( error ) {
150+ window . location . href = '/#/login' ;
151+ throw error ;
152+ }
158153 }
159154
160155 userdetails ( ) {
0 commit comments