@@ -17,17 +17,11 @@ import { ChatHistory } from './components/ChatHistory';
1717import type { ChatMessage , CreativeBrief , Product , GeneratedContent } from './types' ;
1818import ContosoLogo from './styles/images/contoso.svg' ;
1919
20- interface UserInfo {
21- user_principal_id : string ;
22- user_name : string ;
23- auth_provider : string ;
24- is_authenticated : boolean ;
25- }
26-
2720
2821function App ( ) {
2922 const [ conversationId , setConversationId ] = useState < string > ( ( ) => uuidv4 ( ) ) ;
3023 const [ userId , setUserId ] = useState < string > ( '' ) ;
24+ const [ userName , setUserName ] = useState < string > ( '' ) ;
3125 const [ messages , setMessages ] = useState < ChatMessage [ ] > ( [ ] ) ;
3226 const [ isLoading , setIsLoading ] = useState ( false ) ;
3327 const [ generationStatus , setGenerationStatus ] = useState < string > ( '' ) ;
@@ -72,18 +66,32 @@ function App() {
7266 fetchConfig ( ) ;
7367 } , [ ] ) ;
7468
75- // Fetch current user on mount
69+ // Fetch current user on mount - using /.auth/me (Azure App Service built-in auth endpoint)
7670 useEffect ( ( ) => {
7771 const fetchUser = async ( ) => {
7872 try {
79- const response = await fetch ( '/api/user ' ) ;
73+ const response = await fetch ( '/.auth/me ' ) ;
8074 if ( response . ok ) {
81- const user : UserInfo = await response . json ( ) ;
82- setUserId ( user . user_principal_id || 'anonymous' ) ;
75+ const payload = await response . json ( ) ;
76+
77+ // Extract user ID from objectidentifier claim
78+ const userClaims = payload [ 0 ] ?. user_claims || [ ] ;
79+ const objectIdClaim = userClaims . find (
80+ ( claim : { typ : string ; val : string } ) =>
81+ claim . typ === 'http://schemas.microsoft.com/identity/claims/objectidentifier'
82+ ) ;
83+ setUserId ( objectIdClaim ?. val || 'anonymous' ) ;
84+
85+ // Extract display name from 'name' claim
86+ const nameClaim = userClaims . find (
87+ ( claim : { typ : string ; val : string } ) => claim . typ === 'name'
88+ ) ;
89+ setUserName ( nameClaim ?. val || '' ) ;
8390 }
8491 } catch ( err ) {
8592 console . error ( 'Error fetching user:' , err ) ;
8693 setUserId ( 'anonymous' ) ;
94+ setUserName ( '' ) ;
8795 }
8896 } ;
8997 fetchUser ( ) ;
@@ -725,17 +733,6 @@ function App() {
725733 }
726734 } , [ confirmedBrief , selectedProducts , conversationId ] ) ;
727735
728- // Get user initials for avatar
729- const getUserInitials = ( ) => {
730- if ( ! userId ) return 'U' ;
731- // If we have a name, use first letter of first and last name
732- const parts = userId . split ( '@' ) [ 0 ] . split ( '.' ) ;
733- if ( parts . length >= 2 ) {
734- return ( parts [ 0 ] [ 0 ] + parts [ parts . length - 1 ] [ 0 ] ) . toUpperCase ( ) ;
735- }
736- return userId [ 0 ] . toUpperCase ( ) ;
737- } ;
738-
739736 return (
740737 < div className = "app-container" >
741738 { /* Header */ }
@@ -764,8 +761,7 @@ function App() {
764761 />
765762 </ Tooltip >
766763 < Avatar
767- name = { userId || 'User' }
768- initials = { getUserInitials ( ) }
764+ name = { userName || undefined }
769765 color = "colorful"
770766 size = { 36 }
771767 />
0 commit comments