ThexXTURBOXx / flutter_web_auth_2

Flutter plugin for authenticating a user with a web service
https://pub.dev/packages/flutter_web_auth_2
MIT License
51 stars 50 forks source link

Fatal error: Unexpectedly found nil while unwrapping an Optional value #62

Closed meroo36 closed 11 months ago

meroo36 commented 1 year ago

Describe the bug

App crashes after second time I initialize oauth2 flow in IOS.

To Reproduce

Steps to reproduce the behavior:

  1. Click on a dynamic link to initialize the app with oauth flow
  2. Authentication completes succesfully
  3. Everthing works unless you click the dynamic link once more
  4. You see the error: flutter_web_auth_2/SwiftFlutterWebAuth2Plugin.swift:29: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Device (please complete the following information!)

meroo36 commented 1 year ago

My guess is the sessionToKeepAlive variable is beeing nil for the second time.

var sessionToKeepAlive: Any? // if we do not keep the session alive, it will get closed immediately while showing the dialog
            completionHandler = { (url: URL?, err: Error?) in
                self.completionHandler = nil

                if #available(iOS 12, *) {
                    (sessionToKeepAlive as! ASWebAuthenticationSession).cancel() // Error Happens this line 29 in SwiftFlutterWebAuth2Plugin
                } else if #available(iOS 11, *) {
                    (sessionToKeepAlive as! SFAuthenticationSession).cancel()
                }

                sessionToKeepAlive = nil

EDIT: after commenting out the last line in the code block => "sessionToKeepAlive = nil". The issue seems to be gone.

@ThexXTURBOXx can you maybe give an hint why do we have to set it to nil and what would gone wrong if we dont set it to nil.

meroo36 commented 1 year ago

I could fix the issue by wrapping the oauth2 call with a timer. I guess you need to wait a little untill the app is on the foreground succesfully.

        Timer(Duration(milliseconds: lazyLoginDelayTime), () async {
          List<String> scopes = ["*****", "******"];
          await account.createOAuth2Session(
              provider: "*******", scopes: scopes);
          await ref.read(authProvider.notifier).login();
        });

Closing for now.

andreivarlamov commented 1 year ago

Same issue, is it fixed in the 3.0.0?

ThexXTURBOXx commented 1 year ago

I think I have changed nothing in the Apple-specific implementations. Also, I don't have any Apple hardware, so I cannot properly debug these issues. PRs are welcome, though.

ozzy1873 commented 12 months ago

Here is a stack trace, perhaps it will help:

0 (null) in Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value ()

1 0x000000010416d06c in closure #1 in SwiftFlutterWebAuth2Plugin.handle(_:result:) at /Users/xxx/.pub-cache/hosted/pub.dev/flutter_web_auth_2-2.2.1/ios/Classes/SwiftFlutterWebAuth2Plugin.swift:29

2 0x000000010416cc08 in closure #1 in SwiftFlutterWebAuth2Plugin.handle(_:result:) at /Users/xxx/.pub-cache/hosted/pub.dev/flutter_web_auth_2-2.2.1/ios/Classes/SwiftFlutterWebAuth2Plugin.swift:29

3 (null) in thunk for @escaping @callee_guaranteed (@in_guaranteed URL?, @guaranteed Error?) -> () ()

4 (null) in -[ASWebAuthenticationSession _startDryRun:] ()

5 0x000000010416ebb8 in specialized SwiftFlutterWebAuth2Plugin.handle(_:result:) at /Users/xxx/.pub-cache/hosted/pub.dev/flutter_web_auth_2-2.2.1/ios/Classes/SwiftFlutterWebAuth2Plugin.swift:100

6 (null) in specialized SwiftFlutterWebAuth2Plugin.handle(_:result:) ()

7 (null) in @objc SwiftFlutterWebAuth2Plugin.handle(_:result:) ()

ThexXTURBOXx commented 11 months ago

Thanks for the stacktrace. However, it sadly does not contain any additional information. I have just released version 3.0.0-alpha.4. It could contain a valid fix, but I don't have any Apple hardware to test this. So, please report back in case you tried it! Also, if you are upgrading from 2.x, note the migration guide

ThexXTURBOXx commented 11 months ago

Will close for now until further feedback on 3.x