diff --git a/android/src/main/java/com/frontegg/ionic/FronteggNativePlugin.java b/android/src/main/java/com/frontegg/ionic/FronteggNativePlugin.java index cfaa8fe..45df3ac 100644 --- a/android/src/main/java/com/frontegg/ionic/FronteggNativePlugin.java +++ b/android/src/main/java/com/frontegg/ionic/FronteggNativePlugin.java @@ -42,8 +42,11 @@ public void load() { List regions = new ArrayList<>(); JSONArray array; try { - array = this.getConfig().getConfigJSON().getJSONArray("regions"); + array = this.getConfig().getConfigJSON().optJSONArray("regions"); + if (array == null) { + array = new JSONArray(); + } for (int i = 0; i < array.length(); i++) { JSONObject regionJson = (JSONObject) array.get(i); regions.add(new RegionConfig( @@ -61,9 +64,12 @@ public void load() { PluginConfig config = this.getConfig(); String baseUrl = config.getString("baseUrl"); String clientId = config.getString("clientId"); - String regionKey = config.getString("regionKey"); - if (baseUrl == null || clientId == null || regionKey == null) { - throw new RuntimeException("Missing required config parameters: baseUrl, clientId, regionKey"); + + if (baseUrl == null || clientId == null) { + throw new RuntimeException("Missing required config parameters: baseUrl, clientId"); + } + if(baseUrl.startsWith("https://")) { + baseUrl = baseUrl.substring(baseUrl.indexOf("://") + 3); } FronteggApp.Companion.init( baseUrl, diff --git a/example/capacitor.config.ts b/example/capacitor.config.ts index d0d6e1c..dc93aa8 100644 --- a/example/capacitor.config.ts +++ b/example/capacitor.config.ts @@ -9,15 +9,18 @@ const config: CapacitorConfig = { }, plugins:{ FronteggNative:{ - regions:[{ - key: 'eu', - baseUrl: 'https://auth.davidantoon.me', - clientId: 'b6adfe4c-d695-4c04-b95f-3ec9fd0c6cca' - }, { - key: 'us', - baseUrl: 'https://davidprod.frontegg.com', - clientId: 'd7d07347-2c57-4450-8418-0ec7ee6e096b' - }] + baseUrl:"https://auth.davidantoon.me", + clientId:"b6adfe4c-d695-4c04-b95f-3ec9fd0c6cca", + + // regions:[{ + // key: 'eu', + // baseUrl: 'https://auth.davidantoon.me', + // clientId: 'b6adfe4c-d695-4c04-b95f-3ec9fd0c6cca' + // }, { + // key: 'us', + // baseUrl: 'https://davidprod.frontegg.com', + // clientId: 'd7d07347-2c57-4450-8418-0ec7ee6e096b' + // }] } } }; diff --git a/example/package-lock.json b/example/package-lock.json index cab962a..cc69970 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -3096,9 +3096,9 @@ } }, "node_modules/@frontegg/ionic-capacitor": { - "version": "1.0.0", + "version": "2.0.1", "resolved": "file:../frontegg-ionic-capacitor.tgz", - "integrity": "sha512-7eOjLESUhXQTlL7nVjnVBKkB2cuUC8RJ7KEy7hAlHsQVsLj5t4SxmNsDeNmNTc1H/+qHzjx1vPZzNoenjSRcYg==", + "integrity": "sha512-BJztOq5CF97ZGOYXLF8tNmcwBfDHb9cldOOZekq2Y9OrmIuRx0ryk6o8+CtNVrPhOREGq/WUpLYs96dc3aUx+Q==", "license": "MIT", "dependencies": { "@frontegg/rest-api": "^3.1.5" @@ -17841,7 +17841,7 @@ }, "@frontegg/ionic-capacitor": { "version": "file:../frontegg-ionic-capacitor.tgz", - "integrity": "sha512-7eOjLESUhXQTlL7nVjnVBKkB2cuUC8RJ7KEy7hAlHsQVsLj5t4SxmNsDeNmNTc1H/+qHzjx1vPZzNoenjSRcYg==", + "integrity": "sha512-BJztOq5CF97ZGOYXLF8tNmcwBfDHb9cldOOZekq2Y9OrmIuRx0ryk6o8+CtNVrPhOREGq/WUpLYs96dc3aUx+Q==", "requires": { "@frontegg/rest-api": "^3.1.5" } diff --git a/example/src/app/select-region/select-region.component.ts b/example/src/app/select-region/select-region.component.ts index 67429cd..d14dec9 100644 --- a/example/src/app/select-region/select-region.component.ts +++ b/example/src/app/select-region/select-region.component.ts @@ -14,6 +14,7 @@ export class SelectRegionComponent implements OnInit { regions: { key: string, baseUrl: string, clientId: string }[] = [] async ngOnInit() { + console.log('onInit() select region ') const { selectedRegion } = await this.fronteggService.getNativeState() diff --git a/ios/Plugin/FronteggNativePlugin.swift b/ios/Plugin/FronteggNativePlugin.swift index 7f8b3c1..56f5ad7 100644 --- a/ios/Plugin/FronteggNativePlugin.swift +++ b/ios/Plugin/FronteggNativePlugin.swift @@ -11,29 +11,29 @@ import Capacitor public class FronteggNativePlugin: CAPPlugin { public let fronteggApp = FronteggApp.shared var cancellables = Set() - + private var workItem: DispatchWorkItem? private let delay: TimeInterval = 0.05 // 200ms delay - + func debounce(_ action: @escaping () -> Void) { workItem?.cancel() let newWorkItem = DispatchWorkItem(block: action) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: newWorkItem) workItem = newWorkItem } - + override public func load() { - + let config = self.getConfig() - + let handleLoginWithSocialLogin = config.getBoolean("handleLoginWithSocialLogin", true) let handleLoginWithSSO = config.getBoolean("handleLoginWithSSO", false) - - if let array = config.getArray("regions"), + + if let array = config.getArray("regions", []), array.count > 0 { print("region initialization") var regions:[RegionConfig] = [] - + array.forEach {item in if let dict = item as? [String:String] { regions.append(RegionConfig( @@ -43,7 +43,7 @@ public class FronteggNativePlugin: CAPPlugin { )) } } - + if(regions.isEmpty){ print("Frontegg Error: Missing regions configurations") exit(1) @@ -64,9 +64,9 @@ public class FronteggNativePlugin: CAPPlugin { exit(1) } } - - - + + + let auth = fronteggApp.auth var anyChange: AnyPublisher { return Publishers.Merge8 ( @@ -81,26 +81,26 @@ public class FronteggNativePlugin: CAPPlugin { ) .eraseToAnyPublisher() } - + anyChange.sink(receiveValue: { () in self.debounce() { self.sendEvent() } }).store(in: &cancellables) - + self.sendEvent() } - - - + + + func sendEvent() { let auth = fronteggApp.auth - + var jsonUser: [String: Any]? = nil if let userData = try? JSONEncoder().encode(auth.user) { jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any] } - + let body: [String: Any?] = [ "accessToken": auth.accessToken, "refreshToken": auth.refreshToken, @@ -111,13 +111,13 @@ public class FronteggNativePlugin: CAPPlugin { "showLoader": auth.showLoader, "selectedRegion": regionToJson(auth.selectedRegion) ] - + self.notifyListeners("onFronteggAuthEvent", data: body as [String : Any]) } - - + + func regionToJson(_ region: RegionConfig?) -> [String:String]? { - + if let reg = region { return [ "baseUrl": reg.baseUrl, @@ -129,17 +129,17 @@ public class FronteggNativePlugin: CAPPlugin { } } func regionsToJson(_ regions: [RegionConfig]) -> [[String:String]] { - + var regionData: [[String:String]] = [] regions.forEach { reg in if let region = regionToJson(reg) { regionData.append(region) } } - + return regionData } - + @objc func getConstants(_ call: CAPPluginCall) { call.resolve([ "baseUrl": fronteggApp.baseUrl, @@ -149,43 +149,43 @@ public class FronteggNativePlugin: CAPPlugin { "regionData": regionsToJson(fronteggApp.auth.regionData) ]) } - + @objc func login(_ call: CAPPluginCall) { DispatchQueue.main.sync { fronteggApp.auth.login() } call.resolve() } - + @objc func logout(_ call: CAPPluginCall) { DispatchQueue.main.sync { fronteggApp.auth.logout() } call.resolve() } - + @objc func switchTenant(_ call: CAPPluginCall) { guard let tenantId = call.options["tenantId"] as? String else { call.reject("No tenantId provided") return } - + fronteggApp.auth.switchTenant(tenantId: tenantId) { _ in call.resolve() } } - + @objc func initWithRegion(_ call: CAPPluginCall) { guard let regionKey = call.options["regionKey"] as? String else { call.reject("No regionKey provided") return } - + fronteggApp.initWithRegion(regionKey: regionKey) } - + @objc func refreshToken(_ call: CAPPluginCall) { - + DispatchQueue.global(qos: .background).async { Task { await self.fronteggApp.auth.refreshTokenIfNeeded() @@ -193,14 +193,14 @@ public class FronteggNativePlugin: CAPPlugin { } } } - + @objc func getAuthState(_ call: CAPPluginCall) { let auth = fronteggApp.auth var jsonUser: [String: Any]? = nil if let userData = try? JSONEncoder().encode(auth.user) { jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any] } - + let body: [String: Any?] = [ "accessToken": auth.accessToken, "refreshToken": auth.refreshToken, @@ -213,5 +213,5 @@ public class FronteggNativePlugin: CAPPlugin { ] call.resolve(body as [String: Any] ) } - + } diff --git a/package.json b/package.json index a2a108e..6db042b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@frontegg/ionic-capacitor", - "version": "2.0.0", + "version": "2.0.1", "description": "Frontegg Ionic Capacitor SDK", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", diff --git a/src/definitions.ts b/src/definitions.ts index 0ff75a8..09b8434 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -78,7 +78,7 @@ type FronteggNativeRegionOptions = { * @since 1.0.0 * @example [{key: "us", baseUrl: "https://us-api.frontegg.com", clientId: "us-client-id"}] */ - regions?: RegionConfig[]; + regions: RegionConfig[]; } type FronteggNativeOptions = (FronteggNativeStandardOptions | FronteggNativeRegionOptions) & { handleLoginWithSocialLogin?: boolean;