1
1
import { handleError } from "./helpers" ;
2
+ import { jwtDecode } from "jwt-decode" ;
2
3
3
4
interface openIdConfig {
4
5
authorizationEndpoint ?: string ;
@@ -200,6 +201,8 @@ export default class AuthService {
200
201
}
201
202
202
203
private async requestTokens ( authorizationCode : string ) {
204
+ if ( ! authorizationCode || ! this . tokenEndpoint ?. trim ( ) ) return ;
205
+
203
206
const tokenRequestBody = new URLSearchParams ( {
204
207
grant_type : "authorization_code" ,
205
208
code : authorizationCode ,
@@ -233,10 +236,20 @@ export default class AuthService {
233
236
const tokens = await response . json ( ) ;
234
237
235
238
if ( this . userinfoEndpoint && tokens ?. access_token ) {
236
- const userinfoResponse = await fetch ( this . userinfoEndpoint , {
239
+ const userinfo = await this . fetchUserInfo ( tokens . access_token ) ;
240
+ if ( userinfo ) {
241
+ tokens [ "userinfo_token" ] = userinfo ;
242
+ }
243
+ }
244
+ sessionStorage . setItem ( "tokens" , JSON . stringify ( tokens || { } ) ) ;
245
+ }
246
+
247
+ private async fetchUserInfo ( accessToken : string ) {
248
+ try {
249
+ const userinfoResponse = await fetch ( this . userinfoEndpoint ! , {
237
250
method : "GET" ,
238
251
headers : {
239
- Authorization : `Bearer ${ tokens ?. access_token } ` ,
252
+ Authorization : `Bearer ${ accessToken } ` ,
240
253
"Content-Type" : "application/x-www-form-urlencoded" ,
241
254
} ,
242
255
} ) ;
@@ -247,11 +260,17 @@ export default class AuthService {
247
260
return ;
248
261
}
249
262
250
- const userinfo = await userinfoResponse . json ( ) ;
251
- tokens [ "userinfo_token" ] = userinfo ;
252
- }
263
+ const response = await userinfoResponse . text ( ) ;
253
264
254
- sessionStorage . setItem ( "tokens" , JSON . stringify ( tokens || { } ) ) ;
265
+ try {
266
+ return JSON . parse ( response ) ;
267
+ } catch {
268
+ return jwtDecode ( response ) ;
269
+ }
270
+ } catch ( err ) {
271
+ console . error ( "Failed to fetch userinfo:" , err ) ;
272
+ return null ;
273
+ }
255
274
}
256
275
257
276
isAuthenticated ( ) {
0 commit comments