@@ -19,6 +19,8 @@ import { CACHE_KEY_PREFIX } from '../AuthGuard/index.js';
19
19
import FullScreenLoader from './LoadingScreen.js' ;
20
20
21
21
import { getRunEnvironmentConfig , ValidServices } from '@ui/config.js' ;
22
+ import { transformCommaSeperatedName } from '@common/utils.js' ;
23
+ import { useApi } from '@ui/util/api.js' ;
22
24
23
25
interface AuthContextDataWrapper {
24
26
isLoggedIn : boolean ;
@@ -28,6 +30,7 @@ interface AuthContextDataWrapper {
28
30
getToken : CallableFunction ;
29
31
logoutCallback : CallableFunction ;
30
32
getApiToken : CallableFunction ;
33
+ setLoginStatus : CallableFunction ;
31
34
}
32
35
33
36
export type AuthContextData = {
@@ -53,7 +56,6 @@ export const clearAuthCache = () => {
53
56
54
57
export const AuthProvider : React . FC < AuthProviderProps > = ( { children } ) => {
55
58
const { instance, inProgress, accounts } = useMsal ( ) ;
56
-
57
59
const [ userData , setUserData ] = useState < AuthContextData | null > ( null ) ;
58
60
const [ isLoggedIn , setIsLoggedIn ] = useState < boolean > ( false ) ;
59
61
@@ -67,11 +69,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
67
69
if ( response ) {
68
70
handleMsalResponse ( response ) ;
69
71
} else if ( accounts . length > 0 ) {
70
- // User is already logged in, set the state
71
- const [ lastName , firstName ] = accounts [ 0 ] . name ?. split ( ',' ) || [ ] ;
72
72
setUserData ( {
73
73
email : accounts [ 0 ] . username ,
74
- name : ` ${ firstName } ${ lastName } ` ,
74
+ name : transformCommaSeperatedName ( accounts [ 0 ] . name || '' ) ,
75
75
} ) ;
76
76
setIsLoggedIn ( true ) ;
77
77
}
@@ -94,29 +94,26 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
94
94
} )
95
95
. then ( ( silentResponse ) => {
96
96
if ( silentResponse ?. account ?. name ) {
97
- const [ lastName , firstName ] = silentResponse . account . name . split ( ',' ) ;
98
97
setUserData ( {
99
- email : silentResponse . account . username ,
100
- name : ` ${ firstName } ${ lastName } ` ,
98
+ email : accounts [ 0 ] . username ,
99
+ name : transformCommaSeperatedName ( accounts [ 0 ] . name || '' ) ,
101
100
} ) ;
102
101
setIsLoggedIn ( true ) ;
103
102
}
104
103
} )
105
104
. catch ( console . error ) ;
106
105
return ;
107
106
}
108
-
109
- // Use response.account instead of accounts[0]
110
- const [ lastName , firstName ] = response . account . name ?. split ( ',' ) || [ ] ;
111
107
setUserData ( {
112
- email : response . account . username ,
113
- name : ` ${ firstName } ${ lastName } ` ,
108
+ email : accounts [ 0 ] . username ,
109
+ name : transformCommaSeperatedName ( accounts [ 0 ] . name || '' ) ,
114
110
} ) ;
115
111
setIsLoggedIn ( true ) ;
116
112
}
117
113
} ,
118
114
[ accounts , instance ]
119
115
) ;
116
+
120
117
const getApiToken = useCallback (
121
118
async ( service : ValidServices ) => {
122
119
if ( ! userData ) {
@@ -194,6 +191,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
194
191
} ,
195
192
[ instance ]
196
193
) ;
194
+ const setLoginStatus = useCallback ( ( val : boolean ) => {
195
+ setIsLoggedIn ( val ) ;
196
+ } , [ ] ) ;
197
197
198
198
const logout = useCallback ( async ( ) => {
199
199
try {
@@ -209,7 +209,16 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
209
209
} ;
210
210
return (
211
211
< 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
+ } }
213
222
>
214
223
{ inProgress !== InteractionStatus . None ? (
215
224
< MantineProvider >
0 commit comments