@frontegg/ionic-capacitor 2.0.0 → 2.0.1
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.
|
@@ -42,8 +42,11 @@ public class FronteggNativePlugin extends Plugin {
|
|
|
42
42
|
List<RegionConfig> regions = new ArrayList<>();
|
|
43
43
|
JSONArray array;
|
|
44
44
|
try {
|
|
45
|
-
array = this.getConfig().getConfigJSON().
|
|
45
|
+
array = this.getConfig().getConfigJSON().optJSONArray("regions");
|
|
46
46
|
|
|
47
|
+
if (array == null) {
|
|
48
|
+
array = new JSONArray();
|
|
49
|
+
}
|
|
47
50
|
for (int i = 0; i < array.length(); i++) {
|
|
48
51
|
JSONObject regionJson = (JSONObject) array.get(i);
|
|
49
52
|
regions.add(new RegionConfig(
|
|
@@ -61,9 +64,12 @@ public class FronteggNativePlugin extends Plugin {
|
|
|
61
64
|
PluginConfig config = this.getConfig();
|
|
62
65
|
String baseUrl = config.getString("baseUrl");
|
|
63
66
|
String clientId = config.getString("clientId");
|
|
64
|
-
|
|
65
|
-
if (baseUrl == null || clientId == null
|
|
66
|
-
throw new RuntimeException("Missing required config parameters: baseUrl, clientId
|
|
67
|
+
|
|
68
|
+
if (baseUrl == null || clientId == null) {
|
|
69
|
+
throw new RuntimeException("Missing required config parameters: baseUrl, clientId");
|
|
70
|
+
}
|
|
71
|
+
if(baseUrl.startsWith("https://")) {
|
|
72
|
+
baseUrl = baseUrl.substring(baseUrl.indexOf("://") + 3);
|
|
67
73
|
}
|
|
68
74
|
FronteggApp.Companion.init(
|
|
69
75
|
baseUrl,
|
|
@@ -63,7 +63,7 @@ declare type FronteggNativeRegionOptions = {
|
|
|
63
63
|
* @since 1.0.0
|
|
64
64
|
* @example [{key: "us", baseUrl: "https://us-api.frontegg.com", clientId: "us-client-id"}]
|
|
65
65
|
*/
|
|
66
|
-
regions
|
|
66
|
+
regions: RegionConfig[];
|
|
67
67
|
};
|
|
68
68
|
declare type FronteggNativeOptions = (FronteggNativeStandardOptions | FronteggNativeRegionOptions) & {
|
|
69
69
|
handleLoginWithSocialLogin?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["\nimport type { ListenerCallback, PluginListenerHandle } from '@capacitor/core';\nimport type { ITenantsResponse, IUserProfile } from '@frontegg/rest-api';\n\nexport type User = IUserProfile & {\n tenants: ITenantsResponse[];\n activeTenant: ITenantsResponse\n}\n\nexport interface FronteggState {\n accessToken: string | null;\n refreshToken: string | null;\n isAuthenticated: boolean;\n user: User | null;\n showLoader: boolean;\n selectedRegion: string | null;\n}\n\n\nexport type SubscribeFunc<T, K extends keyof T> = (value: T[K]) => void\nexport type SubscribeMap<T> = {\n [K in keyof T]: Set<SubscribeFunc<T, K>>\n}\n\n\nexport interface FronteggConstants {\n baseUrl: string;\n clientId: string;\n bundleId: string;\n isRegional: boolean;\n regionData?: { key: string, baseUrl: string, clientId: string }[]\n}\n\nexport interface FronteggNativePlugin {\n addListener(eventName: string, listenerFunc: ListenerCallback): Promise<PluginListenerHandle> & PluginListenerHandle\n\n getConstants(): Promise<FronteggConstants>;\n\n getAuthState(): Promise<FronteggState>;\n\n login(): Promise<void>;\n\n logout(): void;\n\n switchTenant(payload: { tenantId: string }): Promise<void>;\n\n /**\n * used to initialize the plugin with multiple regions\n * for more information see:\n * iOS: https://github.com/frontegg/frontegg-ios-swift#multi-region-support\n * Android: https://github.com/frontegg/frontegg-android-kotlin#multi-region-support\n */\n initWithRegion(payload: { regionKey: string }): Promise<void>;\n\n refreshToken(): Promise<void>;\n\n}\n\n\n\nexport type RegionConfig = {\n key: string;\n baseUrl: string;\n clientId: string;\n};\n\n\n\ntype FronteggNativeStandardOptions = {\n baseUrl: string;\n clientId: string;\n\n}\ntype FronteggNativeRegionOptions = {\n /**\n * This is an array of regions to be used as frontegg app.\n *\n * @since 1.0.0\n * @example [{key: \"us\", baseUrl: \"https://us-api.frontegg.com\", clientId: \"us-client-id\"}]\n */\n regions
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["\nimport type { ListenerCallback, PluginListenerHandle } from '@capacitor/core';\nimport type { ITenantsResponse, IUserProfile } from '@frontegg/rest-api';\n\nexport type User = IUserProfile & {\n tenants: ITenantsResponse[];\n activeTenant: ITenantsResponse\n}\n\nexport interface FronteggState {\n accessToken: string | null;\n refreshToken: string | null;\n isAuthenticated: boolean;\n user: User | null;\n showLoader: boolean;\n selectedRegion: string | null;\n}\n\n\nexport type SubscribeFunc<T, K extends keyof T> = (value: T[K]) => void\nexport type SubscribeMap<T> = {\n [K in keyof T]: Set<SubscribeFunc<T, K>>\n}\n\n\nexport interface FronteggConstants {\n baseUrl: string;\n clientId: string;\n bundleId: string;\n isRegional: boolean;\n regionData?: { key: string, baseUrl: string, clientId: string }[]\n}\n\nexport interface FronteggNativePlugin {\n addListener(eventName: string, listenerFunc: ListenerCallback): Promise<PluginListenerHandle> & PluginListenerHandle\n\n getConstants(): Promise<FronteggConstants>;\n\n getAuthState(): Promise<FronteggState>;\n\n login(): Promise<void>;\n\n logout(): void;\n\n switchTenant(payload: { tenantId: string }): Promise<void>;\n\n /**\n * used to initialize the plugin with multiple regions\n * for more information see:\n * iOS: https://github.com/frontegg/frontegg-ios-swift#multi-region-support\n * Android: https://github.com/frontegg/frontegg-android-kotlin#multi-region-support\n */\n initWithRegion(payload: { regionKey: string }): Promise<void>;\n\n refreshToken(): Promise<void>;\n\n}\n\n\n\nexport type RegionConfig = {\n key: string;\n baseUrl: string;\n clientId: string;\n};\n\n\n\ntype FronteggNativeStandardOptions = {\n baseUrl: string;\n clientId: string;\n\n}\ntype FronteggNativeRegionOptions = {\n /**\n * This is an array of regions to be used as frontegg app.\n *\n * @since 1.0.0\n * @example [{key: \"us\", baseUrl: \"https://us-api.frontegg.com\", clientId: \"us-client-id\"}]\n */\n regions: RegionConfig[];\n}\ntype FronteggNativeOptions = (FronteggNativeStandardOptions | FronteggNativeRegionOptions) & {\n handleLoginWithSocialLogin?: boolean;\n handleLoginWithSSO?: boolean;\n}\n// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\ndeclare module \"@capacitor/cli\" {\n export interface PluginsConfig {\n /**\n * You can configure the way the push notifications are displayed when the app is in foreground.\n */\n FronteggNative?: FronteggNativeOptions;\n }\n}\n"]}
|
|
@@ -11,29 +11,29 @@ 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
27
|
let config = self.getConfig()
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
let handleLoginWithSocialLogin = config.getBoolean("handleLoginWithSocialLogin", true)
|
|
30
30
|
let handleLoginWithSSO = config.getBoolean("handleLoginWithSSO", false)
|
|
31
|
-
|
|
32
|
-
if let array = config.getArray("regions"),
|
|
31
|
+
|
|
32
|
+
if let array = config.getArray("regions", []),
|
|
33
33
|
array.count > 0 {
|
|
34
34
|
print("region initialization")
|
|
35
35
|
var regions:[RegionConfig] = []
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
array.forEach {item in
|
|
38
38
|
if let dict = item as? [String:String] {
|
|
39
39
|
regions.append(RegionConfig(
|
|
@@ -43,7 +43,7 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
43
43
|
))
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
if(regions.isEmpty){
|
|
48
48
|
print("Frontegg Error: Missing regions configurations")
|
|
49
49
|
exit(1)
|
|
@@ -64,9 +64,9 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
64
64
|
exit(1)
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
70
|
let auth = fronteggApp.auth
|
|
71
71
|
var anyChange: AnyPublisher<Void, Never> {
|
|
72
72
|
return Publishers.Merge8 (
|
|
@@ -81,26 +81,26 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
81
81
|
)
|
|
82
82
|
.eraseToAnyPublisher()
|
|
83
83
|
}
|
|
84
|
-
|
|
84
|
+
|
|
85
85
|
anyChange.sink(receiveValue: { () in
|
|
86
86
|
self.debounce() {
|
|
87
87
|
self.sendEvent()
|
|
88
88
|
}
|
|
89
89
|
}).store(in: &cancellables)
|
|
90
|
-
|
|
90
|
+
|
|
91
91
|
self.sendEvent()
|
|
92
92
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
96
|
func sendEvent() {
|
|
97
97
|
let auth = fronteggApp.auth
|
|
98
|
-
|
|
98
|
+
|
|
99
99
|
var jsonUser: [String: Any]? = nil
|
|
100
100
|
if let userData = try? JSONEncoder().encode(auth.user) {
|
|
101
101
|
jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any]
|
|
102
102
|
}
|
|
103
|
-
|
|
103
|
+
|
|
104
104
|
let body: [String: Any?] = [
|
|
105
105
|
"accessToken": auth.accessToken,
|
|
106
106
|
"refreshToken": auth.refreshToken,
|
|
@@ -111,13 +111,13 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
111
111
|
"showLoader": auth.showLoader,
|
|
112
112
|
"selectedRegion": regionToJson(auth.selectedRegion)
|
|
113
113
|
]
|
|
114
|
-
|
|
114
|
+
|
|
115
115
|
self.notifyListeners("onFronteggAuthEvent", data: body as [String : Any])
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
|
|
118
|
+
|
|
119
119
|
func regionToJson(_ region: RegionConfig?) -> [String:String]? {
|
|
120
|
-
|
|
120
|
+
|
|
121
121
|
if let reg = region {
|
|
122
122
|
return [
|
|
123
123
|
"baseUrl": reg.baseUrl,
|
|
@@ -129,17 +129,17 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
func regionsToJson(_ regions: [RegionConfig]) -> [[String:String]] {
|
|
132
|
-
|
|
132
|
+
|
|
133
133
|
var regionData: [[String:String]] = []
|
|
134
134
|
regions.forEach { reg in
|
|
135
135
|
if let region = regionToJson(reg) {
|
|
136
136
|
regionData.append(region)
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
|
|
139
|
+
|
|
140
140
|
return regionData
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
|
|
143
143
|
@objc func getConstants(_ call: CAPPluginCall) {
|
|
144
144
|
call.resolve([
|
|
145
145
|
"baseUrl": fronteggApp.baseUrl,
|
|
@@ -149,43 +149,43 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
149
149
|
"regionData": regionsToJson(fronteggApp.auth.regionData)
|
|
150
150
|
])
|
|
151
151
|
}
|
|
152
|
-
|
|
152
|
+
|
|
153
153
|
@objc func login(_ call: CAPPluginCall) {
|
|
154
154
|
DispatchQueue.main.sync {
|
|
155
155
|
fronteggApp.auth.login()
|
|
156
156
|
}
|
|
157
157
|
call.resolve()
|
|
158
158
|
}
|
|
159
|
-
|
|
159
|
+
|
|
160
160
|
@objc func logout(_ call: CAPPluginCall) {
|
|
161
161
|
DispatchQueue.main.sync {
|
|
162
162
|
fronteggApp.auth.logout()
|
|
163
163
|
}
|
|
164
164
|
call.resolve()
|
|
165
165
|
}
|
|
166
|
-
|
|
166
|
+
|
|
167
167
|
@objc func switchTenant(_ call: CAPPluginCall) {
|
|
168
168
|
guard let tenantId = call.options["tenantId"] as? String else {
|
|
169
169
|
call.reject("No tenantId provided")
|
|
170
170
|
return
|
|
171
171
|
}
|
|
172
|
-
|
|
172
|
+
|
|
173
173
|
fronteggApp.auth.switchTenant(tenantId: tenantId) { _ in
|
|
174
174
|
call.resolve()
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
|
-
|
|
177
|
+
|
|
178
178
|
@objc func initWithRegion(_ call: CAPPluginCall) {
|
|
179
179
|
guard let regionKey = call.options["regionKey"] as? String else {
|
|
180
180
|
call.reject("No regionKey provided")
|
|
181
181
|
return
|
|
182
182
|
}
|
|
183
|
-
|
|
183
|
+
|
|
184
184
|
fronteggApp.initWithRegion(regionKey: regionKey)
|
|
185
185
|
}
|
|
186
|
-
|
|
186
|
+
|
|
187
187
|
@objc func refreshToken(_ call: CAPPluginCall) {
|
|
188
|
-
|
|
188
|
+
|
|
189
189
|
DispatchQueue.global(qos: .background).async {
|
|
190
190
|
Task {
|
|
191
191
|
await self.fronteggApp.auth.refreshTokenIfNeeded()
|
|
@@ -193,14 +193,14 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
|
-
|
|
196
|
+
|
|
197
197
|
@objc func getAuthState(_ call: CAPPluginCall) {
|
|
198
198
|
let auth = fronteggApp.auth
|
|
199
199
|
var jsonUser: [String: Any]? = nil
|
|
200
200
|
if let userData = try? JSONEncoder().encode(auth.user) {
|
|
201
201
|
jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any]
|
|
202
202
|
}
|
|
203
|
-
|
|
203
|
+
|
|
204
204
|
let body: [String: Any?] = [
|
|
205
205
|
"accessToken": auth.accessToken,
|
|
206
206
|
"refreshToken": auth.refreshToken,
|
|
@@ -213,5 +213,5 @@ public class FronteggNativePlugin: CAPPlugin {
|
|
|
213
213
|
]
|
|
214
214
|
call.resolve(body as [String: Any] )
|
|
215
215
|
}
|
|
216
|
-
|
|
216
|
+
|
|
217
217
|
}
|