@frontegg/ionic-capacitor 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,19 +11,62 @@ import Capacitor
11
11
  public class FronteggNativePlugin: CAPPlugin {
12
12
  public let fronteggApp = FronteggApp.shared
13
13
  var cancellables = Set<AnyCancellable>()
14
-
14
+
15
15
  private var workItem: DispatchWorkItem?
16
16
  private let delay: TimeInterval = 0.05 // 200ms delay
17
-
17
+
18
18
  func debounce(_ action: @escaping () -> Void) {
19
19
  workItem?.cancel()
20
20
  let newWorkItem = DispatchWorkItem(block: action)
21
21
  DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: newWorkItem)
22
22
  workItem = newWorkItem
23
23
  }
24
-
24
+
25
25
  override public func load() {
26
-
26
+
27
+ let config = self.getConfig()
28
+
29
+ let handleLoginWithSocialLogin = config.getBoolean("handleLoginWithSocialLogin", true)
30
+ let handleLoginWithSSO = config.getBoolean("handleLoginWithSSO", false)
31
+
32
+ if let array = config.getArray("regions"),
33
+ array.count > 0 {
34
+ print("region initialization")
35
+ var regions:[RegionConfig] = []
36
+
37
+ array.forEach {item in
38
+ if let dict = item as? [String:String] {
39
+ regions.append(RegionConfig(
40
+ key: dict["key"]!,
41
+ baseUrl: dict["baseUrl"]!,
42
+ clientId: dict["clientId"]!
43
+ ))
44
+ }
45
+ }
46
+
47
+ if(regions.isEmpty){
48
+ print("Frontegg Error: Missing regions configurations")
49
+ exit(1)
50
+ }
51
+ fronteggApp.manualInitRegions(regions: regions,
52
+ handleLoginWithSocialLogin: handleLoginWithSocialLogin,
53
+ handleLoginWithSSO: handleLoginWithSSO)
54
+ } else {
55
+ print("standard initialization")
56
+ if let baseUrl = config.getString("baseUrl"),
57
+ let clientId = config.getString("clientId") {
58
+ fronteggApp.manualInit(baseUrl: baseUrl,
59
+ cliendId: clientId,
60
+ handleLoginWithSocialLogin: handleLoginWithSocialLogin,
61
+ handleLoginWithSSO: handleLoginWithSSO)
62
+ }else {
63
+ print("Frontegg Error: Missing baseUrl or clientId in project configurations")
64
+ exit(1)
65
+ }
66
+ }
67
+
68
+
69
+
27
70
  let auth = fronteggApp.auth
28
71
  var anyChange: AnyPublisher<Void, Never> {
29
72
  return Publishers.Merge8 (
@@ -34,28 +77,30 @@ public class FronteggNativePlugin: CAPPlugin {
34
77
  auth.$isLoading.map {_ in },
35
78
  auth.$initializing.map {_ in },
36
79
  auth.$showLoader.map {_ in },
37
- auth.$appLink.map {_ in }
80
+ auth.$selectedRegion.map{_ in}
38
81
  )
39
82
  .eraseToAnyPublisher()
40
83
  }
41
-
84
+
42
85
  anyChange.sink(receiveValue: { () in
43
86
  self.debounce() {
44
87
  self.sendEvent()
45
88
  }
46
89
  }).store(in: &cancellables)
47
-
90
+
48
91
  self.sendEvent()
49
92
  }
50
-
93
+
94
+
95
+
51
96
  func sendEvent() {
52
97
  let auth = fronteggApp.auth
53
-
98
+
54
99
  var jsonUser: [String: Any]? = nil
55
100
  if let userData = try? JSONEncoder().encode(auth.user) {
56
101
  jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any]
57
102
  }
58
-
103
+
59
104
  let body: [String: Any?] = [
60
105
  "accessToken": auth.accessToken,
61
106
  "refreshToken": auth.refreshToken,
@@ -64,47 +109,83 @@ public class FronteggNativePlugin: CAPPlugin {
64
109
  "isLoading": auth.isLoading,
65
110
  "initializing": auth.initializing,
66
111
  "showLoader": auth.showLoader,
67
- "appLink": auth.appLink
112
+ "selectedRegion": regionToJson(auth.selectedRegion)
68
113
  ]
69
-
114
+
70
115
  self.notifyListeners("onFronteggAuthEvent", data: body as [String : Any])
71
116
  }
72
-
117
+
118
+
119
+ func regionToJson(_ region: RegionConfig?) -> [String:String]? {
120
+
121
+ if let reg = region {
122
+ return [
123
+ "baseUrl": reg.baseUrl,
124
+ "clientId": reg.clientId,
125
+ "key": reg.key
126
+ ]
127
+ }else {
128
+ return nil
129
+ }
130
+ }
131
+ func regionsToJson(_ regions: [RegionConfig]) -> [[String:String]] {
132
+
133
+ var regionData: [[String:String]] = []
134
+ regions.forEach { reg in
135
+ if let region = regionToJson(reg) {
136
+ regionData.append(region)
137
+ }
138
+ }
139
+
140
+ return regionData
141
+ }
142
+
73
143
  @objc func getConstants(_ call: CAPPluginCall) {
74
144
  call.resolve([
75
145
  "baseUrl": fronteggApp.baseUrl,
76
146
  "clientId": fronteggApp.clientId,
77
- "bundleId": Bundle.main.bundleIdentifier!
147
+ "bundleId": Bundle.main.bundleIdentifier!,
148
+ "isRegional": fronteggApp.auth.isRegional,
149
+ "regionData": regionsToJson(fronteggApp.auth.regionData)
78
150
  ])
79
151
  }
80
-
152
+
81
153
  @objc func login(_ call: CAPPluginCall) {
82
154
  DispatchQueue.main.sync {
83
155
  fronteggApp.auth.login()
84
156
  }
85
157
  call.resolve()
86
158
  }
87
-
159
+
88
160
  @objc func logout(_ call: CAPPluginCall) {
89
161
  DispatchQueue.main.sync {
90
162
  fronteggApp.auth.logout()
91
163
  }
92
164
  call.resolve()
93
165
  }
94
-
166
+
95
167
  @objc func switchTenant(_ call: CAPPluginCall) {
96
168
  guard let tenantId = call.options["tenantId"] as? String else {
97
169
  call.reject("No tenantId provided")
98
170
  return
99
171
  }
100
-
172
+
101
173
  fronteggApp.auth.switchTenant(tenantId: tenantId) { _ in
102
174
  call.resolve()
103
175
  }
104
176
  }
105
-
177
+
178
+ @objc func initWithRegion(_ call: CAPPluginCall) {
179
+ guard let regionKey = call.options["regionKey"] as? String else {
180
+ call.reject("No regionKey provided")
181
+ return
182
+ }
183
+
184
+ fronteggApp.initWithRegion(regionKey: regionKey)
185
+ }
186
+
106
187
  @objc func refreshToken(_ call: CAPPluginCall) {
107
-
188
+
108
189
  DispatchQueue.global(qos: .background).async {
109
190
  Task {
110
191
  await self.fronteggApp.auth.refreshTokenIfNeeded()
@@ -112,14 +193,14 @@ public class FronteggNativePlugin: CAPPlugin {
112
193
  }
113
194
  }
114
195
  }
115
-
196
+
116
197
  @objc func getAuthState(_ call: CAPPluginCall) {
117
198
  let auth = fronteggApp.auth
118
199
  var jsonUser: [String: Any]? = nil
119
200
  if let userData = try? JSONEncoder().encode(auth.user) {
120
201
  jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any]
121
202
  }
122
-
203
+
123
204
  let body: [String: Any?] = [
124
205
  "accessToken": auth.accessToken,
125
206
  "refreshToken": auth.refreshToken,
@@ -128,9 +209,9 @@ public class FronteggNativePlugin: CAPPlugin {
128
209
  "isLoading": auth.isLoading,
129
210
  "initializing": auth.initializing,
130
211
  "showLoader": auth.showLoader,
131
- "appLink": auth.appLink
212
+ "selectedRegion": regionToJson(auth.selectedRegion)
132
213
  ]
133
- call.resolve(body as? [String: Any] ?? [:])
214
+ call.resolve(body as [String: Any] )
134
215
  }
135
-
216
+
136
217
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontegg/ionic-capacitor",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Frontegg Ionic Capacitor SDK",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",