@@ -19,6 +19,8 @@ import { CACHE_KEY_PREFIX } from '../AuthGuard/index.js';
1919import FullScreenLoader from './LoadingScreen.js' ;
2020
2121import { getRunEnvironmentConfig , ValidServices } from '@ui/config.js' ;
22+ import { transformCommaSeperatedName } from '@common/utils.js' ;
23+ import { useApi } from '@ui/util/api.js' ;
2224
2325interface AuthContextDataWrapper {
2426 isLoggedIn : boolean ;
@@ -28,6 +30,7 @@ interface AuthContextDataWrapper {
2830 getToken : CallableFunction ;
2931 logoutCallback : CallableFunction ;
3032 getApiToken : CallableFunction ;
33+ setLoginStatus : CallableFunction ;
3134}
3235
3336export type AuthContextData = {
@@ -53,7 +56,6 @@ export const clearAuthCache = () => {
5356
5457export const AuthProvider : React . FC < AuthProviderProps > = ( { children } ) => {
5558 const { instance, inProgress, accounts } = useMsal ( ) ;
56-
5759 const [ userData , setUserData ] = useState < AuthContextData | null > ( null ) ;
5860 const [ isLoggedIn , setIsLoggedIn ] = useState < boolean > ( false ) ;
5961
@@ -67,11 +69,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
6769 if ( response ) {
6870 handleMsalResponse ( response ) ;
6971 } else if ( accounts . length > 0 ) {
70- // User is already logged in, set the state
71- const [ lastName , firstName ] = accounts [ 0 ] . name ?. split ( ',' ) || [ ] ;
7272 setUserData ( {
7373 email : accounts [ 0 ] . username ,
74- name : ` ${ firstName } ${ lastName } ` ,
74+ name : transformCommaSeperatedName ( accounts [ 0 ] . name || '' ) ,
7575 } ) ;
7676 setIsLoggedIn ( true ) ;
7777 }
@@ -94,29 +94,26 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
9494 } )
9595 . then ( ( silentResponse ) => {
9696 if ( silentResponse ?. account ?. name ) {
97- const [ lastName , firstName ] = silentResponse . account . name . split ( ',' ) ;
9897 setUserData ( {
99- email : silentResponse . account . username ,
100- name : ` ${ firstName } ${ lastName } ` ,
98+ email : accounts [ 0 ] . username ,
99+ name : transformCommaSeperatedName ( accounts [ 0 ] . name || '' ) ,
101100 } ) ;
102101 setIsLoggedIn ( true ) ;
103102 }
104103 } )
105104 . catch ( console . error ) ;
106105 return ;
107106 }
108-
109- // Use response.account instead of accounts[0]
110- const [ lastName , firstName ] = response . account . name ?. split ( ',' ) || [ ] ;
111107 setUserData ( {
112- email : response . account . username ,
113- name : ` ${ firstName } ${ lastName } ` ,
108+ email : accounts [ 0 ] . username ,
109+ name : transformCommaSeperatedName ( accounts [ 0 ] . name || '' ) ,
114110 } ) ;
115111 setIsLoggedIn ( true ) ;
116112 }
117113 } ,
118114 [ accounts , instance ]
119115 ) ;
116+
120117 const getApiToken = useCallback (
121118 async ( service : ValidServices ) => {
122119 if ( ! userData ) {
@@ -194,6 +191,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
194191 } ,
195192 [ instance ]
196193 ) ;
194+ const setLoginStatus = useCallback ( ( val : boolean ) => {
195+ setIsLoggedIn ( val ) ;
196+ } , [ ] ) ;
197197
198198 const logout = useCallback ( async ( ) => {
199199 try {
@@ -209,7 +209,16 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
209209 } ;
210210 return (
211211 < AuthContext . Provider
212- value = { { isLoggedIn, userData, loginMsal, logout, getToken, logoutCallback, getApiToken } }
212+ value = { {
213+ isLoggedIn,
214+ userData,
215+ setLoginStatus,
216+ loginMsal,
217+ logout,
218+ getToken,
219+ logoutCallback,
220+ getApiToken,
221+ } }
213222 >
214223 { inProgress !== InteractionStatus . None ? (
215224 < MantineProvider >
0 commit comments