Skip to content

Commit 3e25b24

Browse files
authored
Use UIImplementation to open settings #376
ref DEV-2547
2 parents d02b055 + de716e0 commit 3e25b24

File tree

15 files changed

+38
-423
lines changed

15 files changed

+38
-423
lines changed

example/capacitor/src/pages/Home.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,19 @@ function AuthgearDemo() {
509509
}, [authenticationFlowGroup, showError, colorScheme, showAuthTime]);
510510

511511
const openSettings = useCallback(async () => {
512+
setLoading(true);
512513
if (isPlatformWeb()) {
513-
authgearWeb.open(WebPage.Settings);
514+
await authgearWeb.open(WebPage.Settings);
514515
} else {
515-
authgearCapacitor.open(CapacitorPage.Settings);
516+
try {
517+
await authgearCapacitor.open(CapacitorPage.Settings);
518+
} catch (e) {
519+
showError(e);
520+
} finally {
521+
setLoading(false);
522+
}
516523
}
517-
}, []);
524+
}, [showError]);
518525

519526
const changePassword = useCallback(async () => {
520527
if (isPlatformWeb()) {

packages/authgear-capacitor/android/src/main/AndroidManifest.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
<activity android:name=".OAuthCoordinatorActivity"
1212
android:exported="false"
1313
android:launchMode="singleTask" />
14-
<activity android:name=".WebViewActivity"
15-
android:exported="false"
16-
android:launchMode="singleTask"
17-
android:theme="@style/AuthgearTheme"
18-
android:configChanges="orientation|screenSize"/>
1914
<activity android:name=".WebKitWebViewActivity"
2015
android:exported="false"
2116
android:launchMode="singleTask"

packages/authgear-capacitor/android/src/main/java/com/authgear/capacitor/AuthgearPlugin.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,6 @@ private void handleOpenAuthorizeURLWithWebView(PluginCall call, ActivityResult a
186186
}
187187
}
188188

189-
@PluginMethod
190-
public void openURL(PluginCall call) {
191-
String urlString = call.getString("url");
192-
Uri uri = Uri.parse(urlString).normalizeScheme();
193-
194-
Context ctx = this.getContext();
195-
Intent intent = WebViewActivity.createIntent(ctx, uri.toString());
196-
this.startActivityForResult(call, intent, "handleOpenURL");
197-
}
198-
199-
@ActivityCallback
200-
private void handleOpenURL(PluginCall call, ActivityResult activityResult) {
201-
int resultCode = activityResult.getResultCode();
202-
if (resultCode == Activity.RESULT_CANCELED) {
203-
call.resolve();
204-
}
205-
if (resultCode == Activity.RESULT_OK) {
206-
call.resolve();
207-
}
208-
}
209-
210189
@PluginMethod
211190
public void checkBiometricSupported(PluginCall call) {
212191
JSObject android = call.getObject("android");

packages/authgear-capacitor/android/src/main/java/com/authgear/capacitor/WebViewActivity.java

Lines changed: 0 additions & 130 deletions
This file was deleted.

packages/authgear-capacitor/ios/Plugin/AuthgearPlugin.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
CAP_PLUGIN_METHOD(generateUUID, CAPPluginReturnPromise);
1414
CAP_PLUGIN_METHOD(openAuthorizeURL, CAPPluginReturnPromise);
1515
CAP_PLUGIN_METHOD(openAuthorizeURLWithWebView, CAPPluginReturnPromise);
16-
CAP_PLUGIN_METHOD(openURL, CAPPluginReturnPromise);
1716
CAP_PLUGIN_METHOD(checkBiometricSupported, CAPPluginReturnPromise);
1817
CAP_PLUGIN_METHOD(createBiometricPrivateKey, CAPPluginReturnPromise);
1918
CAP_PLUGIN_METHOD(signWithBiometricPrivateKey, CAPPluginReturnPromise);

packages/authgear-capacitor/ios/Plugin/AuthgearPlugin.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,6 @@ public class AuthgearPlugin: CAPPlugin {
133133
}
134134
}
135135

136-
@objc func openURL(_ call: CAPPluginCall) {
137-
let url = URL(string: call.getString("url")!)!
138-
139-
DispatchQueue.main.async {
140-
self.impl.openURL(window: (self.bridge?.webView?.window)!, url: url) { (error) in
141-
if let error = error {
142-
error.reject(call)
143-
} else {
144-
call.resolve()
145-
}
146-
}
147-
}
148-
}
149-
150136
@objc func checkBiometricSupported(_ call: CAPPluginCall) {
151137
let ios = call.getObject("ios")!
152138
let policyString = ios["policy"] as! String

packages/authgear-capacitor/ios/Plugin/AuthgearPluginImpl.swift

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -250,38 +250,6 @@ import Capacitor
250250
controller?.start()
251251
}
252252

253-
@objc func openURL(window: UIWindow, url: URL, completion: @escaping (Error?) -> Void) {
254-
if #available(iOS 12.0, *) {
255-
let scheme = "nocallback"
256-
var asWebSession: ASWebAuthenticationSession?
257-
asWebSession = ASWebAuthenticationSession(
258-
url: url,
259-
callbackURLScheme: scheme
260-
) { redirectURI, error in
261-
self.asWebAuthenticationSessionHandles.removeValue(forKey: asWebSession!)
262-
if let error = error {
263-
let nsError = error as NSError
264-
let isCancel = nsError.domain == ASWebAuthenticationSessionErrorDomain && nsError.code == ASWebAuthenticationSessionError.Code.canceledLogin.rawValue
265-
if isCancel {
266-
completion(nil)
267-
} else {
268-
completion(NSError.makeUnrecoverableAuthgearError(message: "openURL failed", error: error))
269-
}
270-
} else {
271-
completion(nil)
272-
}
273-
}
274-
self.asWebAuthenticationSessionHandles[asWebSession!] = window
275-
if #available(iOS 13.0, *) {
276-
asWebSession!.presentationContextProvider = self
277-
asWebSession!.prefersEphemeralWebBrowserSession = true
278-
}
279-
asWebSession!.start()
280-
} else {
281-
completion(NSError.makeUnrecoverableAuthgearError(message: "SDK supports only iOS 12.0 or newer", error: nil))
282-
}
283-
}
284-
285253
@objc func checkBiometricSupported(policyString: String) throws {
286254
if #available(iOS 11.3, *) {
287255
let policy = LAPolicy.from(string: policyString)!

packages/authgear-capacitor/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type TokenStorage,
66
type UserInfo,
77
AuthgearError,
8+
CancelError,
89
OAuthError,
910
SessionState,
1011
SessionStateChangeReason,
@@ -30,7 +31,6 @@ import { generateCodeVerifier, computeCodeChallenge } from "./pkce";
3031
import {
3132
generateUUID,
3233
getDeviceInfo,
33-
openURL,
3434
createBiometricPrivateKey,
3535
checkBiometricSupported,
3636
removeBiometricPrivateKey,
@@ -588,7 +588,19 @@ export class CapacitorContainer {
588588
platform,
589589
});
590590

591-
await openURL({ url: targetURL });
591+
try {
592+
await this.uiImplementation.openAuthorizationURL({
593+
url: targetURL,
594+
redirectURI: "nocallback://nocallback",
595+
shareCookiesWithDeviceBrowser: false,
596+
});
597+
} catch (e: unknown) {
598+
if (e instanceof CancelError) {
599+
// Ignore CancelError.
600+
return;
601+
}
602+
throw e;
603+
}
592604
}
593605

