Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LinusU#135 #28

Merged
merged 19 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion flutter_web_auth_2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ final result = await FlutterWebAuth2.authenticate(url: url.toString(), callbackU
// Extract code from resulting url
final code = Uri.parse(result).queryParameters['code'];

// Construct an Uri to Google's oauth2 endpoint
final url = Uri.https('www.googleapis.com', 'oauth2/v4/token');

// Use this code to get an access token
final response = await http.post('https://www.googleapis.com/oauth2/v4/token', body: {
final response = await http.post(url, body: {
'client_id': googleClientId,
'redirect_uri': '$callbackUrlScheme:/',
'grant_type': 'authorization_code',
Expand Down
35 changes: 24 additions & 11 deletions flutter_web_auth_2/ios/Classes/SwiftFlutterWebAuth2Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,33 @@ public class SwiftFlutterWebAuth2Plugin: NSObject, FlutterPlugin {
let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: completionHandler!)

if #available(iOS 13, *) {
guard var topController = UIApplication.shared.keyWindow?.rootViewController else {
result(FlutterError.aquireRootViewControllerFailed)
var rootViewController: UIViewController? = nil

// FlutterViewController
if (rootViewController == nil) {
rootViewController = UIApplication.shared.delegate?.window??.rootViewController as? FlutterViewController
}

// UIViewController
if (rootViewController == nil) {
rootViewController = UIApplication.shared.keyWindow?.rootViewController
}

// ACQUIRE_ROOT_VIEW_CONTROLLER_FAILED
if (rootViewController == nil) {
result(FlutterError.acquireRootViewControllerFailed)
return
}

while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
while let presentedViewController = rootViewController!.presentedViewController {
rootViewController = presentedViewController
}
if let nav = topController as? UINavigationController {
topController = nav.visibleViewController ?? topController
if let nav = rootViewController as? UINavigationController {
rootViewController = nav.visibleViewController ?? rootViewController
}

guard let contextProvider = topController as? ASWebAuthenticationPresentationContextProviding else {
result(FlutterError.aquireRootViewControllerFailed)
guard let contextProvider = rootViewController as? ASWebAuthenticationPresentationContextProviding else {
result(FlutterError.acquireRootViewControllerFailed)
return
}
session.presentationContextProvider = contextProvider
Expand Down Expand Up @@ -125,8 +138,8 @@ extension FlutterViewController: ASWebAuthenticationPresentationContextProviding
}
}

private extension FlutterError {
static var aquireRootViewControllerFailed: FlutterError {
return FlutterError(code: "AQUIRE_ROOT_VIEW_CONTROLLER_FAILED", message: "Failed to aquire root view controller", details: nil)
fileprivate extension FlutterError {
static var acquireRootViewControllerFailed: FlutterError {
return FlutterError(code: "ACQUIRE_ROOT_VIEW_CONTROLLER_FAILED", message: "Failed to acquire root view controller", details: nil)
}
}