Skip to content

Commit 59ba22c

Browse files
committed
fix: single call to auth/user services
1 parent 93fe769 commit 59ba22c

File tree

4 files changed

+33
-39
lines changed

4 files changed

+33
-39
lines changed

packages/agent-webapp/src/components/auth.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LitElement, css, html, nothing } from 'lit';
22
import { customElement, property, state } from 'lit/decorators.js';
3+
import { getUserInfo, AuthDetails } from '../services/auth.service.js';
34
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
45
import { styleMap } from 'lit/directives/style-map.js';
56
import personSvg from '../../assets/icons/person.svg?raw';
@@ -9,15 +10,6 @@ import githubSvg from '../../assets/providers/github.svg?inline';
910

1011
const loginRoute = '/.auth/login';
1112
const logoutRoute = '/.auth/logout';
12-
const userDetailsRoute = '/.auth/me';
13-
14-
export type AuthDetails = {
15-
identityProvider: string;
16-
userId: string;
17-
userDetails: string;
18-
userRoles: string[];
19-
claims: { typ: string; val: string }[];
20-
};
2113

2214
export type AuthComponentOptions = {
2315
strings: {
@@ -100,7 +92,7 @@ export class AuthComponent extends LitElement {
10092

10193
constructor() {
10294
super();
103-
this.getUserInfo().then((userDetails) => {
95+
getUserInfo().then((userDetails) => {
10496
this._userDetails = userDetails;
10597
this.loaded = true;
10698
});
@@ -116,12 +108,6 @@ export class AuthComponent extends LitElement {
116108
window.location.href = redirect;
117109
}
118110

119-
protected async getUserInfo() {
120-
const response = await fetch(userDetailsRoute);
121-
const payload = await response.json();
122-
return payload?.clientPrincipal;
123-
}
124-
125111
protected renderStatus = () =>
126112
html`<section class="auth-status">
127113
<span class="login-icon">${unsafeSVG(personSvg)}</span>

packages/agent-webapp/src/components/user-card.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { LitElement, css, html, nothing } from 'lit';
22
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
33
import { customElement, state } from 'lit/decorators.js';
44
import { getUserInfo } from '../services/auth.service.js';
5+
import { getUserId } from '../services/user.service.js';
56
import copySvg from '../../assets/icons/copy.svg?raw';
67
import burgerOutlineSvg from '../../assets/icons/burger-outline.svg?raw';
78
import cardSvg from '../../assets/icons/card.svg?raw';
@@ -115,12 +116,10 @@ export class UserCard extends LitElement {
115116
const authDetails = await getUserInfo();
116117
if (!authDetails) return;
117118
this.username = authDetails.userDetails;
118-
119-
const response = await fetch(`/api/me`);
120-
if (!response.ok) {
121-
throw new Error('An error occurred while fetching the user ID');
119+
const id = await getUserId();
120+
if (!id) {
121+
throw new Error('Unable to retrieve user ID');
122122
}
123-
const { id } = await response.json();
124123
this.userId = id;
125124
} catch (error) {
126125
console.error('Error fetching user ID:', error);

packages/agent-webapp/src/services/auth.service.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ export type AuthDetails = {
77
};
88

99
const userDetailsRoute = '/.auth/me';
10-
let authDetails: AuthDetails | undefined;
10+
let authDetailsPromise: Promise<AuthDetails | undefined> | undefined;
1111

1212
export async function getUserInfo(refresh = false): Promise<AuthDetails | undefined> {
13-
if (authDetails && !refresh) {
14-
return authDetails;
13+
if (!refresh && authDetailsPromise) {
14+
return authDetailsPromise;
1515
}
16-
const response = await fetch(userDetailsRoute);
17-
const payload = await response.json();
18-
authDetails = payload?.clientPrincipal;
19-
return authDetails;
16+
17+
authDetailsPromise = (async () => {
18+
const response = await fetch(userDetailsRoute);
19+
const payload = await response.json();
20+
return payload?.clientPrincipal;
21+
})();
22+
23+
return authDetailsPromise;
2024
}

packages/agent-webapp/src/services/user.service.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
import { ChatComponent } from "../components/chat";
2+
import { HistoryComponent } from "../components/history";
3+
4+
// Chat and History components are defined in index.html
5+
// with their respective ids so we can access them here
16
declare global {
27
interface Window {
3-
chatHistory: any;
4-
chat: any;
8+
chatHistory: HistoryComponent;
9+
chat: ChatComponent;
510
}
611
}
712

8-
let userId: string | undefined;
13+
let userIdPromise: Promise<string | undefined> | undefined;
914

1015
export async function getUserId(refresh = false): Promise<string | undefined> {
11-
if (userId && !refresh) {
12-
return userId;
13-
}
14-
const response = await fetch(`/api/me`);
15-
const payload = await response.json();
16-
userId = payload?.id;
17-
return userId;
16+
if (!refresh && userIdPromise) return userIdPromise;
17+
userIdPromise = (async () => {
18+
const response = await fetch(`/api/me`);
19+
const payload = await response.json();
20+
return payload?.id;
21+
})();
22+
return userIdPromise;
1823
}
1924

2025
export async function initUserSession() {
@@ -27,7 +32,7 @@ export async function initUserSession() {
2732
// Set up user ID for chat history and chat components
2833
window.chatHistory.userId = userId;
2934
window.chatHistory.addEventListener('loadSession', (e) => {
30-
const { id, messages } = e.detail;
35+
const { id, messages } = (e as CustomEvent).detail;
3136
window.chat.sessionId = id;
3237
window.chat.messages = messages;
3338
});

0 commit comments

Comments
 (0)