11// src/config.js
22
3- import { UserInfo } from "@/models" ;
3+ import { UserInfo , claim } from "@/models" ;
4+
45
56declare global {
67 interface Window {
78 appConfig ?: Record < string , any > ;
89 activeUserId ?: string ;
9- userInfo ?: UserInfo [ ] ;
10+ userInfo ?: UserInfo ;
1011 }
1112}
1213
1314export let API_URL : string | null = null ;
1415export let USER_ID : string | null = null ;
16+ export let USER_INFO : UserInfo | null = null ;
1517
1618export let config = {
1719 API_URL : "http://localhost:8000/api" ,
@@ -23,7 +25,12 @@ export function setApiUrl(url: string | null) {
2325 API_URL = url . includes ( '/api' ) ? url : `${ url } /api` ;
2426 }
2527}
26-
28+ export function setUserInfoGlobal ( userInfo : UserInfo | null ) {
29+ if ( userInfo ) {
30+ USER_ID = userInfo . user_id || null ;
31+ USER_INFO = userInfo ;
32+ }
33+ }
2734export function setEnvData ( configData : Record < string , any > ) {
2835 if ( configData ) {
2936 config . API_URL = configData . API_URL || "" ;
@@ -41,7 +48,7 @@ export function getConfigData() {
4148
4249 return { ...config } ;
4350}
44- export async function getUserInfo ( ) : Promise < UserInfo [ ] > {
51+ export async function getUserInfo ( ) : Promise < UserInfo > {
4552 try {
4653 const response = await fetch ( "/.auth/me" ) ;
4754 console . log ( "Fetching user info from: " , "/.auth/me" ) ;
@@ -50,12 +57,25 @@ export async function getUserInfo(): Promise<UserInfo[]> {
5057 console . log (
5158 "No identity provider found. Access to chat will be blocked."
5259 ) ;
53- return [ ] ;
60+ return { } as UserInfo ;
5461 }
5562 const payload = await response . json ( ) ;
56- return payload ;
63+ console . log ( "User info payload: " , payload [ 0 ] ) ;
64+ const userInfo : UserInfo = {
65+ access_token : payload [ 0 ] . access_token || "" ,
66+ expires_on : payload [ 0 ] . expires_on || "" ,
67+ id_token : payload [ 0 ] . id_token || "" ,
68+ provider_name : payload [ 0 ] . provider_name || "" ,
69+ user_claims : payload [ 0 ] . user_claims || [ ] ,
70+ user_email : payload [ 0 ] . user_id || "" ,
71+ user_first_last_name : payload [ 0 ] . user_claims ?. find ( ( claim : claim ) => claim . typ === 'name' ) ?. val || "" ,
72+ user_id : payload [ 0 ] . user_claims ?. find ( ( claim : claim ) => claim . typ === 'http://schemas.microsoft.com/identity/claims/objectidentifier' ) ?. val || '' ,
73+ } ;
74+ console . log ( "User info: " , userInfo ) ;
75+ return userInfo ;
5776 } catch ( e ) {
58- return [ ] ;
77+ console . error ( "Error fetching user info: " , e ) ;
78+ return { } as UserInfo ;
5979 }
6080}
6181export function getApiUrl ( ) {
@@ -73,9 +93,27 @@ export function getApiUrl() {
7393
7494 return API_URL ;
7595}
96+ export function getUserInfoGlobal ( ) {
97+ if ( ! USER_INFO ) {
98+ // Check if window.userInfo exists
99+ if ( window . userInfo ) {
100+ setUserInfoGlobal ( window . userInfo ) ;
101+ }
102+ }
103+
104+ if ( ! USER_INFO ) {
105+ console . info ( 'User info not yet configured' ) ;
106+ return null ;
107+ }
108+
109+ return USER_INFO ;
110+ }
76111
77112export function getUserId ( ) : string {
78- // USER_ID = window.userInfo && window.userInfo[0] && window.userInfo[0].user_id ? window.userInfo[0].user_id : null;
113+ // USER_ID = getUserInfoGlobal()?.user_id || null;
114+ if ( ! USER_ID ) {
115+ USER_ID = getUserInfoGlobal ( ) ?. user_id || null ;
116+ }
79117 const userId = USER_ID ?? "00000000-0000-0000-0000-000000000000" ;
80118 return userId ;
81119}
0 commit comments