Skip to content

Commit

Permalink
Port a few more changes from iOS to macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Oct 8, 2023
1 parent 96c3d1b commit 7605a3e
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions flutter_web_auth_2/macos/Classes/FlutterWebAuth2Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import FlutterMacOS
@available(OSX 10.15, *)
public class FlutterWebAuth2Plugin: NSObject, FlutterPlugin, ASWebAuthenticationPresentationContextProviding {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_web_auth_2", binaryMessenger: registrar.messenger)
let channel = FlutterMethodChannel(name: "flutter_web_auth_2", binaryMessenger: registrar.messenger())
let instance = FlutterWebAuth2Plugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "authenticate" {
let url = URL(string: (call.arguments as! Dictionary<String, AnyObject>)["url"] as! String)!
let callbackURLScheme = (call.arguments as! Dictionary<String, AnyObject>)["callbackUrlScheme"] as! String
let options = (call.arguments as! Dictionary<String, AnyObject>)["options"] as? Dictionary<String, AnyObject>

var keepMe: Any? = nil
if call.method == "authenticate",
let arguments = call.arguments as? [String: AnyObject],
let urlString = arguments["url"] as? String,
let url = URL(string: urlString),
let callbackURLScheme = arguments["callbackUrlScheme"] as? String,
let options = arguments["options"] as? [String: AnyObject]
{
var sessionToKeepAlive: Any? // if we do not keep the session alive, it will get closed immediately while showing the dialog
let completionHandler = { (url: URL?, err: Error?) in
keepMe = nil
sessionToKeepAlive = nil

if let err = err {
if case ASWebAuthenticationSessionError.canceledLogin = err {
Expand All @@ -30,17 +32,25 @@ public class FlutterWebAuth2Plugin: NSObject, FlutterPlugin, ASWebAuthentication
return
}

result(url!.absoluteString)
guard let url = url else {
result(FlutterError(code: "EUNKNOWN", message: "URL was null, but no error provided.", details: nil))
return
}

result(url.absoluteString)
}

let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: completionHandler)
if let preferEphemeral = options?["preferEphemeral"] as? Bool {
if let preferEphemeral = options["preferEphemeral"] as? Bool {
session.prefersEphemeralWebBrowserSession = preferEphemeral
}
session.presentationContextProvider = self

session.start()
keepMe = session
sessionToKeepAlive = session
} else if call.method == "cleanUpDanglingCalls" {
// we do not keep track of old callbacks on macOS, so nothing to do here
result(nil)
} else {
result(FlutterMethodNotImplemented)
}
Expand Down

0 comments on commit 7605a3e

Please sign in to comment.