Skip to content

Commit 2e7223c

Browse files
committed
feat: implement callbackURLScheme in ASWebAuthenticationSession
1 parent 8170c22 commit 2e7223c

File tree

8 files changed

+101
-54
lines changed

8 files changed

+101
-54
lines changed

example/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function App(): React.JSX.Element {
122122
onPress={() => {
123123
ASWebAuthenticationSession.start({
124124
url: AUTH_URL,
125+
callbackURLScheme: 'nitro-in-app-browser-example',
125126
})
126127
}}
127128
/>

example/app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"name": "NitroInAppBrowserExample",
3-
"displayName": "NitroInAppBrowserExample"
3+
"displayName": "NitroInAppBrowserExample",
4+
"scheme": "nitro-in-app-browser-example"
45
}
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
4-
<dict>
5-
<key>CFBundleDevelopmentRegion</key>
6-
<string>en</string>
7-
<key>CFBundleDisplayName</key>
8-
<string>NitroInAppBrowserExample</string>
9-
<key>CFBundleExecutable</key>
10-
<string>$(EXECUTABLE_NAME)</string>
11-
<key>CFBundleIdentifier</key>
12-
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
13-
<key>CFBundleInfoDictionaryVersion</key>
14-
<string>6.0</string>
15-
<key>CFBundleName</key>
16-
<string>$(PRODUCT_NAME)</string>
17-
<key>CFBundlePackageType</key>
18-
<string>APPL</string>
19-
<key>CFBundleShortVersionString</key>
20-
<string>$(MARKETING_VERSION)</string>
21-
<key>CFBundleSignature</key>
22-
<string>????</string>
23-
<key>CFBundleVersion</key>
24-
<string>$(CURRENT_PROJECT_VERSION)</string>
25-
<key>LSRequiresIPhoneOS</key>
26-
<true/>
27-
<key>NSAppTransportSecurity</key>
284
<dict>
29-
<!-- Do not change NSAllowsArbitraryLoads to true, or you will risk app rejection! -->
30-
<key>NSAllowsArbitraryLoads</key>
31-
<false/>
32-
<key>NSAllowsLocalNetworking</key>
33-
<true/>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>NitroInAppBrowserExample</string>
9+
<key>CFBundleExecutable</key>
10+
<string>$(EXECUTABLE_NAME)</string>
11+
<key>CFBundleIdentifier</key>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>$(PRODUCT_NAME)</string>
17+
<key>CFBundlePackageType</key>
18+
<string>APPL</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>$(MARKETING_VERSION)</string>
21+
<key>CFBundleSignature</key>
22+
<string>????</string>
23+
<key>CFBundleVersion</key>
24+
<string>$(CURRENT_PROJECT_VERSION)</string>
25+
<key>LSRequiresIPhoneOS</key>
26+
<true />
27+
<key>NSAppTransportSecurity</key>
28+
<dict>
29+
<key>NSAllowsArbitraryLoads</key>
30+
<false />
31+
<key>NSAllowsLocalNetworking</key>
32+
<true />
33+
</dict>
34+
<key>NSLocationWhenInUseUsageDescription</key>
35+
<string></string>
36+
<key>UILaunchStoryboardName</key>
37+
<string>LaunchScreen</string>
38+
<key>UIRequiredDeviceCapabilities</key>
39+
<array>
40+
<string>arm64</string>
41+
</array>
42+
<key>UISupportedInterfaceOrientations</key>
43+
<array>
44+
<string>UIInterfaceOrientationPortrait</string>
45+
<string>UIInterfaceOrientationLandscapeLeft</string>
46+
<string>UIInterfaceOrientationLandscapeRight</string>
47+
</array>
48+
<key>UIViewControllerBasedStatusBarAppearance</key>
49+
<false />
50+
<key>CFBundleURLName</key>
51+
<string>nitro-in-app-browser-example</string>
52+
<key>CFBundleURLSchemes</key>
53+
<array>
54+
<string>nitro-in-app-browser-example</string>
55+
</array>
3456
</dict>
35-
<key>NSLocationWhenInUseUsageDescription</key>
36-
<string></string>
37-
<key>UILaunchStoryboardName</key>
38-
<string>LaunchScreen</string>
39-
<key>UIRequiredDeviceCapabilities</key>
40-
<array>
41-
<string>arm64</string>
42-
</array>
43-
<key>UISupportedInterfaceOrientations</key>
44-
<array>
45-
<string>UIInterfaceOrientationPortrait</string>
46-
<string>UIInterfaceOrientationLandscapeLeft</string>
47-
<string>UIInterfaceOrientationLandscapeRight</string>
48-
</array>
49-
<key>UIViewControllerBasedStatusBarAppearance</key>
50-
<false/>
51-
</dict>
52-
</plist>
57+
</plist>

ios/HybridASWebAuthenticationSession.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import AuthenticationServices
33

