|
| 1 | +import Foundation |
| 2 | +import AuthenticationServices |
| 3 | + |
| 4 | +class HybridASWebAuthenticationSession: HybridASWebAuthenticationSessionSpec { |
| 5 | + var hybridContext = margelo.nitro.HybridContext() |
| 6 | + |
| 7 | + var memorySize: Int { |
| 8 | + return getSizeOf(self) |
| 9 | + } |
| 10 | + |
| 11 | + private var authSession: ASWebAuthenticationSession? |
| 12 | + |
| 13 | + var prefersEphemeralWebBrowserSession: Bool = false |
| 14 | + |
| 15 | + func start(params: ASWebAuthenticationSessionStartParams) throws -> Void { |
| 16 | + NSLog("HybridASWebAuthenticationSession.start(url:%@) is being called", params.url) |
| 17 | + |
| 18 | + guard let nativeUrl = URL(string: params.url) else { |
| 19 | + throw NSError(domain: "HybridASWebAuthenticationSession", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"]) |
| 20 | + } |
| 21 | + |
| 22 | + authSession = ASWebAuthenticationSession(url: nativeUrl, callbackURLScheme: nil) { session, error in |
| 23 | + if let error = error { |
| 24 | + NSLog("ASWebAuthenticationSession failed with error: %@", error.localizedDescription) |
| 25 | + // TODO: Implement error handling and callback to JavaScript |
| 26 | + } else if let callbackURL = session { |
| 27 | + NSLog("ASWebAuthenticationSession succeeded with URL: %@", callbackURL.absoluteString) |
| 28 | + // TODO: Implement success callback to JavaScript with the callbackURL |
| 29 | + } else { |
| 30 | + NSLog("ASWebAuthenticationSession completed without error or callback URL") |
| 31 | + // TODO: Implement callback to JavaScript for completion without result |
| 32 | + } |
| 33 | + |
| 34 | + self.authSession = nil |
| 35 | + } |
| 36 | + |
| 37 | + if !(authSession?.start() ?? false) { |
| 38 | + throw NSError(domain: "HybridASWebAuthenticationSession", code: 1, userInfo: [NSLocalizedDescriptionKey: "ASWebAuthenticationSession failed to start"]) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + func cancel() throws -> Void { |
| 43 | + guard let authSession = authSession else { |
| 44 | + throw NSError(domain: "HybridASWebAuthenticationSession", code: 2, userInfo: [NSLocalizedDescriptionKey: "ASWebAuthenticationSession is not initialized"]) |
| 45 | + } |
| 46 | + |
| 47 | + authSession.cancel() |
| 48 | + self.authSession = nil |
| 49 | + } |
| 50 | +} |
0 commit comments