594606
/**

packages/authgear-capacitor/src/plugin.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export interface AuthgearPlugin {
2525
actionBarButtonTintColor?: string;
2626
iosIsInspectable?: boolean;
2727
}): Promise<{ redirectURI: string }>;
28-
openURL(options: { url: string }): Promise<void>;
2928
checkBiometricSupported(options: BiometricOptions): Promise<void>;
3029
createBiometricPrivateKey(
3130
options: BiometricPrivateKeyOptions
@@ -141,14 +140,6 @@ export async function openAuthorizeURLWithWebView(options: {
141140
}
142141
}
143142

144-
export async function openURL(options: { url: string }): Promise<void> {
145-
try {
146-
await Authgear.openURL(options);
147-
} catch (e: unknown) {
148-
throw _wrapError(e);
149-
}
150-
}
151-
152143
export async function checkBiometricSupported(
153144
options: BiometricOptions
154145
): Promise<void> {

packages/authgear-react-native/android/src/main/AndroidManifest.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
<activity android:name=".OAuthCoordinatorActivity"
1212
android:exported="false"
1313
android:launchMode="singleTask" />
14-
<activity android:name=".WebViewActivity"
15-
android:exported="false"
16-
android:launchMode="singleTask"
17-
android:theme="@style/AuthgearTheme"
18-
android:configChanges="orientation|screenSize"/>
1914
<activity android:name=".WebKitWebViewActivity"
2015
android:exported="false"
2116
android:launchMode="singleTask"

0 commit comments

Comments
 (0)