4-
class HybridASWebAuthenticationSession: HybridASWebAuthenticationSessionSpec {
4+
class HybridASWebAuthenticationSession: NSObject, HybridASWebAuthenticationSessionSpec, ASWebAuthenticationPresentationContextProviding {
55
var hybridContext = margelo.nitro.HybridContext()
66

77
var memorySize: Int {
@@ -12,14 +12,19 @@ class HybridASWebAuthenticationSession: HybridASWebAuthenticationSessionSpec {
1212

1313
var prefersEphemeralWebBrowserSession: Bool = false
1414

15+
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
16+
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
17+
return scene?.windows.first ?? UIWindow()
18+
}
19+
1520
func start(params: ASWebAuthenticationSessionStartParams) throws -> Void {
1621
NSLog("HybridASWebAuthenticationSession.start(url:%@) is being called", params.url)
1722

1823
guard let nativeUrl = URL(string: params.url) else {
1924
throw NSError(domain: "HybridASWebAuthenticationSession", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"])
2025
}
2126

22-
authSession = ASWebAuthenticationSession(url: nativeUrl, callbackURLScheme: nil) { session, error in
27+
authSession = ASWebAuthenticationSession(url: nativeUrl, callbackURLScheme: params.callbackURLScheme) { session, error in
2328
if let error = error {
2429
NSLog("ASWebAuthenticationSession failed with error: %@", error.localizedDescription)
2530
// TODO: Implement error handling and callback to JavaScript
@@ -34,6 +39,8 @@ class HybridASWebAuthenticationSession: HybridASWebAuthenticationSessionSpec {
3439
self.authSession = nil
3540
}
3641

42+
authSession?.presentationContextProvider = self
43+
3744
if !(authSession?.start() ?? false) {
3845
throw NSError(domain: "HybridASWebAuthenticationSession", code: 1, userInfo: [NSLocalizedDescriptionKey: "ASWebAuthenticationSession failed to start"])
3946
}

nitrogen/generated/ios/swift/ASWebAuthenticationSessionStartParams.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public extension ASWebAuthenticationSessionStartParams {
1818
/**
1919
* Create a new instance of `ASWebAuthenticationSessionStartParams`.
2020
*/
21-
init(url: String) {
22-
self.init(std.string(url))
21+
init(url: String, callbackURLScheme: String) {
22+
self.init(std.string(url), std.string(callbackURLScheme))
2323
}
2424

2525
var url: String {
@@ -32,4 +32,15 @@ public extension ASWebAuthenticationSessionStartParams {
3232
self.__url = std.string(newValue)
3333
}
3434
}
35+
36+
var callbackURLScheme: String {
37+
@inline(__always)
38+
get {
39+
return String(self.__callbackURLScheme)
40+
}
41+
@inline(__always)
42+
set {
43+
self.__callbackURLScheme = std.string(newValue)
44+
}
45+
}
3546
}

nitrogen/generated/shared/c++/ASWebAuthenticationSessionStartParams.hpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ namespace margelo::nitro::inappbrowser {
3030
struct ASWebAuthenticationSessionStartParams {
3131
public:
3232
std::string url SWIFT_PRIVATE;
33+
std::string callbackURLScheme SWIFT_PRIVATE;
3334

3435
public:
35-
explicit ASWebAuthenticationSessionStartParams(std::string url): url(url) {}
36+
explicit ASWebAuthenticationSessionStartParams(std::string url, std::string callbackURLScheme): url(url), callbackURLScheme(callbackURLScheme) {}
3637
};
3738

3839
} // namespace margelo::nitro::inappbrowser
@@ -47,12 +48,14 @@ namespace margelo::nitro {
4748
static inline ASWebAuthenticationSessionStartParams fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
4849
jsi::Object obj = arg.asObject(runtime);
4950
return ASWebAuthenticationSessionStartParams(
50-
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "url"))
51+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "url")),
52+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "callbackURLScheme"))
5153
);
5254
}
5355
static inline jsi::Value toJSI(jsi::Runtime& runtime, const ASWebAuthenticationSessionStartParams& arg) {
5456
jsi::Object obj(runtime);
5557
obj.setProperty(runtime, "url", JSIConverter<std::string>::toJSI(runtime, arg.url));
58+
obj.setProperty(runtime, "callbackURLScheme", JSIConverter<std::string>::toJSI(runtime, arg.callbackURLScheme));
5659
return obj;
5760
}
5861
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
@@ -61,6 +64,7 @@ namespace margelo::nitro {
6164
}
6265
jsi::Object obj = value.getObject(runtime);
6366
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "url"))) return false;
67+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "callbackURLScheme"))) return false;
6468
return true;
6569
}
6670
};

src/ASWebAuthenticationSession.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import type { ASWebAuthenticationSession as ASWebAuthenticationSessionType } fro
44

55
export * from './specs/ASWebAuthenticationSession.nitro'
66

7+
/**
8+
* ASWebAuthenticationSession
9+
* A session that an app uses to authenticate a user through a web service.
10+
*
11+
* @platform ios
12+
* @docs https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession
13+
*/
714
let ASWebAuthenticationSession: ASWebAuthenticationSessionType
815

916
const ASWebAuthenticationSessionStub: ASWebAuthenticationSessionType = {

src/specs/ASWebAuthenticationSession.nitro.ts

+11
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,16 @@ export interface ASWebAuthenticationSession
88
}
99

1010
export interface ASWebAuthenticationSessionStartParams {
11+
/**
12+
* The URL to load in the authentication session.
13+
*
14+
* Example: `https://example.com/auth`
15+
*/
1116
url: string
17+
/**
18+
* The URL scheme to use for the callback.
19+
*
20+
* Example: `my-scheme`
21+
*/
22+
callbackURLScheme: string
1223
}

0 commit comments

Comments
 (0)