Skip to content

Commit aa4b247

Browse files
Merge remote-tracking branch 'origin/main'
2 parents a79e82c + ee65d7d commit aa4b247

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/app/services/authorization.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { handleError } from "./helpers";
2+
import { jwtDecode } from "jwt-decode";
23

34
interface openIdConfig {
45
authorizationEndpoint?: string;
@@ -200,6 +201,8 @@ export default class AuthService {
200201
}
201202

202203
private async requestTokens(authorizationCode: string) {
204+
if (!authorizationCode || !this.tokenEndpoint?.trim()) return;
205+
203206
const tokenRequestBody = new URLSearchParams({
204207
grant_type: "authorization_code",
205208
code: authorizationCode,
@@ -233,10 +236,20 @@ export default class AuthService {
233236
const tokens = await response.json();
234237

235238
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!, {
237250
method: "GET",
238251
headers: {
239-
Authorization: `Bearer ${tokens?.access_token}`,
252+
Authorization: `Bearer ${accessToken}`,
240253
"Content-Type": "application/x-www-form-urlencoded",
241254
},
242255
});
@@ -247,11 +260,17 @@ export default class AuthService {
247260
return;
248261
}
249262

250-
const userinfo = await userinfoResponse.json();
251-
tokens["userinfo_token"] = userinfo;
252-
}
263+
const response = await userinfoResponse.text();
253264

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+
}
255274
}
256275

257276
isAuthenticated() {

0 commit comments

Comments
 (0)