File tree Expand file tree Collapse file tree 7 files changed +73
-0
lines changed
Expand file tree Collapse file tree 7 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ abstract class AsgardeoJavaScriptClient<T = Config> implements AsgardeoClient<T>
3737
3838 abstract initialize ( config : T , storage ?: Storage ) : Promise < boolean > ;
3939
40+ abstract reInitialize ( config : Partial < T > ) : Promise < boolean > ;
41+
4042 abstract getUser ( options ?: any ) : Promise < User > ;
4143
4244 abstract getAllOrganizations ( options ?: any , sessionId ?: string ) : Promise < AllOrganizationsApiResponse > ;
Original file line number Diff line number Diff line change @@ -97,6 +97,17 @@ export interface AsgardeoClient<T> {
9797 */
9898 initialize ( config : T , storage ?: Storage ) : Promise < boolean > ;
9999
100+ /**
101+ * Re-initializes the client with a new configuration.
102+ *
103+ * @remarks
104+ * This can be partial configuration to update only specific fields.
105+ *
106+ * @param config - New configuration to re-initialize the client with.
107+ * @returns Promise resolving to boolean indicating success.
108+ */
109+ reInitialize ( config : Partial < T > ) : Promise < boolean > ;
110+
100111 /**
101112 * Checks if the client is currently loading.
102113 * This can be used to determine if the client is in the process of initializing or fetching user data.
Original file line number Diff line number Diff line change @@ -145,6 +145,25 @@ class AsgardeoNextClient<T extends AsgardeoNextConfig = AsgardeoNextConfig> exte
145145 ) ;
146146 }
147147
148+ override async reInitialize ( config : Partial < T > ) : Promise < boolean > {
149+ let isInitialized : boolean = false ;
150+
151+ try {
152+ await this . asgardeo . reInitialize ( config as any ) ;
153+
154+ isInitialized = true ;
155+ } catch ( error ) {
156+ throw new AsgardeoRuntimeError (
157+ `Failed to re-initialize the client: ${ error instanceof Error ? error . message : String ( error ) } ` ,
158+ 'AsgardeoNextClient-reInitialize-RuntimeError-001' ,
159+ 'nextjs' ,
160+ 'An error occurred while re-initializing the client. Please check your configuration and network connection.' ,
161+ ) ;
162+ }
163+
164+ return isInitialized ;
165+ }
166+
148167 override async getUser ( userId ?: string ) : Promise < User > {
149168 await this . ensureInitialized ( ) ;
150169 const resolvedSessionId : string = userId || ( ( await getSessionId ( ) ) as string ) ;
Original file line number Diff line number Diff line change 1919import { TokenExchangeRequestConfig } from '@asgardeo/node' ;
2020import AsgardeoNextClient from '../AsgardeoNextClient' ;
2121import getSessionIdAction from './actions/getSessionId' ;
22+ import { AsgardeoNextConfig } from '../models/config' ;
2223
2324const asgardeo = async ( ) => {
2425 const getAccessToken = async ( sessionId : string ) => {
@@ -35,10 +36,16 @@ const asgardeo = async () => {
3536 return await client . exchangeToken ( config , sessionId ) ;
3637 } ;
3738
39+ const reInitialize = async ( config : Partial < AsgardeoNextConfig > ) => {
40+ const client : AsgardeoNextClient = AsgardeoNextClient . getInstance ( ) ;
41+ return await client . reInitialize ( config ) ;
42+ } ;
43+
3844 return {
3945 getAccessToken,
4046 getSessionId,
4147 exchangeToken,
48+ reInitialize,
4249 } ;
4350} ;
4451
Original file line number Diff line number Diff line change @@ -106,6 +106,27 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
106106 } ) ;
107107 }
108108
109+ override reInitialize ( config : Partial < AsgardeoReactConfig > ) : Promise < boolean > {
110+ return this . withLoading ( async ( ) => {
111+ let isInitialized : boolean ;
112+
113+ try {
114+ await this . asgardeo . reInitialize ( config ) ;
115+
116+ isInitialized = true ;
117+ } catch ( error ) {
118+ throw new AsgardeoRuntimeError (
119+ `Failed to check if the client is initialized: ${ error instanceof Error ? error . message : String ( error ) } ` ,
120+ 'AsgardeoReactClient-reInitialize-RuntimeError-001' ,
121+ 'react' ,
122+ 'An error occurred while checking the initialization status of the client.' ,
123+ ) ;
124+ }
125+
126+ return isInitialized ;
127+ } ) ;
128+ }
129+
109130 override async updateUserProfile ( payload : any , userId ?: string ) : Promise < User > {
110131 throw new Error ( 'Not implemented' ) ;
111132 }
Original file line number Diff line number Diff line change @@ -121,6 +121,17 @@ export type AsgardeoContextProps = {
121121 * @returns A promise that resolves to the token response or the raw response.
122122 */
123123 exchangeToken : ( config : TokenExchangeRequestConfig ) => Promise < TokenResponse | Response > ;
124+
125+ /**
126+ * Re-initializes the client with a new configuration.
127+ *
128+ * @remarks
129+ * This can be partial configuration to update only specific fields.
130+ *
131+ * @param config - New configuration to re-initialize the client with.
132+ * @returns Promise resolving to boolean indicating success.
133+ */
134+ reInitialize : ( config : Partial < AsgardeoReactConfig > ) => Promise < boolean > ;
124135} & Pick < AsgardeoReactConfig , 'storage' > ;
125136
126137/**
@@ -151,6 +162,7 @@ const AsgardeoContext: Context<AsgardeoContextProps | null> = createContext<null
151162 getAccessToken : null ,
152163 exchangeToken : null ,
153164 storage : 'sessionStorage' ,
165+ reInitialize : null ,
154166} ) ;
155167
156168AsgardeoContext . displayName = 'AsgardeoContext' ;
Original file line number Diff line number Diff line change @@ -430,6 +430,7 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
430430 request : asgardeo . request . bind ( asgardeo ) ,
431431 requestAll : asgardeo . requestAll . bind ( asgardeo ) ,
432432 } ,
433+ reInitialize : asgardeo . reInitialize . bind ( asgardeo ) ,
433434 signInOptions,
434435 getDecodedIdToken : asgardeo . getDecodedIdToken . bind ( asgardeo ) ,
435436 exchangeToken : asgardeo . exchangeToken . bind ( asgardeo ) ,
You can’t perform that action at this time.
0 commit comments