2
votes

I am implementing the Firebase authentication by using OAuthProvider class to sign in with a personal microsoft account.

I have followed this instructions: https://firebase.google.com/docs/auth/ios/microsoft-oauth?authuser=0

However when I am using OAuthProvider of Firebase SDK it does not show up the sign in page of Microsoft, actually nothing gets invoked by getCredentialWith.

When I am using GoogleAuthProvider everything works fine and Firebase SDK shows up the sign in page of Google.

let provider = OAuthProvider(providerID: "microsoft.com")
provider.scopes = ["files.readwrite.appfolder", "user.read"]

provider.getCredentialWith(nil, completion: { credential, error in
  if let error = error {
    os_log("Firebase Error: %@", type: .fault, error as CVarArg)
    return
  }

  if (credential != nil) {
    Auth.auth().signInAndRetrieveData(with: credential!, completion: { authResult, error in
      if let error = error {
        os_log("Firebase Error: %@", type: .fault, error as CVarArg)
        return
      }
    })
  }
})
2

2 Answers

4
votes

Declare in global scope like below

var provider: OAuthProvider?
var authMicrosoft: Auth?


@IBAction func buttonTapped(_ sender: Any) {


     provider = OAuthProvider(providerID: "microsoft.com")


    provider?.customParameters = [
        "prompt": "consent",
                "login_hint": "",
    ]

    provider?.scopes = ["mail.read", "calendars.read"]




    provider?.getCredentialWith(nil ) { credential, error in
        if error != nil {
            // Handle error.
        }

        print(credential?.provider)



        if let x = credential {
            self. authMicrosoft?.signIn(with: x) { authResult, error in
                if error != nil {
                    // Handle error.
                }


                print(authResult?.additionalUserInfo?.profile)
                print(authResult?.user.providerID)


            }
        } else {

        }

    }

}
3
votes

It seems that you define a provider locally. While the getCredentialWith(_:completion) call gets executed asynchronously, your local function may already finished executing, and the provider may already be deallocated by ARC.

To solve your problem, you may need to specifically retain the provider pointer somewhere --- for instance in your view controller as a property or ivar, and by that way the provider can be created/recycled when your view controller is initialized/deallocated.