1717 */
1818
1919import { AuthClient , CryptoUtils , MeAPIResponse , Store , UIAuthClient , getProfileInformation } from '@asgardeo/js-ui-core' ;
20- import { FC , PropsWithChildren , useCallback , useEffect , useMemo , useState } from 'react' ;
20+ import { FC , PropsWithChildren , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
2121import BrandingPreferenceProvider from './BrandingPreferenceProvider' ;
2222import I18nProvider from './I18nProvider' ;
2323import AsgardeoContext from '../contexts/asgardeo-context' ;
@@ -52,6 +52,23 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
5252 const [ isTextLoading , setIsTextLoading ] = useState < boolean > ( true ) ;
5353 const [ isAuthLoading , setIsAuthLoading ] = useState < boolean > ( false ) ;
5454
55+ const onSignInRef : React . MutableRefObject < Function > = useRef < Function > ( ) ;
56+ const onSignOutRef : React . MutableRefObject < Function > = useRef < Function > ( ) ;
57+
58+ const setOnSignIn : ( newOnSignIn : Function ) => void = useCallback (
59+ ( newOnSignIn : Function ) : void => {
60+ onSignInRef . current = newOnSignIn ;
61+ } ,
62+ [ ] , // Add any dependencies here...
63+ ) ;
64+
65+ const setOnSignOut : ( newOnSignOut : Function ) => void = useCallback (
66+ ( newOnSignOut : Function ) : void => {
67+ onSignOutRef . current = newOnSignOut ;
68+ } ,
69+ [ ] , // Add any dependencies here...
70+ ) ;
71+
5572 const storeInstance : Store = store || new SessionStore ( ) ;
5673
5774 const spaUtils : CryptoUtils = new SPACryptoUtils ( ) ;
@@ -64,14 +81,20 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
6481 const setAuthentication : ( ) => void = useCallback ( ( ) : void => {
6582 authClient . isAuthenticated ( ) . then ( ( isAuth : boolean ) => {
6683 setIsAuthenticated ( isAuth ) ;
67- } ) ;
6884
69- authClient . getAccessToken ( ) . then ( ( accessTokenFromClient : string ) => {
70- if ( accessTokenFromClient ) {
71- setAccessToken ( accessTokenFromClient ) ;
85+ if ( isAuth ) {
86+ authClient . getAccessToken ( ) . then ( ( accessTokenFromClient : string ) => {
87+ if ( accessTokenFromClient ) {
88+ setAccessToken ( accessTokenFromClient ) ;
7289
73- getProfileInformation ( ) . then ( ( response : MeAPIResponse ) => {
74- setUser ( response ) ;
90+ getProfileInformation ( ) . then ( ( response : MeAPIResponse ) => {
91+ setUser ( response ) ;
92+ } ) ;
93+
94+ if ( onSignInRef . current ) {
95+ onSignInRef . current ( ) ;
96+ }
97+ }
7598 } ) ;
7699 }
77100 } ) ;
@@ -105,13 +128,27 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
105128 isBrandingLoading,
106129 isGlobalLoading : isBrandingLoading || isTextLoading || isAuthLoading ,
107130 isTextLoading,
131+ onSignOutRef,
108132 setAuthentication,
109133 setIsAuthLoading,
110134 setIsBrandingLoading,
111135 setIsTextLoading,
136+ setOnSignIn,
137+ setOnSignOut,
112138 user,
113139 } ) ,
114- [ accessToken , config , isAuthLoading , isAuthenticated , isBrandingLoading , isTextLoading , setAuthentication , user ] ,
140+ [
141+ accessToken ,
142+ config ,
143+ isAuthLoading ,
144+ isAuthenticated ,
145+ isBrandingLoading ,
146+ isTextLoading ,
147+ setAuthentication ,
148+ setOnSignIn ,
149+ setOnSignOut ,
150+ user ,
151+ ] ,
115152 ) ;
116153
117154 return (
0 commit comments