@@ -67,6 +67,8 @@ const DefaultConfig: Partial<AuthClientConfig<Config>> = {
6767
6868/**
6969 * This class provides the necessary methods to implement authentication in a Single Page Application.
70+ * Implements a Multiton pattern to support multi-tenancy scenarios where multiple authentication
71+ * contexts need to coexist in the same application.
7072 *
7173 * @export
7274 * @class AsgardeoSPAClient
@@ -175,15 +177,22 @@ export class AsgardeoSPAClient {
175177 }
176178
177179 /**
178- * This method returns the instance of the singleton class.
180+ * This method returns the instance of the client for the specified ID.
181+ * Implements a Multiton pattern to support multiple authentication contexts.
179182 * If an ID is provided, it will return the instance with the given ID.
180- * If no ID is provided, it will return the default instance value 0 .
183+ * If no ID is provided, it will return the default instance (ID: 0) .
181184 *
182- * @return {AsgardeoSPAClient } - Returns the instance of the singleton class.
185+ * @param {number } id - Optional unique identifier for the instance.
186+ * @return {AsgardeoSPAClient } - Returns the instance associated with the ID.
183187 *
184188 * @example
185189 * ```
190+ * // Single tenant application (default instance)
186191 * const auth = AsgardeoSPAClient.getInstance();
192+ *
193+ * // Multi-instance application
194+ * const instance1 = AsgardeoSPAClient.getInstance(1);
195+ * const instance2 = AsgardeoSPAClient.getInstance(2);
187196 * ```
188197 *
189198 * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getinstance
@@ -192,22 +201,115 @@ export class AsgardeoSPAClient {
192201 *
193202 * @preserve
194203 */
195- public static getInstance ( id ?: number ) : AsgardeoSPAClient | undefined {
196- if ( id && this . _instances ?. get ( id ) ) {
197- return this . _instances . get ( id ) ;
198- } else if ( ! id && this . _instances ?. get ( 0 ) ) {
199- return this . _instances . get ( 0 ) ;
204+ public static getInstance ( id : number = 0 ) : AsgardeoSPAClient {
205+ if ( ! this . _instances . has ( id ) ) {
206+ this . _instances . set ( id , new AsgardeoSPAClient ( id ) ) ;
200207 }
201208
202- if ( id ) {
203- this . _instances . set ( id , new AsgardeoSPAClient ( id ) ) ;
209+ return this . _instances . get ( id ) ! ;
210+ }
211+
212+ /**
213+ * This method checks if an instance exists for the given ID.
214+ *
215+ * @param {number } id - Optional unique identifier for the instance.
216+ * @return {boolean } - Returns true if an instance exists for the ID.
217+ *
218+ * @example
219+ * ```
220+ * if (AsgardeoSPAClient.hasInstance(1)) {
221+ * const auth = AsgardeoSPAClient.getInstance(1);
222+ * }
223+ * ```
224+ *
225+ * @memberof AsgardeoSPAClient
226+ */
227+ public static hasInstance ( id : number = 0 ) : boolean {
228+ return this . _instances . has ( id ) ;
229+ }
204230
205- return this . _instances . get ( id ) ;
231+ /**
232+ * This method removes and cleans up a specific instance.
233+ * Useful when an instance is no longer needed.
234+ *
235+ * @param {number } id - Optional unique identifier for the instance to destroy.
236+ * @return {boolean } - Returns true if the instance was found and removed.
237+ *
238+ * @example
239+ * ```
240+ * // Remove a specific instance
241+ * AsgardeoSPAClient.destroyInstance(1);
242+ *
243+ * // Remove the default instance
244+ * AsgardeoSPAClient.destroyInstance();
245+ * ```
246+ *
247+ * @memberof AsgardeoSPAClient
248+ */
249+ public static destroyInstance ( id : number = 0 ) : boolean {
250+ const instance = this . _instances . get ( id ) ;
251+ if ( instance ) {
252+ // Clean up the instance's session data before removing it
253+ instance . clearSession ( ) ;
254+ return this . _instances . delete ( id ) ;
206255 }
256+ return false ;
257+ }
207258
208- this . _instances . set ( 0 , new AsgardeoSPAClient ( 0 ) ) ;
259+ /**
260+ * This method returns all active instance IDs.
261+ * Useful for debugging or managing multiple instances.
262+ *
263+ * @return {number[] } - Returns an array of all active instance IDs.
264+ *
265+ * @example
266+ * ```
267+ * const activeInstances = AsgardeoSPAClient.getInstanceKeys();
268+ * console.log('Active instances:', activeInstances);
269+ * ```
270+ *
271+ * @memberof AsgardeoSPAClient
272+ */
273+ public static getInstanceKeys ( ) : number [ ] {
274+ return Array . from ( this . _instances . keys ( ) ) ;
275+ }
209276
210- return this . _instances . get ( 0 ) ;
277+ /**
278+ * This method removes all instances.
279+ * Useful for cleanup in testing scenarios or application teardown.
280+ *
281+ * @example
282+ * ```
283+ * AsgardeoSPAClient.destroyAllInstances();
284+ * ```
285+ *
286+ * @memberof AsgardeoSPAClient
287+ */
288+ public static destroyAllInstances ( ) : void {
289+ // Clean up each instance's session data before clearing
290+ this . _instances . forEach ( ( instance ) => {
291+ instance . clearSession ( ) ;
292+ } ) ;
293+ this . _instances . clear ( ) ;
294+ }
295+
296+ /**
297+ * This method returns the instance ID for this client instance.
298+ *
299+ * @return {number } - The instance ID.
300+ *
301+ * @example
302+ * ```
303+ * const auth = AsgardeoSPAClient.getInstance(1);
304+ * console.log(auth.getInstanceId()); // 1
305+ * ```
306+ *
307+ * @memberof AsgardeoSPAClient
308+ *
309+ * @preserve
310+ */
311+ public getInstanceId ( ) : number {
312+ return this . _instanceID ;
211313 }
212314
213315 /**
0 commit comments