aws-amplify/amplify-swift

The external login UI is not working when closing the dialog programmatically

medhatIbsais-Harri opened this issue · 12 comments

Describe the bug

The external login dialog is not appearing again when dismissing it programmatically, the idea that we have an app the at some states we changes the root view controller, if the user prompted the external login UI and the root view changed, if the user tried again to prompt the external login dialog, it will get stuck and the dialog will not appear again,
we have two apps, the first one uses Amplify and the other one uses AWSMobileClient, it works normally on the who that uses AWSMobileClient, but its not working on Amplify, check the screen recording on Drive please
Screen recordings

you can use the code below for example to dismiss the presented view controller

 // check if there is any presented view
 if let presentedViewController = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.rootViewController?.presentedViewController {
                
  // dismiss
 presentedViewController.dismiss(animated: false, completion: nil)
 }

Steps To Reproduce

Steps to reproduce the behavior:
1. Type the external login company URL
2. When the external login dialog appears, change the root controller or simply dismiss the presented view controller like the code added in the description 
3. Try to prompt the dialog again 
4. It will not appear

Expected behavior

it should work normally and show the dialog

Amplify Framework Version

1.31.0

Amplify Categories

Auth

Dependency manager

Cocoapods

Swift version

5.0

CLI version

Not installed

Xcode version

15.4

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

17.4

Device

iPad mini 6th generation

Specific to simulators

No response

Additional context

No response

@medhatIbsais-Harri Would you be able to share more context on the code that is being used? That is more code snippets around how your login UI is using Amplify

Hello @harsh62, yes sure, check the code below please, also just an important note, that after dismissing the external login view programmatically, anything related to amplify will hang even the sign out Amplify.Auth.signOut,

simply just add the code below and start a timer for example to dismiss the presented view "as the code in description" after 10 seconds, then everything related Amplify will hang.

let hostedUIOptions = AuthSocialWebUISignInOperation.Request.Options(scopes: ["profile","openid", "email"], signInQueryParameters: nil, signOutQueryParameters: nil, tokenQueryParameters: nil, pluginOptions: AWSAuthWebUISignInOptions(preferPrivateSession: true))
        
Amplify.Auth.signInWithWebUI(for:  AuthProvider.custom(source.name), presentationAnchor: UIApplication.shared.windows.filter({$0.isKeyWindow}).first!, options: hostedUIOptions, listener: { result in
.......
})

@medhatIbsais-Harri Could you try not passing the presentation anchor and let the library create its own. So something like:

Amplify.Auth.signInWithWebUI(for:  AuthProvider.custom(source.name), options: hostedUIOptions, listener: { result in
.......
})

Hello @harsh62, is the above code working with you?, because the presentation anchor is a required parameter

Screenshot 2024-10-11 at 10 29 06 AM

Hello @harsh62, can you please try the case I sent you, its very easy to reproduce, just add the below code, then after its being dismissed try to call Amplify.Auth.signInWithWebUI again it will never open the dialog, also even if you tried to call anything related to Amplify it will not work even the Amplify.Auth.signOut

let hostedUIOptions = AuthSocialWebUISignInOperation.Request.Options(scopes: ["profile","openid", "email"], signInQueryParameters: nil, signOutQueryParameters: nil, tokenQueryParameters: nil, pluginOptions: AWSAuthWebUISignInOptions(preferPrivateSession: true))
        
Amplify.Auth.signInWithWebUI(for:  AuthProvider.custom(source.name), presentationAnchor: UIApplication.shared.windows.filter({$0.isKeyWindow}).first!, options: hostedUIOptions, listener: { result in
.......
})

Timer.scheduledTimer(withTimeInterval: 15, repeats: false) { _ in
            
      if let presentedViewController = UIApplication.shared.windows.first(where: { $0.isKeyWindow})?.rootViewController?.presentedViewController {

       presentedViewController.dismiss(animated: false, completion: nil)
           }
  }


This is because you are forcefully dismissing the view controller in the following code snippet.

Timer.scheduledTimer(withTimeInterval: 15, repeats: false) { _ in        
      if let presentedViewController = UIApplication.shared.windows.first(where: { $0.isKeyWindow})?.rootViewController?.presentedViewController {
       presentedViewController.dismiss(animated: false, completion: nil)
    }
  }

Amplify signInWithWebUI is listening for callbacks from ASWebAuthentication and in this case the callback is not triggered. Because the callback is not triggered, Amplify is still waiting and discarding any other requests that come in.
We have a state machine running that currently in waiting for sign in state. I would suggest that you rely on the users either completing the flow or cancelling the flow, which would call the correct callbacks and will not cause any problems.


Also could you try passing nil value to the presentation anchor for your original issue, if nil value is received, Amplify would create its own presentation anchor.

Hello @harsh62, for passing nil it will not work, because the presentation anchor is not an optional parameter, whatever regarding the dismiss issue, can't you override the Apple dismiss method and call the cancel call back in it?
because the above issue is only happening on Amplify side, its working well with AWSMobileClient

@medhatIbsais-Harri I just saw now that you are using Amplify V1 (1.31.0). Can you migrate to Amplify V2 as Amplify V1 has been deprecated?

Hello @harsh62, yes we have the plan to release our apps with Amplify v2, but for the current issue its also happens on lower version of Amplify, and what is the cause that its working normally on AWSMobileClient?

but for the current issue its also happens on lower version of Amplify, and what is the cause that its working normally on AWSMobileClient

Amplify V1 wraps some of its own logic on top of AWSMobileClient, there could be a bug or unhandled code paths in the implementation which is causing this issue.

For now, I would suggest trying out Amplify V2 with and without presentation anchor. If this issue still persists in Amplify V2, then please let us know and we will investigate further.

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.