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

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

Closed
meroo36 opened this issue Sep 6, 2023 · 7 comments
Closed
Assignees
Labels
apple [See issue #68] Just Apple being Apple again... bug Something isn't working

Comments

@meroo36
Copy link

meroo36 commented Sep 6, 2023

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!)

  • Device: Iphone 13
  • OS: iOS 16.1.1
  • flutter_web_auth_2 version: 2.2.1
@meroo36 meroo36 added the bug Something isn't working label Sep 6, 2023
@ThexXTURBOXx ThexXTURBOXx added the apple [See issue #68] Just Apple being Apple again... label Sep 6, 2023
@meroo36
Copy link
Author

meroo36 commented Sep 7, 2023

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
Copy link
Author

meroo36 commented Sep 8, 2023

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.

@meroo36 meroo36 closed this as completed Sep 8, 2023
@andreivarlamov
Copy link

Same issue, is it fixed in the 3.0.0?

@ThexXTURBOXx
Copy link
Owner

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.

@ThexXTURBOXx ThexXTURBOXx reopened this Sep 15, 2023
@ozzy1873
Copy link

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
Copy link
Owner

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
Copy link
Owner

Will close for now until further feedback on 3.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apple [See issue #68] Just Apple being Apple again... bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants