AzureAD / microsoft-authentication-library-for-objc

Microsoft Authentication Library (MSAL) for iOS and macOS
http://aka.ms/aadv2
MIT License
264 stars 142 forks source link

Web Browser Dismissing Automatically When App Goes to Background During Login (MSAL) #2381

Open vinaykumar0339 opened 6 days ago

vinaykumar0339 commented 6 days ago

https://github.com/user-attachments/assets/c4725ced-251a-4ccc-8979-ad26c489cf8c

ameyapat commented 6 days ago

@vinaykumar0339 we can't replicate the issue with our test app. Can you provide MSAL logs for your issue? Are you sure the view controller is not deallocated before the callback is called?

vinaykumar0339 commented 5 days ago

@ameyapat Thanks for the update.

I found the root cause. The below code causes the model to be dismissed.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
  // This should be omitted if your application presents a lock screen
  // in -applicationDidEnterBackground:
  [self.window.rootViewController dismissViewControllerAnimated:NO completion:NULL];
}

One query. Can we open the browser like a normal push screen instead of a presenter Modal sheet? What ever the viewController i given to MSALWebviewParameters it always showing as a sheet. is there any way to open it as normal view?

My Application was Built with react native. This is the below code snippet.

@objc(login:reject:)
  func login(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
    guard let msalApplication = msalApplication else {
      // INFO: not required to show in ui. this scenario will not happen just fallback code
      reject(MSAL_INITIALIZE_MSAL_APPLICATION_ERROR, "Please Initialize Application Before loginging", nil)
      return
    }

    DispatchQueue.main.async { [weak self] in
      guard let presentedViewController = RCTPresentedViewController(), let strongSelf = self else {
        // INFO: not required to show in ui. this scenario will not happen just fallback code
        reject(self?.MSAL_RCT_PRESENTED_VIEW_CONTROLLER_ERROR, "RCTPresentedViewController Not Found", nil)
        return
      }

      let webParameters = MSALWebviewParameters(authPresentationViewController: presentedViewController)

      let interactiveParameters = MSALInteractiveTokenParameters(scopes: strongSelf.scopes, webviewParameters: webParameters)
      interactiveParameters.promptType = .selectAccount
      interactiveParameters.extraQueryParameters = strongSelf.queryParams

      msalApplication.acquireToken(with: interactiveParameters) { result, error in
        guard let authResult = result, error == nil else {
          let nsError = error! as NSError
          reject("\(nsError.code)", "SomeThingWentWrong".localized(), nsError)
          return
        }
        strongSelf.handleAcquireToken(with: authResult, resolve: resolve)
      }
    }

  }
ameyapat commented 4 days ago

@vinaykumar0339 You can set the webview type in MSALWebViewParameters. See: https://azuread.github.io/microsoft-authentication-library-for-objc/Classes/MSALWebviewParameters.html#/c:objc(cs)MSALWebviewParameters(py)webviewType and https://azuread.github.io/microsoft-authentication-library-for-objc/Enums/MSALWebviewType.html