Skip to content

Commit

Permalink
Merge pull request #8 from frontegg/FR-14813-multi-region-bug-fixes
Browse files Browse the repository at this point in the history
FR-14813 - multi region bug fixes
  • Loading branch information
frontegg-david authored Jan 8, 2024
2 parents 67b8a40 + 3b31535 commit 9703b51
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 55 deletions.
14 changes: 10 additions & 4 deletions android/src/main/java/com/frontegg/ionic/FronteggNativePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public void load() {
List<RegionConfig> 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(
Expand All @@ -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,
Expand Down
21 changes: 12 additions & 9 deletions example/capacitor.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
// }]
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/src/app/select-region/select-region.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
74 changes: 37 additions & 37 deletions ios/Plugin/FronteggNativePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import Capacitor
public class FronteggNativePlugin: CAPPlugin {
public let fronteggApp = FronteggApp.shared
var cancellables = Set<AnyCancellable>()

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(
Expand All @@ -43,7 +43,7 @@ public class FronteggNativePlugin: CAPPlugin {
))
}
}

if(regions.isEmpty){
print("Frontegg Error: Missing regions configurations")
exit(1)
Expand All @@ -64,9 +64,9 @@ public class FronteggNativePlugin: CAPPlugin {
exit(1)
}
}



let auth = fronteggApp.auth
var anyChange: AnyPublisher<Void, Never> {
return Publishers.Merge8 (
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -149,58 +149,58 @@ 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()
call.resolve()
}
}
}

@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,
Expand All @@ -213,5 +213,5 @@ public class FronteggNativePlugin: CAPPlugin {
]
call.resolve(body as [String: Any] )
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9703b51

Please sign in to comment.