@@ -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,113 @@ 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+ }
204211
205- return this . _instances . get ( id ) ;
206- }
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+ * @preserve
228+ */
229+ public static hasInstance ( id : number = 0 ) : boolean {
230+ return this . _instances . has ( id ) ;
231+ }
207232
208- this . _instances . set ( 0 , new AsgardeoSPAClient ( 0 ) ) ;
233+ /**
234+ * This method removes and cleans up a specific instance.
235+ * Useful when an instance is no longer needed.
236+ *
237+ * @param {number } id - Optional unique identifier for the instance to destroy.
238+ * @return {boolean } - Returns true if the instance was found and removed.
239+ *
240+ * @example
241+ * ```
242+ * // Remove a specific instance
243+ * AsgardeoSPAClient.destroyInstance(1);
244+ *
245+ * // Remove the default instance
246+ * AsgardeoSPAClient.destroyInstance();
247+ * ```
248+ *
249+ * @memberof AsgardeoSPAClient
250+ *
251+ * @preserve
252+ */
253+ public static destroyInstance ( id : number = 0 ) : boolean {
254+ return this . _instances . delete ( id ) ;
255+ }
209256
210- return this . _instances . get ( 0 ) ;
257+ /**
258+ * This method returns all active instance IDs.
259+ * Useful for debugging or managing multiple instances.
260+ *
261+ * @return {number[] } - Returns an array of all active instance IDs.
262+ *
263+ * @example
264+ * ```
265+ * const activeInstances = AsgardeoSPAClient.getInstanceKeys();
266+ * console.log('Active instances:', activeInstances);
267+ * ```
268+ *
269+ * @memberof AsgardeoSPAClient
270+ *
271+ * @preserve
272+ */
273+ public static getInstanceKeys ( ) : number [ ] {
274+ return Array . from ( this . _instances . keys ( ) ) ;
275+ }
276+
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+ * @preserve
289+ */
290+ public static destroyAllInstances ( ) : void {
291+ this . _instances . clear ( ) ;
292+ }
293+
294+ /**
295+ * This method returns the instance ID for this client instance.
296+ *
297+ * @return {number } - The instance ID.
298+ *
299+ * @example
300+ * ```
301+ * const auth = AsgardeoSPAClient.getInstance(1);
302+ * console.log(auth.getInstanceId()); // 1
303+ * ```
304+ *
305+ * @memberof AsgardeoSPAClient
306+ *
307+ * @preserve
308+ */
309+ public getInstanceId ( ) : number {
310+ return this . _instanceID ;
211311 }
212312
213313 /**
0 commit comments